From e6a1285e0ad8c653454acb28c2417d8cd0b6990a Mon Sep 17 00:00:00 2001 From: Hardcore Sushi Date: Sat, 16 Apr 2022 13:58:58 +0200 Subject: [PATCH] Fix camera output rotation --- .../sushi/hardcore/droidfs/CameraActivity.kt | 25 ++++++++++--------- .../droidfs/SensorOrientationListener.kt | 13 +++++----- 2 files changed, 20 insertions(+), 18 deletions(-) diff --git a/app/src/main/java/sushi/hardcore/droidfs/CameraActivity.kt b/app/src/main/java/sushi/hardcore/droidfs/CameraActivity.kt index e646c2b..77e83a0 100644 --- a/app/src/main/java/sushi/hardcore/droidfs/CameraActivity.kt +++ b/app/src/main/java/sushi/hardcore/droidfs/CameraActivity.kt @@ -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) } } diff --git a/app/src/main/java/sushi/hardcore/droidfs/SensorOrientationListener.kt b/app/src/main/java/sushi/hardcore/droidfs/SensorOrientationListener.kt index 16b3cc6..6854fa3 100644 --- a/app/src/main/java/sushi/hardcore/droidfs/SensorOrientationListener.kt +++ b/app/src/main/java/sushi/hardcore/droidfs/SensorOrientationListener.kt @@ -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>(3) - private var orientation = 0 + private val mListeners = ArrayList>(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