更高级别的API传感器值俯仰和滚动

时间:2012-04-28 22:40:53

标签: android sensor device-orientation pitch

我正在实施自己的相机活动。 要旋转捕获的图像,我需要知道拍摄时的方向。 传感器值俯仰和滚动是否有更高级别的API,告诉我我的设备的方向是:

自上而下 - 手机正常,肖像
自底向上
左右 - 手机的顶部是右侧的横向 左右 - 手机顶部位于左侧

或者还有其他方法可以直接从系统中找到它吗?

2 个答案:

答案 0 :(得分:1)

遗憾的是,当摄影机处于横向模式时(这是Android上摄像头的“自然”方向),摄像机在Android上以奇怪的方式工作。

最好的办法是将活动设置为横向模式,并添加onConfigurationChanged事件(并将android:configChanges =“orientation”添加到清单中)以获取当前方向。

捕获图像时,检查方向并根据您希望的逻辑进行操作。

答案 1 :(得分:1)

Ok解决了我的问题到某一点,这样它对我有用,而且我忽略了这个问题。

public class DeviceOrientation {

public static final int ORIENTATION_PORTRAIT = 0;
public static final int ORIENTATION_LANDSCAPE_REVERSE = 1;
public static final int ORIENTATION_LANDSCAPE = 2;
public static final int ORIENTATION_PORTRAIT_REVERSE = 3;

int smoothness = 1;
public float averagePitch = 0;
public float averageRoll = 0;
public int orientation = ORIENTATION_PORTRAIT;

private float[] pitches;
private float[] rolls;

public DeviceOrientation(int smoothness) {
    this.smoothness = smoothness;

    pitches = new float[smoothness];
    rolls = new float[smoothness];
}

public void addSensorEvent(SensorEvent event) {
    azimuth = event.values[0];

    averagePitch = addValue(event.values[1], pitches);
    averageRoll = addValue(event.values[2], rolls);

    orientation = calculateOrientation();
}

private float addValue(float value, float[] values) {
    float average = 0;

    for(int i=1; i<smoothness; i++) {
        values[i-1] = values[i];
        average += values[i];
    }
    values[smoothness-1] = value;
    average = (average + value)/smoothness;

    return average;
}

/** handles all 4 possible positions perfectly */
private int calculateOrientation() {
    // finding local orientation dip
    if (((orientation == ORIENTATION_PORTRAIT || orientation == ORIENTATION_PORTRAIT_REVERSE)
            && (averageRoll > -30 && averageRoll < 30))) {
        if (averagePitch > 0)
            return ORIENTATION_PORTRAIT_REVERSE;
        else
            return ORIENTATION_PORTRAIT;
    } else {
        // divides between all orientations
        if (Math.abs(averagePitch) >= 30) {
            if (averagePitch > 0)
                return ORIENTATION_PORTRAIT_REVERSE;
            else
                return ORIENTATION_PORTRAIT;
        } else {
                if (averageRoll > 0) {
                    return ORIENTATION_LANDSCAPE_REVERSE;
                } else {
                    return ORIENTATION_LANDSCAPE;
                }
        }
    }
}

说明: 如果我处于纵向模式并且直到它向前移动直到它处于水平位置,它将由于其余代码而切换到横向。 因此,我检查它是否是纵向的,并使条件难以保持这种模式。 这就是我对当地的倾向。 其余的只分为三个方向。

有一件事情很糟糕。如果设备在landscape_x中并向后倾斜一定度数,那么pich会从~2跳到~175。那时我的代码在横向和纵向之间进行了翻转。

平滑度将通过组合最后n个值并计算平均值来平滑传感器数据的值。这不是必要的。

我希望这会对别人有所帮助。如果您可以进一步改进代码,请告诉我。