1
votes

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;
}
1

1 Answers

1
votes

Emulator is a very bad starting point to work with Camera2 API. Essentially, it has LEGACY Camera2 support, with some quirks.

This said, Jpeg orientation is a very delicate topic on Android camera. The official docs explain that rotation request may apply to the image itself, or to EXIF flag only, but some devices (which Huawai did you test?) don't comply at all.

Also note that BitmapFactory.decodeByteArray() ignores the EXIF flag, since the very beginning.