BitmapFactory.decodeByteArray()始终返回null(手动创建的字节数组) [英] BitmapFactory.decodeByteArray() always returns null (manually-created byte array)

查看:274
本文介绍了BitmapFactory.decodeByteArray()始终返回null(手动创建的字节数组)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我试图从同事那里移植一些C ++代码,这些代码通过Bluetoth串口抓取图像数据(我使用的是Android手机)。从数据中我将需要生成一个位图。

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.

在测试移植代码之前,我将这个快速函数写入 suposedly 生成一个纯红色长方形。但是,BitmapFactory.decodeByteArray()始终失败并返回null位图。我已经检查了它可以抛出的两个可能的exeptions并且都没有抛出。

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()代码:

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

我认为它是 nativeDecodeByteArray()失败了。

我还注意到日志信息:


D / skia :--- SkImageDecoder :: Factory返回null

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

任何人都有任何想法?

推荐答案

decodeByteArray of BitmapFactory 实际解码图像,即以JPEG或PNG格式编码的图像。 decodeFile decodeStream 更有意义,因为您的编码图像可能来自文件或服务器或其他东西。

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.

您不想解码任何东西。您正在尝试将原始图像数据转换为位图。看一下你的代码,看来你正在生成一个225 x 160位图,每个像素有4个字节,格式化为ARGB。所以这段代码应该适合你:

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);

这篇关于BitmapFactory.decodeByteArray()始终返回null(手动创建的字节数组)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆