android - 在 android 版本 10 中使用 camera2 api 捕获时出现图像方向问题
问题描述
我已将我的相机功能从 Camera 更新为 camera2 api。
当我用前置摄像头捕捉图像并将其显示在 imageview 中时。图像的方向被改变。然后我使用此代码更改图像的方向。
int jpegOrientation =
(ORIENTATIONS.get(rotation) + characteristics.get(CameraCharacteristics.SENSOR_ORIENTATION) + 270) % 360;
capturebuilder.set(CaptureRequest.JPEG_ORIENTATION, jpegOrientation);
上面的代码适用于我测试过的 android 版本(即版本 5,9)。当我在 Android 版本 10 中运行相同的代码时,它不起作用。下面是图片
拜托,任何人都可以帮助我解决这个问题
解决方案
private int getJpegOrientation(CameraCharacteristics c, int deviceOrientation) {
if (deviceOrientation == android.view.OrientationEventListener.ORIENTATION_UNKNOWN) return 0;
int sensorOrientation = c.get(CameraCharacteristics.SENSOR_ORIENTATION);
// Round device orientation to a multiple of 90
deviceOrientation = (deviceOrientation + 45) / 90 * 90;
// Reverse device orientation for front-facing cameras
boolean facingFront = c.get(CameraCharacteristics.LENS_FACING) == CameraCharacteristics.LENS_FACING_FRONT;
if (facingFront) deviceOrientation = -deviceOrientation;
// Calculate desired JPEG orientation relative to camera orientation to make
// the image upright relative to the device orientation
int jpegOrientation = (sensorOrientation + deviceOrientation + 360) % 360;
return jpegOrientation;
}
要了解更多详细信息,请关注此帖子:https ://medium.com/@kenodoggy/solving-image-rotation-on-android-using-camera2-api-7b3ed3518ab6
推荐阅读
- sockets - Flutter `socket_io_client` 将 socket.on(event, data) 转换为流并在动态生成的流构建器中使用
- python - 如何从python中的字符串中删除所有字符,直到第n个字符X出现
- validation - 如果 yii2 中另一组字段为空,我想设置一组字段。任何一组字段都需要完美填写(必填)
- javascript - 如何在模糊时触发 React-hook-form Controller 验证?
- python - Tkinter 帧大小定义为父帧大小的一部分
- javascript - JS 对象、Python 字典和 C++ 结构之间有什么区别?
- java - 可以在 solr 中执行不带字段名的部分搜索
- python - 如何创建数据框并使用数组和 for 循环调用它们?
- javascript - 如何从Javascript中的数组中获取元素的随机组合?
- c# - Winforms OnFormClosing 和 Disposing/Close