本文介绍了如何获得手机走向增强现实?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能的复制:Android当手机倾斜 getOrientation方位被污染

Possible duplicate : Android getOrientation Azimuth gets polluted when phone is tilted

我是新android开发,我试图让面对用户。

I am new to android development and I try to get the user facing.

在网络上搜索后我pretty知道我需要使用 remapCoordinateSystem ,Android文档说,这是很好的使用增强现实应用程序。

After searching on the web I am pretty sure I need to use remapCoordinateSystem, android documentation say it is good to use for an augmented reality app.

我尝试使用一些现有的code,以确保这是一个很好的方式。

I try to use some existing code to be sure that is a good way.

(最后,我想创建一个增强现实应用程序)

(Finally I want to create an augmented reality application)

以下code应该返回度面对用户:

The following code should return the user facing in degree :

        Sensor sensor = event.sensor;
        if (sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
            gravity = event.values.clone();
        }else if (sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
            geomagnetic = event.values.clone();
        }

        if (gravity != null && geomagnetic != null){
            _rM = SensorManager.getRotationMatrix (R, null, gravity, geomagnetic);
                if(_rM){

                    int mScreenRotation = activity.getWindowManager().getDefaultDisplay().getRotation(); // get user orientation mode
                    switch (mScreenRotation) { // Handle device rotation (landscape, portrait)
                        case Surface.ROTATION_0:
                            axisX = SensorManager.AXIS_X;
                            axisY = SensorManager.AXIS_Y;
                            break;

                        case Surface.ROTATION_90:
                            axisX = SensorManager.AXIS_Y;
                            axisY = SensorManager.AXIS_MINUS_X;
                            break;

                        case Surface.ROTATION_180: // not handled by my phone so I can't test
                            axisX = SensorManager.AXIS_MINUS_X;
                            axisY = SensorManager.AXIS_MINUS_Y;
                            break;

                        case Surface.ROTATION_270:
                            axisX = SensorManager.AXIS_MINUS_Y;
                            axisY = SensorManager.AXIS_X;
                            break;

                        default:
                            break;
                    }
                    SensorManager.remapCoordinateSystem (R, axisX, axisY, outR);
                    SensorManager.getOrientation (outR, gO);
            }
        }


        userFacing = (float) Math.toDegrees(gO[0]); // Radian to degree
        if(userFacing<0) { userFacing+=360; } // -180;180 to 0;360
        return userFacing;

其实我能得到一个准确的指南针当手机朝下,但是当我尝试移动电话(作为一个用户会做),结果是非常不准确的:当我倾斜装置,方向可移动40°...

Actually I am able to get an accurate compass when the phone is facing down, but when I try to move the phone (as a user would do), the results are very inaccurate : when i tilt the device, direction can move of 40°...

我看到此链接:http://stackoverflow.com/questions/17979238/android-getorientation-azimuth-gets-polluted-when-phone-is-tilted

但是,如果我留倾斜,结果是不准确的再次(这是一个平均水平,所以这是正常的!)
我需要指南针随时随地工作...

But if i stay tilted, the results are inaccurate again (it's an average, so it's normal!)And i need the compass working anytime...

我想我会使用 TYPE_GYROSCOPE ,但不是所有的设备都有一个陀螺仪时下,所以我需要的所有其他设备另一种解决方案!

I think I will use TYPE_GYROSCOPE, but not all devices have a gyroscope nowadays so i need another solution for all the other devices !

希望你能理解我的问题,我的英语不好对不起! (我是法国人)

Hope you can understand my problem, and sorry for my bad english! (I'm French)

好了,所以适应iOS上的应用程序之后,我决定进去看看android的方法是一样好,我虽然。一个月前,我贴的解决方案,这是真的错了。我做了关于 remapCoordinateSystem 一个巨大的错误:我用 AXIS_X AXIS_Y 让我矩阵的计算,我试图纠正不同的价值观我的价值观。作为@HoanNguyen建议,我们只需要使用 AXIS_X AXIS_Z 和android处理剩下的。

Ok so after adapting the application on iOS, I decided to check if the android methods were as good as I though. A month ago, I posted a solution and it was really wrong. I made a huge mistake on remapCoordinateSystem : I used AXIS_X and AXIS_Y to get my matrice and I tried to correct my values with different values. As @HoanNguyen suggested, we just have to use AXIS_X and AXIS_Zand android handle the rest.

因此​​,这里是最后的code。很多更短,更容易:

So here is the final code. Lot more shorter and easier:

    if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
        gravity = lowPass(event.values.clone(), gravity);
    }
    else if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
        geomagnetic = lowPass(event.values.clone(), geomagnetic);
    }

    if (geomagnetic != null && gravity != null) {
        if (SensorManager.getRotationMatrix (R_in, null, gravity, geomagnetic)) {
            SensorManager.remapCoordinateSystem (R_in, SensorManager.AXIS_X, SensorManager.AXIS_Z, R_out);
            SensorManager.getOrientation (R_out, gO);
        }
    }

和得到磁北,你只需要使用 GO [0] 标题= GO [0]; (注意,返回的值是弧度)

And to get magnetic north, you just have to use gO[0] like heading = gO[0]; (Caution, the returned values are in radians)

希望它可以在这里有人!

推荐答案

您都code,你找到了解决办法是通过手机面临着你是指设备z坐标的相反方向错的,如果。增强现实,你只需要调用

Both your code and the solution you found are wrong if by "phone facing" you means the opposite direction of the device z-coordinate. For augmented reality, you just call

remapCoordinateSystem(inR, AXIS_X, AXIS_Z, outR);

设备方向的独立。然后在调用getOrientation()中的方位给出关于磁北面向电话的方向。

independent of the device orientation. Then the azimuth in the call to getOrientation() gives the direction of the phone facing with respect to Magnetic North.

这2个呼叫等于器件z坐标到世界的XY平面的投影座标,然后计算所得到的矢量的方向。

These 2 calls amount to projection of the device z-coordinate to the XY plane of the world coordinates and then calculate the direction of the resulting vector.

这篇关于如何获得手机走向增强现实?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 21:22