【问题标题】:Convert a Bitmap image to grayscale within BlackBerry J2Me在 BlackBerry J2Me 中将位图图像转换为灰度
【发布时间】:2011-08-11 20:43:47
【问题描述】:

我一直在尝试使用这里的示例:
J2ME: Convert transparent PNG image to grayscale
在这里:
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;
}

【问题讨论】:

    标签: blackberry image-processing java-me grayscale


    【解决方案1】:

    引用EncodedImage javadoc

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

    你为什么要摆弄 EncodedImage?看来您应该能够只创建第二个位图并使用setARGB()

    【讨论】:

      【解决方案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
      

      【讨论】:

      • 感谢 Scott 和 Arhimed 的跟进,但输入的数据类型是 png 文件格式,我只是想将每个像素转换为灰度。我会尝试你的建议,看看我是否有运气。感谢您的快速回复!
      猜你喜欢
      • 2011-01-25
      • 1970-01-01
      • 1970-01-01
      • 2010-11-20
      • 1970-01-01
      相关资源
      最近更新 更多