Thursday, 13 May 2021

Android: Converting an Image to a Bitmap changes aspect ratio of the image

I am converting an android Image captured in my application to a bitmap. I am doing this by getting the image buffer from the pixel plane of the image and then using BitMapFactory to decode it into a Bitmap. However, doing so seems to change the resolution of the Image from 1920 x 1440 to 1800 x 1600, cropping out the top and bottom of the image in the process. The code for the method is shown here.

`protected void getImageFromBuffer(ImageReader reader){
    Image image = null;
    image = reader.acquireLatestImage();

    ByteBuffer buffer = image.getPlanes()[0].getBuffer();
    System.out.println("Getting Image Ready");


    synchronized (this){
        image_to_upload = new byte[buffer.capacity()];
        buffer.get(image_to_upload);

        Bitmap storedBitmap = BitmapFactory.decodeByteArray(image_to_upload, 0, image_to_upload.length, null);

        Matrix mat = new Matrix();
        mat.postRotate(jpegOrientation);  // angle is the desired angle you wish to rotate
        storedBitmap = Bitmap.createBitmap(storedBitmap, 0, 0, storedBitmap.getWidth(), storedBitmap.getHeight(), mat, true);
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        storedBitmap.compress(Bitmap.CompressFormat.JPEG,70, byteArrayOutputStream);
        image_to_upload = byteArrayOutputStream.toByteArray();

        image_ready = true;
        System.out.println("Image Ready");
    }

}`

Debugging shows that the height and width of the Image are correct before the buffer is converted to a bitmap, but the bitmap dimensions are wrong immediately after decodeByteArray. Can anyone suggest why this may be?



from Android: Converting an Image to a Bitmap changes aspect ratio of the image

No comments:

Post a Comment