4

我正在實現一個簡單的Android應用程序,在該應用程序中我需要識別北部。在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。

爲什麼我的重新映射不起作用?

回答

0

我用這個:

/* Compensate device orientation */ 
      float[] remappedRotationMatrix = new float[9]; 
      switch (getWindowManager().getDefaultDisplay() 
        .getRotation()) { 
      case Surface.ROTATION_0: 
       SensorManager.remapCoordinateSystem(rotationMatrix, 
         SensorManager.AXIS_X, SensorManager.AXIS_Y, 
         remappedRotationMatrix); 
       break; 
      case Surface.ROTATION_90: 
       SensorManager.remapCoordinateSystem(rotationMatrix, 
         SensorManager.AXIS_Y, 
         SensorManager.AXIS_MINUS_X, 
         remappedRotationMatrix); 
       break; 
      case Surface.ROTATION_180: 
       SensorManager.remapCoordinateSystem(rotationMatrix, 
         SensorManager.AXIS_MINUS_X, 
         SensorManager.AXIS_MINUS_Y, 
         remappedRotationMatrix); 
       break; 
      case Surface.ROTATION_270: 
       SensorManager.remapCoordinateSystem(rotationMatrix, 
         SensorManager.AXIS_MINUS_Y, 
         SensorManager.AXIS_X, remappedRotationMatrix); 
       break; 
      } 

      /* Calculate Orientation */ 
      float results[] = new float[3]; 
      SensorManager.getOrientation(remappedRotationMatrix, 
        results); 

請告訴我,如果它的作品,因爲我仍然有與解決的問題。這是我自己的代碼here。我是根據this official post編寫的。

2

不知道,如果你解決了這個,但你行:

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,等等),這對某些人可能是顯而易見的,但對其他人不是。