Android旋转矩阵传感器获取旋转角度失败

Posted

技术标签:

【中文标题】Android旋转矩阵传感器获取旋转角度失败【英文标题】:Android rotation matrix sensor to get rotation angles failed 【发布时间】:2014-05-28 06:56:53 【问题描述】:

我目前正在研究有关测量我的 android 设备的方向角度的运动传感器。在实现方面,无论我们如何转动,设备都会归零。是否有任何条件来初始化浮点数组以便可以计算角度?

下面是我的代码

public class MainActivity extends Activity  implements SensorEventListener 

    private SensorManager sensorManager;

    TextView xCoor; // declare X axis object
    TextView yCoor; // declare Y axis object
    TextView zCoor; // declare Z axis object
    float[] mRotationMatrixFromVector =  new float[3];
    float[] mRotationMatrix =  new float[9];
    float[] orientationVals =  new float[3];

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        xCoor=(TextView)findViewById(R.id.xcoor); // create X axis object
        yCoor=(TextView)findViewById(R.id.ycoor); // create Y axis object
        zCoor=(TextView)findViewById(R.id.zcoor); // create Z axis object

        sensorManager=(SensorManager)getSystemService(SENSOR_SERVICE);
        // add listener. The listener will be HelloAndroid (this) class
        sensorManager.registerListener(this, 
                sensorManager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR),
                SensorManager.SENSOR_DELAY_NORMAL);

    

    @Override
    public void onSensorChanged(SensorEvent event) 
        // TODO Auto-generated method stub
        // check sensor type
        if(event.sensor.getType()==Sensor.TYPE_ROTATION_VECTOR)


            SensorManager.getRotationMatrixFromVector(mRotationMatrixFromVector, event.values);

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

            SensorManager.getOrientation(mRotationMatrix, orientationVals);

            // Optionally convert the result from radians to degrees
            orientationVals[0] = (float) Math.toDegrees(orientationVals[0]);
            orientationVals[1] = (float) Math.toDegrees(orientationVals[1]);
            orientationVals[2] = (float) Math.toDegrees(orientationVals[2]);


            xCoor.setText("X: "+ String.valueOf(orientationVals[0]));
            yCoor.setText("Y: "+ String.valueOf(orientationVals[1]));
            zCoor.setText("Z: "+ String.valueOf(orientationVals[2]));
        
    

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) 
        // TODO Auto-generated method stub

    

【问题讨论】:

【参考方案1】:

来自docs:mRotationMatrixFromVector 的长度必须为 9 或 16。

【讨论】:

以上是关于Android旋转矩阵传感器获取旋转角度失败的主要内容,如果未能解决你的问题,请参考以下文章

Android Camera旋转角度

从 OpenCV 中的旋转矩阵和平移向量中获取旋转轴

如何从旋转矩阵中获取用户视点?

iphone sdk CGAffineTransform 获取对象的旋转角度

JAVA android 矩阵 怎么设置坐标

JS获取transform rotate旋转角度的度数,matrix解析