Fix camera output rotation

This commit is contained in:
Matéo Duparc 2022-04-16 13:58:58 +02:00
parent ab48f9219b
commit e6a1285e0a
Signed by untrusted user: hardcoresushi
GPG Key ID: AFE384344A45E13A
2 changed files with 20 additions and 18 deletions

View File

@ -10,9 +10,7 @@ import android.os.Build
import android.os.Bundle
import android.text.InputType
import android.util.Size
import android.view.MotionEvent
import android.view.ScaleGestureDetector
import android.view.View
import android.view.*
import android.view.animation.Animation
import android.view.animation.LinearInterpolator
import android.view.animation.RotateAnimation
@ -511,21 +509,24 @@ class CameraActivity : BaseActivity(), SensorOrientationListener.Listener {
}
override fun onOrientationChange(newOrientation: Int) {
val reversedOrientation = when (newOrientation){
90 -> 270
270 -> 90
else -> newOrientation
}.toFloat()
val realOrientation = when (newOrientation) {
Surface.ROTATION_0 -> 0f
Surface.ROTATION_90 -> 90f
Surface.ROTATION_180 -> 180f
else -> 270f
}
val rotateAnimation = RotateAnimation(previousOrientation, when {
reversedOrientation - previousOrientation > 180 -> reversedOrientation - 360
reversedOrientation - previousOrientation < -180 -> reversedOrientation + 360
else -> reversedOrientation
realOrientation - previousOrientation > 180 -> realOrientation - 360
realOrientation - previousOrientation < -180 -> realOrientation + 360
else -> realOrientation
}, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f)
rotateAnimation.duration = 300
rotateAnimation.interpolator = LinearInterpolator()
rotateAnimation.fillAfter = true
orientedIcons.map { it.startAnimation(rotateAnimation) }
previousOrientation = reversedOrientation
previousOrientation = realOrientation
imageCapture?.targetRotation = newOrientation
videoCapture?.setTargetRotation(newOrientation)
}
}

View File

@ -5,6 +5,7 @@ import android.hardware.Sensor
import android.hardware.SensorEvent
import android.hardware.SensorEventListener
import android.hardware.SensorManager
import android.view.Surface
import java.lang.ref.WeakReference
import java.util.*
@ -15,8 +16,8 @@ class SensorOrientationListener(context: Context) {
mSensorEventListener = NotifierSensorEventListener()
mSensorManager = context.getSystemService(Context.SENSOR_SERVICE) as SensorManager
}
private val mListeners = ArrayList<WeakReference<Listener?>>(3)
private var orientation = 0
private val mListeners = ArrayList<WeakReference<Listener?>>(1)
private var orientation = Surface.ROTATION_0
private fun onResume() {
mSensorManager.registerListener(
@ -35,10 +36,10 @@ class SensorOrientationListener(context: Context) {
val x = event.values[0]
val y = event.values[1]
var newOrientation: Int = orientation
if (x < 5 && x > -5 && y > 5) newOrientation = 0
else if (x < -5 && y < 5 && y > -5) newOrientation = 90
else if (x < 5 && x > -5 && y < -5) newOrientation = 180
else if (x > 5 && y < 5 && y > -5) newOrientation = 270
if (x < 5 && x > -5 && y > 5) newOrientation = Surface.ROTATION_0
else if (x < -5 && y < 5 && y > -5) newOrientation = Surface.ROTATION_270
else if (x < 5 && x > -5 && y < -5) newOrientation = Surface.ROTATION_180
else if (x > 5 && y < 5 && y > -5) newOrientation = Surface.ROTATION_90
if (orientation != newOrientation) {
orientation = newOrientation