0

I'm experimenting with a camera app that shows you the inclination angle of the phone to help you level the phone.

As shown below, the sensor angle should say 90% when the phone is standing upright:

enter image description here

what I'm trying to do is similar to Google's Camera app which shows you the tilt angle and inclination angle: enter image description here

I have the following implementation that uses Sensor.TYPE_ROTATION_VECTOR, but the problem I have is that the angle changes based on the starting position/angle of the phone. I want a consistent angle measurement that shows 90% when the phone is standing upright.


class MainActivity : Activity(), SensorEventListener {
    private var mSensorManager: SensorManager? = null
    private var mRotationVectorSensor: Sensor? = null

    private val rotationMatrix = FloatArray(9)
    private val orientationAngles = FloatArray(3)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        // Get an instance of the SensorManager
        mSensorManager = getSystemService(SENSOR_SERVICE) as SensorManager
        mRotationVectorSensor = mSensorManager!!.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR)
        mSensorManager!!.registerListener(
            this, mRotationVectorSensor, SensorManager.SENSOR_DELAY_NORMAL
        )
    }

    override fun onSensorChanged(event: SensorEvent) {
        if (event.sensor.type == Sensor.TYPE_ROTATION_VECTOR) {
            // Convert the rotation-vector to a 3x3 matrix.
            SensorManager.getRotationMatrixFromVector(rotationMatrix, event.values)
            SensorManager.getOrientation(rotationMatrix, orientationAngles)

            val inclination = SensorManager.getInclination(rotationMatrix)
            val degree = Math.toDegrees(inclination.toDouble())

            Log.d("++tiltAngleDegrees,", degree.roundToInt().toString())

        }
    }
}

I also tried using remapCoordinateSystem, but that didn't fix the issue:

SensorManager.remapCoordinateSystem(
    rotationMatrix, SensorManager.AXIS_X, SensorManager.AXIS_Z, rotationMatrix
)

Android SensorManager strange how to remapCoordinateSystem is a similar StackOverflow post, but it's about the tilt or orientation angle

leo
  • 113
  • 3
  • 11

0 Answers0