检测 Phone 的方向

Detect the Orientation of Phone

我想检测 phone 的方向以从相机旋转帧,然后我的姿势估计可以正确推断出这个旋转的图像。

例如:有人站在我的phone前面,我把我的phone水平放置,然后我想在推理之前将此图像旋转为垂直。因为模型刚好能垂直抓人

我试过这个: var 方向 = resources.configuration.orientation

但这只有在屏幕的自动旋转打开时才有效,我不想要这个。 我没有旋转我的应用程序。

         val orientationEventListener = object : OrientationEventListener(activity) {
            override fun onOrientationChanged(orientation: Int) {
                val defaultPortrait = 0
                val upsideDownPortrait = 180
                val rightLandscape = 90
            val leftLandscape = 270
                when {
                    isWithinOrientationRange(orientation, defaultPortrait) -> {} 
                    isWithinOrientationRange(orientation, leftLandscape) -> {} 
                    isWithinOrientationRange(orientation, upsideDownPortrait) -> {} 
                    isWithinOrientationRange(orientation, rightLandscape) -> {} 
                }
            }

           private fun isWithinOrientationRange(
               currentOrientation: Int, targetOrientation: Int, epsilon: Int = 10
           ): Boolean {
               return currentOrientation > targetOrientation - epsilon
                    && currentOrientation < targetOrientation + epsilon
           }
        }
        orientationEventListener.enable()