首页 > 解决方案 > 如果应用程序的默认方向设置为纵向模式,我们如何检测使用 CameraX 捕获的图像的方向

问题描述

基本上,我的相机应用程序设置为人像模式。但是,用户可以通过相应地旋转手机来拍摄肖像或风景照片(应用程序不旋转)。

所以我的问题是,我们如何找到捕获的图像方向?

我尝试使用DisplayManager.DisplayListener,但是,它仅在应用程序的方向发生时才有效。由于我已阻止应用程序的方向为纵向模式,因此这里没有任何回调。

我什至尝试使用ExifInterface,但是,它总是给出 6 作为旋转。

我正在寻找使用 CameraX api 的解决方案。

标签: androidkotlincameraandroid-camerax

解决方案


我也有这个问题。解决它的方法是使用设备传感器数据来获得正确的方向,然后将其设置在我的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


推荐阅读