I'm currently using an OnImageCaptured
callback to get my image instead of saving it to the device. I'm having trouble understanding when it's necessary to rotate an image when it comes from an ImageProxy
.
I use the following method to convert the data from an ImageProxy
to a BitMap
:
...
val buffer: ByteBuffer = imageProxy.planes[0].buffer // Only first plane because of JPEG format.
val bytes = ByteArray(buffer.remaining())
buffer.get(bytes)
return BitmapFactory.decodeByteArray(bytes, 0, bytes.size)
The resulting bitmap is sometimes rotated, and sometimes not, depending on the device the picture is taken from. ImageProxy.getImageInfo().getRotationDegrees()
returns the correct rotation, but I don't know when it's necessary to apply it, since sometimes it's applied in the bitmap, and sometimes not.
The ImageCapture.OnCapturedImageListener
documentation also says:
The image is provided as captured by the underlying ImageReader without rotation applied. rotationDegrees describes the magnitude of clockwise rotation, which if applied to the image will make it match the currently configured target rotation.
which leads me to think that I'm getting the bitmap incorrectly, because sometimes it has the rotation applied. Is there something I'm missing here?
Well, as it turn out the only necessary information is the Exif metadata. rotationDegrees
contains the final orientation the image should be in, starting from the base orientation, but the Exif metadata only shows the rotation I had to make to get the final result. So rotating according to TAG_ORIENTATION
solved the issue.
1.0.0-beta02
, so now the exif metadata and rotationDegrees
contain the same information.