How to convert bitmap byte nude data to bitmap images in Android int data 2014-06-11 10:45:14 read 375 times
We processed the BMP image raw data in jni, how should we convert it to bitmap?
Because the resulting data is unsigned char * type data, and for bitmap class, its class method inside:
| 12 |
publicstatic Bitmap createBitmap(int colors[], int offset, int stride, int width, intheight, Config config) |
Required to pass in int * data, here we need to convert unsigned char * data to an int value of RGB.
The Java method can take the following code:
| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
publicstaticintconvertByteToInt(bytedata){ int heightBit = (int) ((data>>4) & 0x0F); intlowBit = (int) (0x0F& data); returnheightBit * 16+ lowBit; } publicstaticint[] convertByteToColor(byte[] data){ intsize = data.length; if(size == 0){ returnnull; } intarg = 0; if(size % 3!= 0){ arg = 1; } int[]color = newint[size / 3+ arg]; intred, green, blue; if(arg == 0){ for(int i = 0; i < color.length; ++i){ red = convertByteToInt(data[i * 3]); green = convertByteToInt(data[i * 3+ 1]); blue = convertByteToInt(data[i * 3+ 2]); color[i] = (red << 16) | (green << 8) | blue | 0xFF000000; } }else{ for(inti = 0; i < color.length - 1; ++i){ red = convertByteToInt(data[i * 3]); green = convertByteToInt(data[i * 3 + 1]); blue = convertByteToInt(data[i * 3+ 2]); color[i] = (red << 16) | (green << 8) | blue | 0xFF000000; } color[color.length - 1] = 0xFF000000; } returncolor; } Bitmap decodeFrameToBitmap(byte[] frame) { int[]colors = convertByteToColor(frame); if(colors == null){ returnnull; } Bitmap bmp = Bitmap.createBitmap(colors, 0, 1280, 1280, 720,Bitmap.Config.ARGB_8888); return bmp; } |
The code does not explain, has the question blog to mention, will answer.
-end-
How to convert bitmap byte nude data into bitmap picture int data in Android