BitmapFactory.decodeByteArray() always returns nul

2019-06-02 04:48发布

So i'm trying to port some C++ code from a colleague that grabs image data over a Bluetoth serial port (I'm using an Android phone). From the data I will need to generate a bitmap.

Before testing the ported code, I wrote this quick function to suposedly generate a pure red rectangle. However, BitmapFactory.decodeByteArray() always fails and returns with a null bitmap. I've checked for both of the possible exeptions it can throw and neither one is thrown.

byte[] pixelData = new byte[225*160*4];
                for(int i = 0; i < 225*160; i++) {
                    pixelData[i * 4 + 0] = (byte)255;
                    pixelData[i * 4 + 1] = (byte)255;
                    pixelData[i * 4 + 2] = (byte)0;
                    pixelData[i * 4 + 3] = (byte)0;
                }
                Bitmap image = null;
                logBox.append("Creating bitmap from pixel data...\n");
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inPreferredConfig = Bitmap.Config.ARGB_8888;
                options.outWidth = 225;
                options.outHeight = 160;

                try {
                    image = BitmapFactory.decodeByteArray(pixelData, 0, pixelData.length, options);
                } catch (IllegalArgumentException e) {
                    logBox.append(e.toString() + '\n');
                }
                //pixelData = null;
                logBox.append("Bitmap generation complete\n");

decodeByteArray() code:

public static Bitmap decodeByteArray(byte[] data, int offset, int length, Options opts) {
    if ((offset | length) < 0 || data.length < offset + length) {
        throw new ArrayIndexOutOfBoundsException();
    }

    Bitmap bm;

    Trace.traceBegin(Trace.TRACE_TAG_GRAPHICS, "decodeBitmap");
    try {
        bm = nativeDecodeByteArray(data, offset, length, opts);

        if (bm == null && opts != null && opts.inBitmap != null) {
            throw new IllegalArgumentException("Problem decoding into existing bitmap");
        }
        setDensityFromOptions(bm, opts);
    } finally {
        Trace.traceEnd(Trace.TRACE_TAG_GRAPHICS);
    }

    return bm;
}

I would presume that it's nativeDecodeByteArray() that is failing.

I also notice the log message:

D/skia: --- SkImageDecoder::Factory returned null

Anyone got any ideas?

1条回答
趁早两清
2楼-- · 2019-06-02 05:14

decodeByteArray of BitmapFactory actually decodes an image, i.e. an image that has been encoded in a format such as JPEG or PNG. decodeFile and decodeStream make a little more sense, since your encoded image would probably be coming from a file or server or something.

You don't want to decode anything. You are trying to get raw image data into a bitmap. Looking at your code it appears you are generating a 225 x 160 bitmap with 4 bytes per pixel, formatted ARGB. So this code should work for you:

    int width = 225;
    int height = 160;
    int size = width * height;
    int[] pixelData = new byte[size];
    for (int i = 0; i < size; i++) {
        // pack 4 bytes into int for ARGB_8888
        pixelData[i] = ((0xFF & (byte)255) << 24) // alpha, 8 bits
                | ((0xFF & (byte)255) << 16)      // red, 8 bits
                | ((0xFF & (byte)0) << 8)         // green, 8 bits
                | (0xFF & (byte)0);               // blue, 8 bits
    }

    Bitmap image = Bitmap.createBitmap(pixelData, width, height, Bitmap.Config.ARGB_8888);
查看更多
登录 后发表回答