android - 在 Android 应用程序中重新映射坐标系

标签 android android-sensors coordinate-transformation sensormanager

我正在实现一个简单的 Android 应用程序,我需要在其中识别北方。

所以我已经实现了 SensorEventListener 并且我使用了这样的东西:

@Override
public void onSensorChanged(SensorEvent event) {
    if(event.sensor.getType() == Sensor.TYPE_ROTATION_VECTOR)
    {
        SensorManager.getRotationMatrixFromVector(mRotationMatrix, event.values);
        SensorManager.remapCoordinateSystem(mRotationMatrix, SensorManager.AXIS_X, SensorManager.AXIS_Z, orientationVals);
        SensorManager.getOrientation(mRotationMatrix, orientationVals);

        orientationVals[0] = (float) Math.toDegrees(orientationVals[0]);
        orientationVals[1] = (float) Math.toDegrees(orientationVals[1]);
        orientationVals[2] = (float) Math.toDegrees(orientationVals[2]);

        tv.setText(" Yaw: "+ orientationVals[0] +"\n Pitch: "+ orientationVals[1]+"\n Roll: "+ orientationVals[2]);
    }

}

问题是代码行

SensorManager.remapCoordinateSystem(mRotationMatrix, SensorManager.AXIS_X, SensorManager.AXIS_Z, orientationVals);

似乎不起作用,因为在这两种情况下(有或没有此代码行)Yaw(方位角)的值取决于手机头部的方向(让它放在背面)

我预期,使用我的重新映射,偏航会根据手机背面(后置摄像头)的方向发生变化。

为什么我的重映射不起作用?

最佳答案

不确定你是否解决了这个问题,但是你的台词:

SensorManager.remapCoordinateSystem(mRotationMatrix, SensorManager.AXIS_X, SensorManager.AXIS_Z, orientationVals);

重新映射 mRotationMatrix 中的值,并将结果放入 orientationVals。这是正确的(除了重新映射的坐标数组的混淆变量名称之外)。

但是,您可以在调用之后直接覆盖此重新映射:

SensorManager.getOrientation(mRotationMatrix, orientationVals);

这会获取 mRotationMatrix 中的值并根据这些(旧的、未重新映射的)值计算方向,并将其存储在 orientationVals 中。

因此您上面的代码只是获取 mRotationMatrix 的方向,其轴尚未重新映射,这可能解释了您的问题。

像下面这样的东西可能解决你的问题:

float[] remapCoords = new float[16];
SensorManager.remapCoordinateSystem(mRotationMatrix, SensorManager.AXIS_X, SensorManager.AXIS_Z, remapCoords);
SensorManager.getOrientation(remapCoords, orientationVals);

假设您的轴重新映射是您真正想要/正确的,但这是另一个问题。

我应该注意到 article cited ,虽然最优秀,但没有明确提到必须在计算旋转矩阵时每次调用重新映射坐标系,而不仅仅是在显示更改(即 onCreate、onSurfaceChanged 等)时,这可能对某些人来说是显而易见的,但对其他人而言则不然。

关于android - 在 Android 应用程序中重新映射坐标系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20564735/

相关文章:

android - 抽屉导航重叠在 ActionBar 导航选项卡内容上

android - Android 上的 HTML5 <video> 元素无法播放

android - 在选择器不起作用的情况下更改按下时 TextView 的颜色

android - Google Nexus 7 上的重要运动传感器

c# - Lambert 72 转换为纬度/经度对

opengl - 使用opengl围绕枢轴原点旋转

java - 理解 Java 代码背后的数学 : 2D Binary Space Partitioning

Android Sleep API - 未获取数据

android指南针似乎不可靠

android - 使用 Flutter 从 Wear OS 可穿戴设备读取传感器数据