Cropping rectangular area from bitmao using YuvImage produces strange result

929 views Asked by At

I have attempted to crop image from a Bitmap using YuvImage like this:

        Bitmap bitmap=params[0];
        ByteArrayOutputStream bos=new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);
        byte[] data=bos.toByteArray();
        //bos=null;
        YuvImage yuvImage=new YuvImage(data, ImageFormat.NV21, bitmap.getWidth(), bitmap.getHeight(),null);
        bos=new ByteArrayOutputStream();
        boolean isCropped=yuvImage.compressToJpeg(rect, 100, bos);
        if(isCropped)
            data=bos.toByteArray();
        bitmap=BitmapFactory.decodeByteArray(data, 0, data.length);
        return bitmap;

This causes the resulting imageStrange and scary...like an old Black and White TV

Is there an alternate approach in order to crop and save the image without using the Intent.

1

There are 1 answers

3
auselen On BEST ANSWER

You consider Bitmap.CompressFormat.JPEG as ImageFormat.NV21 which shouldn't be true.