如何获得方向的安卓(如北,西)如何获得、方向

2023-09-06 15:16:02 作者:依旧那么甜

我是新的Andr​​oid和我想根据我的相机获得的方向。根据我的相机我怎样才能得到方向信息?你能不能给一个想法呢?

I am new in Android and i want to get direction according to my camera. How can i get direction information according to my camera? Could you give an idea for this?

问候。

推荐答案

我们不能使用方向传感器了,我们就可以利用磁场传感器和加速度传感器串联来获得同样的功能。这是更多的工作,但它允许继续使用一个回调来处理方向改变。

TYPE_ORIENTATION is deprecated

We cannot use the Orientation Sensor anymore, we can use the Magnetic Field Sensor and Accelerometer Sensors in tandem to get equivalent functionality. It's more work but it does allow to continue to use a callback to handle orientation changes.

下面是一个指南针样品: http://www.codingforandroid.com/2011/01/using取向的传感器,simple.html

从的加速度计和磁场的转换来的 AZIMUT 的:

float[] mGravity;
float[] mGeomagnetic;

public void onSensorChanged(SensorEvent event) {

    if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER)
        mGravity = event.values;

    if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD)
        mGeomagnetic = event.values;

    if (mGravity != null && mGeomagnetic != null) {
        float R[] = new float[9];
        float I[] = new float[9];

        if (SensorManager.getRotationMatrix(R, I, mGravity, mGeomagnetic)) {

            // orientation contains azimut, pitch and roll
            float orientation[] = new float[3];
            SensorManager.getOrientation(R, orientation);

            azimut = orientation[0];
        }
    }
}

要指向北方,你可以计算出度的旋转:

To point the north you can calculate a rotation in degrees :

float rotation = -azimut * 360 / (2 * 3.14159f);