Camera2控制图像方向



我基于Googlecamera2示例创建了相机屏幕,所有代码几乎相同,相机拍照并以JPEG格式保存在设备上,但我有一些奇怪的行为。

例如,从模拟器中拍照会将图像旋转90度(图像旋转,而不是预览(,在我的华为上,图像不会旋转。

奇怪的是,Emulator和华为的屏幕旋转和传感器方向值完全相同。那么jpeg方向究竟是如何设置的呢?

同样在探索CaptureRequest.JPEG_ORIENTATION

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

我注意到此方法对Emulator没有任何影响

我试图在位图保存后从ExifInterface获得JPEG方向,但在Emulator和Huawei中,值都是ORIENTATION_UNDEFINED。也许在将Image(从ImageReader(转换为File时忽略了Exif标记?

也许我需要在拍摄图像时手动设置ExifInterface,但如果值相同,有什么区别?我们应该如何控制JPEG的方向?

使用这种方法来获得方向(来自Google camera2示例(,Emulator和华为的结果是90度。

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

用这种方法从ImageReader中得到Bitmap

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

Emulator是使用Camera2 API的一个非常糟糕的起点。从本质上讲,它有LEGACY Camera2的支持,但也有一些怪癖。

也就是说,Jpeg定向在安卓相机上是一个非常微妙的话题。官方文件解释说,旋转请求可能适用于图像本身,也可能仅适用于EXIF标志,但有些设备(华外测试了哪些设备?(根本不符合要求。

还要注意,BitmapFactory.decodeByteArray()从一开始就忽略EXIF标志。

最新更新