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.os.Bundle
import android.text.InputType import android.text.InputType
import android.util.Size import android.util.Size
import android.view.MotionEvent import android.view.*
import android.view.ScaleGestureDetector
import android.view.View
import android.view.animation.Animation import android.view.animation.Animation
import android.view.animation.LinearInterpolator import android.view.animation.LinearInterpolator
import android.view.animation.RotateAnimation import android.view.animation.RotateAnimation
@ -511,21 +509,24 @@ class CameraActivity : BaseActivity(), SensorOrientationListener.Listener {
} }
override fun onOrientationChange(newOrientation: Int) { override fun onOrientationChange(newOrientation: Int) {
val reversedOrientation = when (newOrientation){ val realOrientation = when (newOrientation) {
90 -> 270 Surface.ROTATION_0 -> 0f
270 -> 90 Surface.ROTATION_90 -> 90f
else -> newOrientation Surface.ROTATION_180 -> 180f
}.toFloat() else -> 270f
}
val rotateAnimation = RotateAnimation(previousOrientation, when { val rotateAnimation = RotateAnimation(previousOrientation, when {
reversedOrientation - previousOrientation > 180 -> reversedOrientation - 360 realOrientation - previousOrientation > 180 -> realOrientation - 360
reversedOrientation - previousOrientation < -180 -> reversedOrientation + 360 realOrientation - previousOrientation < -180 -> realOrientation + 360
else -> reversedOrientation else -> realOrientation
}, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f) }, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f)
rotateAnimation.duration = 300 rotateAnimation.duration = 300
rotateAnimation.interpolator = LinearInterpolator() rotateAnimation.interpolator = LinearInterpolator()
rotateAnimation.fillAfter = true rotateAnimation.fillAfter = true
orientedIcons.map { it.startAnimation(rotateAnimation) } 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.SensorEvent
import android.hardware.SensorEventListener import android.hardware.SensorEventListener
import android.hardware.SensorManager import android.hardware.SensorManager
import android.view.Surface
import java.lang.ref.WeakReference import java.lang.ref.WeakReference
import java.util.* import java.util.*
@ -15,8 +16,8 @@ class SensorOrientationListener(context: Context) {
mSensorEventListener = NotifierSensorEventListener() mSensorEventListener = NotifierSensorEventListener()
mSensorManager = context.getSystemService(Context.SENSOR_SERVICE) as SensorManager mSensorManager = context.getSystemService(Context.SENSOR_SERVICE) as SensorManager
} }
private val mListeners = ArrayList<WeakReference<Listener?>>(3) private val mListeners = ArrayList<WeakReference<Listener?>>(1)
private var orientation = 0 private var orientation = Surface.ROTATION_0
private fun onResume() { private fun onResume() {
mSensorManager.registerListener( mSensorManager.registerListener(
@ -35,10 +36,10 @@ class SensorOrientationListener(context: Context) {
val x = event.values[0] val x = event.values[0]
val y = event.values[1] val y = event.values[1]
var newOrientation: Int = orientation var newOrientation: Int = orientation
if (x < 5 && x > -5 && y > 5) newOrientation = 0 if (x < 5 && x > -5 && y > 5) newOrientation = Surface.ROTATION_0
else if (x < -5 && y < 5 && y > -5) newOrientation = 90 else if (x < -5 && y < 5 && y > -5) newOrientation = Surface.ROTATION_270
else if (x < 5 && x > -5 && y < -5) newOrientation = 180 else if (x < 5 && x > -5 && y < -5) newOrientation = Surface.ROTATION_180
else if (x > 5 && y < 5 && y > -5) newOrientation = 270 else if (x > 5 && y < 5 && y > -5) newOrientation = Surface.ROTATION_90
if (orientation != newOrientation) { if (orientation != newOrientation) {
orientation = newOrientation orientation = newOrientation