QtSensors / QML设备旋转读数不是唯一的吗?

时间:2018-01-30 01:02:47

标签: qt qml

我正在尝试阅读设备的音高。对于我的理想用途,我试图阅读的方向是后置摄像头的方向。 Qt Sensors和Rotation元素是我如何做到这一点(有倾斜传感器,但也是如此)

我编制了一张桌子,将顶边从几乎平坦的地方抬起,面朝上翻转,直到面朝下(旋转,设备方向):

rot|ori| visual pitch
13 | 5 | _
32 | 5 | _
50 | 1 | \
69 | 1 | \
78 | 1 | \ <-+
90 | 1 | |   +-problem 
74 | 1 | / <-+
62 | 1 | /
47 | 1 | /
29 | 6 | _
19 | 6 | _
12 | 6 | _
 8 | 6 | _

即使翻转动作从未改变方向,但值为78,90,74。由于设备方向从未改变,我无法区分。旋转90度的正确方法是什么?我期待135,140等等的价值(iOS,Android)

import QtQuick 2.7
import QtQuick.Controls 2.0
import QtQuick.Layouts 1.0
import QtSensors 5.3

ApplicationWindow {
    visible: true
    width: 640
    height: 480

    RotationSensor {
        id: rotationSensor
        active: true
    }
    OrientationSensor {
        id: orientation
        active: true
    }

    MouseArea {
        anchors.fill: parent
        onClicked: {
            console.log("rotation.x, orientation:", rotationSensor.reading.x, orientation.reading.orientation)
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您还需要考虑绕其他轴的旋转。除非需要z旋转,否则我更喜欢使用TiltSensor(仅x和y轴)代替RotationSensor。

在这里我如何计算图片的倾斜度:

function calculateVerticalAngle(or,xr,yr){ //or-orientation xr-xRotation yr-yRotation
    var verticalAngle;
    verticalAngle = xr
    if ( or === 0) {
        verticalAngle=0
    }
    if ( or === 1) {
        //"The Top edge of the device was pointing up. (estimated)"
        verticalAngle-=90
    }
    if ( or === 2) {
        //"The Top edge of the device was pointing down. (estimated)"
        verticalAngle = -90-verticalAngle
    }
    if ( or === 3) {
        //"The Left edge of the device was pointing up. (estimated)"
        if(xr<90 && xr > -90) verticalAngle = yr-90
        else verticalAngle = 90-yr
    }
    if ( or === 4) {
        //"The Right edge of the device was pointing up. (estimated)"
        if(xr<90 && xr >-90) verticalAngle = -yr-90
        else verticalAngle = 90+yr
    }
    if ( or === 5) {
        //"The Face of the device was pointing up. (estimated)"
        verticalAngle =0
    }
    if ( or === 6) {
        //"The Face of the device was pointing down. (estimated)"
        verticalAngle =0
    }
    return verticalAngle
}

垂直角零表示完全垂直保持,负值表示手机向下倾斜,正值表示向上。

如果旋转手机,则根据y轴计算倾斜度。

希望我以一种可以理解的方式编写了它