3

我再次需要你们的帮助,伙计们,

我的课很好CustomSensorEventListener(见下文)

我得到正确的所有数据,如方位角、轮询、俯仰。

我在屏幕上有一个根据传感器方向移动的图像。

然而,传感器太敏感了,我的意思是在任何纳米变化时,我的图像都会轻微移动。

颤抖之类的。如何将传感器级别降低到像 Android 上的相机一样。

如您所知,相机不会因任何传感器变化而颤抖。

谢谢,

public class CustomSensorEventListener implements SensorEventListener {
float[] inR = new float[16];
float[] I = new float[16];
float[] gravity = new float[3];
float[] geomag = new float[3];
float[] orientVals = new float[3];

public static double azimuth = 0;
public static double pitch = 0;
static double roll = 0;

private Display mDisplay;

public CustomSensorEventListener(Display display){
    mDisplay = display;     
}

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
 // TODO Auto-generated method stub

}

@Override
public void onSensorChanged(SensorEvent sensorEvent) {
    // If the sensor data is unreliable return
    if (sensorEvent.accuracy == SensorManager.SENSOR_STATUS_UNRELIABLE)
        return;

    // Gets the value of the sensor that has been changed
    switch (sensorEvent.sensor.getType()) {  
        case Sensor.TYPE_ACCELEROMETER:
            gravity = sensorEvent.values.clone();
            break;
        case Sensor.TYPE_MAGNETIC_FIELD:
            geomag = sensorEvent.values.clone();
            break;
    }

    // If gravity and geomag have values then find rotation matrix
    if (gravity != null && geomag != null) {

        // checks that the rotation matrix is found
        boolean success = SensorManager.getRotationMatrix(inR, I,
                                                          gravity, geomag);

        float[] outR = new float[16];
        if (success) {

            SensorManager.remapCoordinateSystem(inR, SensorManager.AXIS_Y, SensorManager.AXIS_MINUS_Z, outR);

//              Display display = mContext.getWindowManager().getDefaultDisplay(); 
            int deviceRot = mDisplay.getRotation();

            switch (deviceRot)
            {
            // portrait - normal
            case Surface.ROTATION_0: SensorManager.remapCoordinateSystem(inR,
                    SensorManager.AXIS_X, SensorManager.AXIS_Z,
                    outR);
            break;
            // rotated left (landscape - keys to bottom)
            case Surface.ROTATION_90: SensorManager.remapCoordinateSystem(inR,
                    SensorManager.AXIS_Z, SensorManager.AXIS_MINUS_X,
                    outR); 
            break;
            // upside down
            case Surface.ROTATION_180: SensorManager.remapCoordinateSystem(inR,
                    SensorManager.AXIS_X, SensorManager.AXIS_Z,
                    outR); 
            break;
            // rotated right
            case Surface.ROTATION_270: SensorManager.remapCoordinateSystem(inR,
                    SensorManager.AXIS_MINUS_Z, SensorManager.AXIS_X,
                    outR); 
            break;

            default:  break;
            }



            SensorManager.getOrientation(outR, orientVals);
            azimuth = Math.toDegrees(orientVals[0]);
            pitch = Math.toDegrees(orientVals[1]);
            roll = Math.toDegrees(orientVals[2]);
        }

        //Log.v("MA","Az: "+azimuth+", Pi: "+pitch+", Ro: "+roll);
    }
}
};
4

1 回答 1

13

您可以使用低通滤波器过滤掉抖动、振动和突然的快速移动。滤波器将允许较慢的运动,例如根据特定的截止频率改变旋转角度。

更多信息和实现细节:Low-Pass-Filter-To-Android-Sensors

使用低通滤波器平滑传感器数据

更多关于低通滤波器(维基百科):低通滤波器

于 2013-07-28T21:28:17.923 回答