Convert int array to Bitmap on Android

I have an MxN array from int representing colors (let's say RGBA format, but this is easily replaced). I would like to convert them to MxN Bitmap or something else (like OpenGL texture) that I can display on the screen. Is there a quick way to do this? Passing through an array and drawing them on canvas is too slow.

+8
java android opengl-es
source share
3 answers

Try it, it will give you a bitmap.

// You are using RGBA that why Config is ARGB.8888 bitmap = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888); // vector is your int[] of ARGB bitmap.copyPixelsFromBuffer(IntBuffer.wrap(vector)); 

EDIT:

  //OR , you can generate IntBuffer from following native method /*private IntBuffer makeBuffer(int[] src, int n) { IntBuffer dst = IntBuffer.allocate(n*n); for (int i = 0; i < n; i++) { dst.put(src); } dst.rewind(); return dst; }*/ 

Hope this helps you.

+15
source share

Why not use Bitmap.setPixel ? This is even a level 1 API:

 int[] array = your array of pixels here... int width = width of "array"... int height = height of "array"... // Create bitmap Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); // Set the pixels bitmap.setPixels(array, 0, width, 0, 0, width, height); 

You can play with offset / pitch / x / y as needed.
No cycles. No additional appropriations.

+8
source share

Yes, it looks like you have all the information you need. If M is width and N is height, you can create a new bitmap using Bitmap.createBitmap, and you can fill in the ARGB values ​​using the setPixels method, which takes an int array.

Bitmap.createBitmap

Bitmap.setPixels

+3
source share

All Articles