1

我一直在尝试使用此处的示例:
J2ME:将透明 PNG 图像转换为灰度
,此处:
http ://www.java2s.com/Code/Java/Collections-Data-Structure/intarraytobytearray.htm

即时将位图图像对象转换为灰度,但是当我尝试将字节重新编码为图像时遇到问题,并且出现以下错误/堆栈:

(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;
}
4

2 回答 2

4

引用EncodedImage javadoc

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

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

于 2011-08-10T21:33:46.787 回答
2

扩展Scott W答案。

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

因此,正如Scott W您所说,您应该使用Bitmap.setARGB()结果字节数组来获得Bitmap转换后的数据。

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

JPEGEncodedImage eImage = JPEGEncodedImage.encode(bitmap, 75);
byte[] fileData = eImage.getData();
// open a FileConnection and write the fileData
于 2011-08-11T08:16:40.480 回答