1

私はここからサンプルを使用しようとしています:
J2ME: Convert transparent PNG image to grayscale
and here:
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がスローされます。

EncodedImage をいじるのはなぜですか? 2番目のビットマップを作成して使用できるはずです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 画像ファイルを開いてファイル バイトを読み取ると、取得したバイトを使用して を作成できます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
于 2011-08-11T08:16:40.480 に答える