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 |
public static Bitmap createBitmap( int colors[], int offset, int stride, int width, int height, 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 |
public
static
int
convertByteToInt(
byte
data){
int heightBit = (
int
) ((data>>
4
) &
0x0F
);
int
lowBit = (
int
) (
0x0F
& data);
return
heightBit *
16
+ lowBit;
}
public
static
int
[] convertByteToColor(
byte
[] data){
int
size = data.length;
if
(size ==
0
){
return
null
;
}
int
arg =
0
;
if
(size %
3
!=
0
){
arg =
1
;
}
int
[]color =
new
int
[size /
3
+ arg];
int
red, 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
(
int
i =
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
;
}
return
color;
}
Bitmap decodeFrameToBitmap(
byte
[] frame)
{
int
[]colors = convertByteToColor(frame);
if
(colors ==
null
){
return
null
;
}
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