我如何知道图像是否在缓冲区中旋转?

How do I know if an image is rotated on the buffer?

我目前正在使用 OnImageCaptured 回调来获取我的图像,而不是将其保存到设备中。当图像来自 ImageProxy 时,我无法理解何时需要旋转图像。

我使用以下方法将数据从 ImageProxy 转换为 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)

生成的位图有时旋转,有时不旋转,具体取决于拍摄图片的设备。 ImageProxy.getImageInfo().getRotationDegrees() returns 正确的旋转,但我不知道什么时候需要应用它,因为有时它应用在位图中,有时不应用。

ImageCapture.OnCapturedImageListener documentation 还说:

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.

这让我认为我得到的位图不正确,因为有时它应用了旋转。我在这里遗漏了什么吗?

好吧,事实证明唯一必要的信息是 Exif 元数据。 rotationDegrees 包含图像应处于的最终方向,从基本方向开始,但 Exif 元数据仅显示我为获得最终结果而必须进行的旋转。所以根据 TAG_ORIENTATION 旋转解决了这个问题。

  • 更新:这是 CameraX 库本身的问题。它已在 1.0.0-beta02 中修复,因此现在 exif 元数据和 rotationDegrees 包含相同的信息。