android - 如果应用程序的默认方向设置为纵向模式,我们如何检测使用 CameraX 捕获的图像的方向
问题描述
基本上,我的相机应用程序设置为人像模式。但是,用户可以通过相应地旋转手机来拍摄肖像或风景照片(应用程序不旋转)。
所以我的问题是,我们如何找到捕获的图像方向?
我尝试使用DisplayManager.DisplayListener
,但是,它仅在应用程序的方向发生时才有效。由于我已阻止应用程序的方向为纵向模式,因此这里没有任何回调。
我什至尝试使用ExifInterface
,但是,它总是给出 6 作为旋转。
我正在寻找使用 CameraX api 的解决方案。
解决方案
我也有这个问题。解决它的方法是使用设备传感器数据来获得正确的方向,然后将其设置在我的imageCapture
对象中。请参阅下面的片段。
orientationEventListener = object : OrientationEventListener(context) {
override fun onOrientationChanged(orientation: Int) {
// Monitors orientation values to determine the target rotation value
val rotation = if (orientation >= 45 && orientation < 135) {
Surface.ROTATION_270
} else if (orientation >= 135 && orientation < 225) {
Surface.ROTATION_180
} else if (orientation >= 225 && orientation < 315) {
Surface.ROTATION_90
} else {
Surface.ROTATION_0
}
imageCapture?.setTargetRotation(rotation)
}
}
这也是 Google 问题跟踪器中类似问题的推荐方法:https ://issuetracker.google.com/issues/144944155
推荐阅读
- node.js - 如何处理 Node.js 中的异步错误
- angular - 如何将附加行附加到现有的 Observable
- sapui5 - 如何从 SmartTable 的视图设置中删除列?
- c++ - 编译Boost时OS X上的“ld:未知选项:-soname”
- android - 分组通知计数 (+1),例如 Gmail
- matlab - 为什么颜色会随着循环中的 MATLAB contourf 而变化?
- c# - 将 WPF DataGrid 复制到剪贴板需要很长时间
- ms-access - MS Access VBA .additem 到列表框下标超出范围错误
- node.js - 如何将 async/await 与 mongoose 聚合一起使用
- c# - VSTO/Redemption - 在 Outlook application.Session 上获取 NullReferenceException