Androidでカメラの方向を取得しようとしています。ポートレートでは完全に機能するコードがあります (ゆっくりと円を描いて 1 秒間隔で更新を確認してテストします) が、ランドスケープではまったく機能しません。数字がランダムに変化するようです。また、ポートレートからランドスケープに切り替えた後も、まったくおかしくなりません。これが私のコードです
public void onSensorChanged(SensorEvent event) {
switch (event.sensor.getType()) {
case Sensor.TYPE_ACCELEROMETER:
accelerometerValues = event.values.clone();
break;
case Sensor.TYPE_MAGNETIC_FIELD:
geomagneticMatrix = event.values.clone();
break;
default:
break;
}
if (geomagneticMatrix != null && accelerometerValues != null) {
float[] R = new float[16];
float[] I = new float[16];
float[] outR = new float[16];
//Get the rotation matrix, then remap it from camera surface to world coordinates
SensorManager.getRotationMatrix(R, I, accelerometerValues, geomagneticMatrix);
SensorManager.remapCoordinateSystem(R, SensorManager.AXIS_X, SensorManager.AXIS_Z, outR);
float values[] = new float[4];
SensorManager.getOrientation(outR,values);
float direction = normalizeDegrees((float) Math.toDegrees(values[0]));
float pitch = normalizeDegrees((float) Math.toDegrees(values[1]));
float roll = normalizeDegrees((float) Math.toDegrees(values[2]));
if((int)direction != (int)lastDirection){
lastDirection = direction;
for(CompassListener listener: listeners){
listener.onDirectionChanged(lastDirection, pitch, roll);
}
}
}
}
私が間違っていることはありますか?私はこれを 100% 理解しているわけではないことを率直に認めます。また、Google が方向センサーを非推奨にした理由もわかりません。それは十分に一般的な欲求のようです。