diff --git a/cameraViewEx/src/main/api14/com/priyankvasa/android/cameraviewex/Camera1.kt b/cameraViewEx/src/main/api14/com/priyankvasa/android/cameraviewex/Camera1.kt index bc9910e4..3ef5042d 100644 --- a/cameraViewEx/src/main/api14/com/priyankvasa/android/cameraviewex/Camera1.kt +++ b/cameraViewEx/src/main/api14/com/priyankvasa/android/cameraviewex/Camera1.kt @@ -22,13 +22,17 @@ package com.priyankvasa.android.cameraviewex import android.arch.lifecycle.Lifecycle import android.arch.lifecycle.LifecycleRegistry +import android.content.Context +import android.content.Context.WINDOW_SERVICE import android.graphics.ImageFormat import android.hardware.Camera import android.os.Build import android.os.SystemClock import android.support.v4.util.SparseArrayCompat import android.util.SparseIntArray +import android.view.Surface import android.view.SurfaceHolder +import android.view.WindowManager import com.priyankvasa.android.cameraviewex.exif.ExifInterface import kotlinx.coroutines.CoroutineExceptionHandler import kotlinx.coroutines.Dispatchers @@ -49,7 +53,8 @@ internal class Camera1( private val listener: CameraInterface.Listener, private val preview: PreviewImpl, private val config: CameraConfiguration, - private val cameraJob: Job + private val cameraJob: Job, + private val context: Context ) : CameraInterface { override val coroutineContext: CoroutineContext get() = Dispatchers.Default + cameraJob @@ -542,7 +547,7 @@ internal class Camera1( parameters.supportedPictureSizes ?.forEach { pictureSizes.add(it.width, it.height) } - setDisplayOrientation(calcDisplayOrientation(deviceRotation)) + setDisplayOrientation(calcDisplayOrientation()) } adjustCameraParameters() listener.onCameraOpened() @@ -596,15 +601,28 @@ internal class Camera1( * * Note: This is not the same calculation as the camera rotation * - * @param rotationDegrees Screen orientation in degrees * @return Number of degrees required to rotate preview */ - private fun calcDisplayOrientation(rotationDegrees: Int): Int = + private fun calcDisplayOrientation(): Int { + var cameraDegrees = getCameraDegrees() if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) { - (360 - ((cameraInfo.orientation + rotationDegrees) % 360)) % 360 + return (360 - ((cameraInfo.orientation + cameraDegrees) % 360)) % 360 } else { // back-facing - (cameraInfo.orientation - rotationDegrees + 360) % 360 + return (cameraInfo.orientation - cameraDegrees + 360) % 360 } + } + + private fun getCameraDegrees(): Int { + val rotation = (context.getSystemService(WINDOW_SERVICE) as WindowManager).defaultDisplay.rotation + var degrees = 0 + when (rotation) { + Surface.ROTATION_0 -> degrees = 0 + Surface.ROTATION_90 -> degrees = 90 + Surface.ROTATION_180 -> degrees = 180 + Surface.ROTATION_270 -> degrees = 270 + } + return degrees + } /** * Calculate camera rotation diff --git a/cameraViewEx/src/main/api21/com/priyankvasa/android/cameraviewex/Camera2.kt b/cameraViewEx/src/main/api21/com/priyankvasa/android/cameraviewex/Camera2.kt index b13522dd..796adac5 100644 --- a/cameraViewEx/src/main/api21/com/priyankvasa/android/cameraviewex/Camera2.kt +++ b/cameraViewEx/src/main/api21/com/priyankvasa/android/cameraviewex/Camera2.kt @@ -1115,9 +1115,10 @@ internal open class Camera2( prepareContinuousFrameReader() - val template: Int = - if (config.zsl.value) CameraDevice.TEMPLATE_ZERO_SHUTTER_LAG - else CameraDevice.TEMPLATE_PREVIEW +// val template: Int = +// if (config.zsl.value) CameraDevice.TEMPLATE_ZERO_SHUTTER_LAG +// else CameraDevice.TEMPLATE_PREVIEW + val template: Int = CameraDevice.TEMPLATE_PREVIEW runCatching { previewRequestBuilder = camera?.createCaptureRequest(template) diff --git a/cameraViewEx/src/main/java/com/priyankvasa/android/cameraviewex/CameraView.kt b/cameraViewEx/src/main/java/com/priyankvasa/android/cameraviewex/CameraView.kt index 7f95c815..c00fee1c 100644 --- a/cameraViewEx/src/main/java/com/priyankvasa/android/cameraviewex/CameraView.kt +++ b/cameraViewEx/src/main/java/com/priyankvasa/android/cameraviewex/CameraView.kt @@ -118,7 +118,7 @@ class CameraView @JvmOverloads constructor( // Based on OS version select the best camera implementation camera = when { Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP -> - Camera1(listenerManager, preview, config, SupervisorJob(parentJob)) + Camera1(listenerManager, preview, config, SupervisorJob(parentJob), context) Build.VERSION.SDK_INT < Build.VERSION_CODES.M -> Camera2(listenerManager, preview, config, SupervisorJob(parentJob), context) Build.VERSION.SDK_INT < Build.VERSION_CODES.N -> @@ -552,7 +552,7 @@ class CameraView @JvmOverloads constructor( private fun fallback(cameraId: String, savedState: Parcelable) { - camera = Camera1(listenerManager, preview, config, SupervisorJob(parentJob)) + camera = Camera1(listenerManager, preview, config, SupervisorJob(parentJob), context) // Restore original state onRestoreInstanceState(savedState)