Render byte [] as a bitmap in Android

I get a byte array from a JNI call and try to build a Bitmap object with it.

My problem: the following code returns null.

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

Any tips on this?

PS: The pixel layout is BGR, not RGB.

+6
java android
source share
2 answers

DecodeByteArray really does not work with this format. I switch 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++; } 

but

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

may cause:

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

+1
source share

Doc says the method returns "null if the image cannot be decoded." You can try:

 byte[] image = services.getImageBuffer(1024, 600); InputStream is = new ByteArrayInputStream(image); Bitmap bmp = BitmapFactory.decodeStream(is); 

Even if I don’t think it will change anything, though .. Try to look at android.graphics.BitmapFactory.Options, as well

+4
source share

All Articles