如果应用程序的默认方向设置为纵向模式,我们如何检测使用CameraX捕获的图像的方向 [英] How do we detect the Orientation of Image captured using CameraX if Application's default orientation is set to Portrait Mode
问题描述
基本上,我的相机应用设置为人像模式。但是,用户可以通过相应地旋转电话来在Potrait或风景中拍照(应用程序不会旋转)。
Basically, My camera app is set to Portrait Mode. However, user can take photos in Potrait or landscape by rotating the phone accordingly (The app doesnt rotate).
所以我的问题是,我们如何找到捕获的图像方向?
So my question is, how can we find the captured image orientation?
我尝试使用 DisplayManager.DisplayListener
,但是,它仅在定向应用程序时有效。由于我已将应用程序的方向阻止为纵向模式,因此这里没有任何回调。
I tried using DisplayManager.DisplayListener
, however, it works only when orientation of app happens. Since I have blocked the orientation of app to portrait mode, it doesnt get any callbacks here.
我什至尝试使用 ExifInterface
,但是,它始终将6旋转。
I even tried using ExifInterface
, however, it always gives 6 as rotation.
我正在使用CameraX api寻找解决方案。
I am looking for solution using CameraX apis.
推荐答案
I也有这个问题。解决该问题的方法是使用设备传感器数据获取正确的方向,然后将其设置在我的 imageCapture
对象中。见下面的代码段。
I had this problem also. What solved it is by using the device sensor data to get the correct orientation, then set it in my imageCapture
object. See snippet below.
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)
}
}
这也是G中类似问题的推荐方法oogle问题跟踪工具: https://issuetracker.google.com/issues/144944155
This is also the recommended approach from a similar issue in the Google Issue Tracker: https://issuetracker.google.com/issues/144944155
这篇关于如果应用程序的默认方向设置为纵向模式,我们如何检测使用CameraX捕获的图像的方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!