Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting array of int to Bitmap on Android

I have an MxN array of ints representing colors (say RGBA format, but that is easily changeable). I would like to convert them to an MxN Bitmap or something else (such as an OpenGL texture) that I can render to the screen. Is there a fast way to do this? Looping through the array and drawing them to the canvas is far too slow.

like image 230
mindvirus Avatar asked Oct 21 '11 13:10

mindvirus


People also ask

How to convert int array to bitmap in android?

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.

How to Create bitmap array in android?

You can make bitmap aaray in android like this, byte[] bMapArray= new byte[buf. available()]; buf. read(bMapArray); Bitmap bMap = BitmapFactory.

How to Create bitmap in android?

To create a bitmap from a resource, you use the BitmapFactory method decodeResource(): Bitmap bitmap = BitmapFactory. decodeResource(getResources(), R. drawable.


1 Answers

Try this, it will give you the bitmap:

 // You are using RGBA that's 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));

Or you can generate IntBuffer from the 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[i]);
    }
    dst.rewind();
    return dst;
}
like image 153
dharam Avatar answered Oct 29 '22 18:10

dharam