Browse Source

Fix camera output rotation

master
Hardcore Sushi 2 months ago
parent
commit
e6a1285e0a
Signed by: hardcoresushi
GPG Key ID: AFE384344A45E13A
  1. 25
      app/src/main/java/sushi/hardcore/droidfs/CameraActivity.kt
  2. 13
      app/src/main/java/sushi/hardcore/droidfs/SensorOrientationListener.kt

25
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)
}
}

13
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<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

Loading…
Cancel
Save