Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Why does the image break when converted to byte[]? (takePicture with Opencv on Android)

I have an project that used Camera and Opencv. I take picture and crop image with OpenCv. After I have a Mat file to convert Jpeg for send byte[] data to Activity.

Mat to Bitmap

bmp2 = Bitmap.createBitmap(checkFrame.cols(),checkFrame.rows(), Bitmap.Config.ARGB_8888);
Utils.matToBitmap(checkFrame, bmp2, true);
checkFrame.release();

Bitmap to byte[]

ByteArrayOutputStream stream2 = new ByteArrayOutputStream();
bmp2.compress(Bitmap.CompressFormat.JPEG, 0,stream2);
bmp2.recycle();
bmp2 = null;
byte[] byteArray2 = stream2.toByteArray();

On Activity ,

((ImageView) findViewById(R.id.photoView))
                                .setImageBitmap(BitmapFactory
                                        .decodeByteArray(photo2, 0,
                                                photo2.length));

But this image is not same with I cropped. This is like 16bit color... If I direct send Bitmap to activity and set ImageBitmap, this is good. Why does the image break when it's converted to byte[]?

click to hide/show revision 2
retagged

updated 2013-09-16 13:10:17 -0600

berak gravatar image

Why does the image break when converted to byte[]? (takePicture with Opencv on Android)

I have an project that used Camera and Opencv. I take picture and crop image with OpenCv. After I have a Mat file to convert Jpeg for send byte[] data to Activity.

Mat to Bitmap

bmp2 = Bitmap.createBitmap(checkFrame.cols(),checkFrame.rows(), Bitmap.Config.ARGB_8888);
Utils.matToBitmap(checkFrame, bmp2, true);
checkFrame.release();

Bitmap to byte[]

ByteArrayOutputStream stream2 = new ByteArrayOutputStream();
bmp2.compress(Bitmap.CompressFormat.JPEG, 0,stream2);
bmp2.recycle();
bmp2 = null;
byte[] byteArray2 = stream2.toByteArray();

On Activity ,

((ImageView) findViewById(R.id.photoView))
                                .setImageBitmap(BitmapFactory
                                        .decodeByteArray(photo2, 0,
                                                photo2.length));

But this image is not same with I cropped. This is like 16bit color... If I direct send Bitmap to activity and set ImageBitmap, this is good. Why does the image break when it's converted to byte[]?