Home:ALL Converter>Camera2 control image orientation

Camera2 control image orientation

Ask Time:2020-02-29T23:27:43         Author:Pavel Poley

Json Formatter

I have created camera screen based on Google camera2 sample, all code almost identical, the camera takes photo and saves it on the device in JPEG format, but I have some weird behavior.

For example, taking photo from emulator rotates the image 90 degrees(the image rotated, not preview), on my Huawei the image not rotated.

What weird is that screen rotation and sensor orientation values is identical both on Emulator and Huawei. So how exactly the jpeg orientation is sets?

Also while exploring CaptureRequest.JPEG_ORIENTATION

captureBuilder.set(CaptureRequest.JPEG_ORIENTATION, getOrientation(rotation))

I have noticed that this method have no effect on Emulator at all.

I have tried to get JPEG orientation from ExifInterface after bitmap saved, but in both Emulator and Huawei the value is ORIENTATION_UNDEFINED. Maybe while converting Image(from ImageReader) to File Exif tags ignored?

Maybe i need to set the ExifInterface manually while taking image, but if the values is identical what is the difference? How we supposed to control the JPEG orientation?

Using this method to get orientation(from Google camera2 sample) result is 90 degrees for Emulator and Huawei.

private int getOrientation(int rotation) {
        return (ORIENTATIONS.get(rotation) + mSensorOrientation + 270) % 360;
    }

using this method to get Bitmap from ImageReader

public static Bitmap getBitmapFromReader(ImageReader reader) {

    Bitmap bitmap = null;
    Image image = null;

    try {
        image = reader.acquireLatestImage();
        Image.Plane[] planes = image.getPlanes();
        ByteBuffer buffer = planes[0].getBuffer();
        buffer.rewind();
        byte[] data = new byte[buffer.capacity()];
        buffer.get(data);
        bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);

    } catch (Exception e) {
        e.printStackTrace();
    }
    if (image != null) {
        image.close();
    }

    return bitmap;
}

Author:Pavel Poley,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/60466670/camera2-control-image-orientation
yy