I obtain raw camera data rawData
of type byte[]
, format RGBA, size 640x480, 4 bytes per pixel, from a library function. And I need to convert it to a Bitmap and display in an ImageView on the screen.
What I do is the following:
byte[] JPEGData = convertToJpeg(rawData, 640, 480, 80);
Bitmap bitmap = BitmapFactory.decodeByteArray(JPEGData , 0, JPEGData .length);
imageView.setImageBitmap(bitmap);
where convertToJpeg()
function is:
public static byte[] convertToJpeg(byte[] buffer, int w, int h, int quality) {
YuvImage yuv_image = new YuvImage(buffer, ImageFormat.NV21, w, h, null);
Rect rect = new Rect(0, 0, w, h);
ByteArrayOutputStream output_stream = new ByteArrayOutputStream();
yuv_image.compressToJpeg(rect, quality, output_stream);
Bitmap sourceBmp = BitmapFactory.decodeByteArray(output_stream.toByteArray(), 0, output_stream.size());
Bitmap destBmp = Bitmap.createScaledBitmap(sourceBmp, (int) (w * 0.75), (int) (h * 0.75), true);
ByteArrayOutputStream pictureStream = new ByteArrayOutputStream();
destBmp.compress(CompressFormat.JPEG, quality, pictureStream);
byte[] pictureByteArray = pictureStream.toByteArray();
return pictureByteArray;
}
After decodeByteArray()
call I have bitmap.getConfig() == ARGB_8888
.
However, what I see on the screen is some chaotic picture, with some blurry green shapes of what's been in the original picture.
What's wrong with it?