android - 检测手机的方向
问题描述
我想检测手机的方向以从相机旋转框架,然后我的姿势估计可以正确推断出这个旋转的图像。
例如:有人站在我的手机前,我将手机水平放置,然后我想在推理之前将此图像旋转为垂直。因为模型正好可以垂直捕捉人。
我试过这个: varorientation = 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()
推荐阅读
- c++ - 在 C++ 中引用多维数组
- python - “更新程序”设计模式,而不是“生成器”
- java - 为什么按钮显示错误的颜色?
- android - 我有一个用 flutter 编写的简单代码。现在我想格式化它。我使用 VSC 并安装了 Prettier
- slick.js - 当前索引幻灯片 slickslider loop false
- sql-server - 如何根据某些列值从 SQL Server 表中查找重复条目?
- sql - 如何将 SQL Server 几何数据插入到 postgis 几何数据类型列
- javascript - 如何在 JavaScript 中显示具有带空格参数的 JSON 中的数据
- flutter - 错误:没有为“问题”类定义吸气剂“长度”
- flutter - 在 integration_test 迁移后无法通过初始屏幕