渲染一个byte []为位图的Andr​​oid [英] Render a byte[] as Bitmap in Android

查看:205
本文介绍了渲染一个byte []为位图的Andr​​oid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到来自JNI调用的字节数组,并试图建立一个位图对象吧。

I'm getting a byte array from a JNI call, and trying to construct a Bitmap object with it.

我的问题是,下面的code,返回null。

My problem is, the following code, returns null.

    byte[] image = services.getImageBuffer(1024, 600);
    Bitmap bmp = BitmapFactory.decodeByteArray(image, 0, image.length);

这件事有什么建议?

Any tips about it?

PS:像素布局BGR,而不是RGB

PS: The pixel layout is BGR, not a RGB.

推荐答案

本德codeByteArray真的好好尝试一下的作品与此格式。我从BGR改为手动RGB。

The decodeByteArray really doens't works with this format. I change from BGR to RGB manually.

    byte[] image = services.getImageBuffer(1024, 600);

    Bitmap bmp = Bitmap.createBitmap(1024, 600, Bitmap.Config.RGB_565);
    int row = 0, col = 0;
    for (int i = 0; i < image.length; i += 3) {
        bmp.setPixel(col++, row, image[i + 2] & image[i + 1] & image[i]);

        if (col == 1024) {
            col = 0;
            row++;
        }

不过,

for (i < image.length) 。。。bmp.setPixel(image[i + 2] & image[i + 1] & image[i]); 

可能会导致:

can cause:

08-29 14:34:23.460:ERROR / AndroidRuntime(8638):java.lang.ArrayIndexOutOfBoundsException

08-29 14:34:23.460: ERROR/AndroidRuntime(8638): java.lang.ArrayIndexOutOfBoundsException

这篇关于渲染一个byte []为位图的Andr​​oid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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