0

打扰一下 。我需要你的帮助 。我想获得 augumentet 现实的方向(s/n/e/w),但它显示错误的结果(来自传感器的错误数据)。我使用横向。这是我的代码(在 loadnewsensordata 中,我从磁场传感器和加速度计传感器加载数据)-

    private float[] calculateOrientation() {

        float[] values = new float[3];
        float[] I = new float[16];
        values[0] = 0;
        values[1] = 0;
        values[2] = 0;
        float[] R = new float[16];
        float[] outR = new float[16];
        for (int i = 0; i < R.length; i++) {
            R[i] = 0;
            outR[i] = 0;
        }

        Display display = parrent.getWindowManager().getDefaultDisplay();
        int deviceRot = display.getRotation();


        SensorManager.getRotationMatrix(R, null, accelData, magnetData);
        switch (deviceRot) {
            case Surface.ROTATION_0:
                SensorManager.remapCoordinateSystem(R,
                        SensorManager.AXIS_X, SensorManager.AXIS_Z,
                        outR);
                break;
//                 rotated left (landscape - keys to bottom)
            case Surface.ROTATION_90:
                SensorManager.remapCoordinateSystem(R,
                        SensorManager.AXIS_Y, SensorManager.AXIS_MINUS_X,
                        outR);
                break;
            // upside down
            case Surface.ROTATION_180:
                SensorManager.remapCoordinateSystem(R,
                        SensorManager.AXIS_X, SensorManager.AXIS_Z,
                        outR);
                break;
            // rotated right
            case Surface.ROTATION_270:
                SensorManager.remapCoordinateSystem(R,
                        SensorManager.AXIS_MINUS_Z, SensorManager.AXIS_X,
                        outR);
                break;

            default:
                break;
        }
        SensorManager.getOrientation(outR, values);

        // Convert from Radians to Degrees.
        values[0] = (float) Math.toDegrees(values[0]);
        values[1] = (float) Math.toDegrees(values[1]);
        values[2] = (float) Math.toDegrees(values[2]);
        if (values[0] < 0) {
            values[0] = 360 + values[0];
        }
        return values;
    }

    public void onSensorChanged(SensorEvent event) {

        loadNewSensorData(event);
        tw1 = (int) calculateOrientation()[0] + " ";
        parrent.viewBearingOnDisplay(tw1);
    }


private void loadNewSensorData(SensorEvent event) {
    final int type = event.sensor.getType(); //Определяем тип датчика

    if (type == Sensor.TYPE_ACCELEROMETER) { //Если акселерометр
        accelData = event.values.clone();
    }

    if (type == Sensor.TYPE_MAGNETIC_FIELD) { //Если геомагнитный датчик
        magnetData = event.values.clone();
    }

}
4

1 回答 1

0

您不需要有 switch 语句

SensorManager.getRotationMatrix(R, null, accelData, magnetData);
SensorManager.remapCoordinateSystem(R, SensorManager.AXIS_X, SensorManager.AXIS_Z, outR);
SensorManager.getOrientation(outR, values);

上面的代码将为您values[0]提供后置摄像头指向的方向,与您的设备方向无关。

于 2013-06-14T00:33:18.663 回答