在BlackBerry J2Me中将位图图像转换为灰度

时间:2011-08-10 20:13:04

标签: blackberry image-processing java-me grayscale

我一直在尝试使用这里的样本:
J2ME: Convert transparent PNG image to grayscale
在这里:
http://www.java2s.com/Code/Java/Collections-Data-Structure/intarraytobytearray.htm

将Bitmap图像对象动态转换为灰度,但当我尝试将字节重新编码为图像时遇到问题,我得到以下错误/堆栈:

(Suspended (exception IllegalArgumentException))    
EncodedImage.createEncodedImage(byte[], int, int, String) line: 367 
EncodedImage.createEncodedImage(byte[], int, int) line: 279 
ScreenTemp.getGrayScaleImage(Bitmap) line: 404

这是我正在尝试的代码:

    Bitmap btemp = getGrayScaleImage(Bitmap.getBitmapResource("add.png"));
    BitmapField bftemp = new BitmapField(btemp, BitmapField.FOCUSABLE | BitmapField.FIELD_HCENTER | BitmapField.FIELD_VCENTER);
    add(bftemp);


    public Bitmap getGrayScaleImage(Bitmap image) {
    int width = image.getWidth();
    int height = image.getHeight();     
    int[] rgbData = new int[width * height];        
    image.getARGB(rgbData, 0, width, 0, 0, width, height);
    for (int x = 0; x < width*height ; x++) {
        rgbData[x] = getGrayScale(rgbData[x]);
    }
    byte[] b = int2byte(rgbData);
    final EncodedImage jpegPic = EncodedImage.createEncodedImage(b, 0, b.length);
    return jpegPic.getBitmap();
}
private int getGrayScale(int c) {
    int[] p = new int[4];
    p[0] = (int) ((c & 0xFF000000) >>> 24); // Opacity level
    p[1] = (int) ((c & 0x00FF0000) >>> 16); // Red level
    p[2] = (int) ((c & 0x0000FF00) >>> 8); // Green level
    p[3] = (int) (c & 0x000000FF); // Blue level

    int nc = p[1] / 3 + p[2] / 3 + p[3] / 3;
    // a little bit brighter
    nc = nc / 2 + 127;

    p[1] = nc;
    p[2] = nc;
    p[3] = nc;

    int gc = (p[0] << 24 | p[1] << 16 | p[2] << 8 | p[3]);
    return gc;
}
private static byte[] int2byte(int[] src) {
    int srcLength = src.length;
    byte[]dst = new byte[srcLength << 2];

    for (int i=0; i<srcLength; i++) {
        int x = src[i];
        int j = i << 2;
        dst[j++] = (byte) ((x >>> 0) & 0xff);           
        dst[j++] = (byte) ((x >>> 8) & 0xff);
        dst[j++] = (byte) ((x >>> 16) & 0xff);
        dst[j++] = (byte) ((x >>> 24) & 0xff);
    }
    return dst;
}

任何帮助都会很棒!

谢谢, 贾斯汀

编辑: 感谢以下信息,我能解决这个问题。这是代码。你不再需要int2byte,这里是更新的getGrayScaleImage方法:

public Bitmap getGrayScaleImage(Bitmap image) {
    int width = image.getWidth();
    int height = image.getHeight();     
    int[] rgbData = new int[width * height];        
    image.getARGB(rgbData, 0, width, 0, 0, width, height);
    for (int x = 0; x < width*height ; x++) {
        rgbData[x] = getGrayScale(rgbData[x]);
    }
    byte[] b = int2byte(rgbData);
    Bitmap bit = new Bitmap(width, height);
    bit.setARGB(rgbData, 0, width, 0, 0, width, height);
    return bit;
}

2 个答案:

答案 0 :(得分:4)

引用EncodedImage javadoc

  

如果无法识别图像格式,则会抛出IllegalArgumentException

你为什么要摆弄EncodedImage?您似乎应该能够创建第二个Bitmap并使用setARGB()

答案 1 :(得分:2)

扩展Scott W回答。

EncodedImage.createEncodedImage(byte[] data, int offset, int length)需要支持图像类型的字节数组(TIFF,BMP,JPEG,GIF,WBMP或PNG)。例如,如果您打开JPEG图像文件,读取文件字节,那么就可以使用获取的字节来创建EncodedImage(实际上它将是JPEGEncodedImage

因此,Scott W表示您应该使用Bitmap.setARGB()生成的字节数组,以便Bitmap转换数据。

然后,如果你需要将图像保存为JPEG文件,你可以像这样使用smth:

JPEGEncodedImage eImage = JPEGEncodedImage.encode(bitmap, 75);
byte[] fileData = eImage.getData();
// open a FileConnection and write the fileData