Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some devices using Camera1 have an upside down camera #209

Open
wants to merge 2 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -542,7 +547,7 @@ internal class Camera1(
parameters.supportedPictureSizes
?.forEach { pictureSizes.add(it.width, it.height) }

setDisplayOrientation(calcDisplayOrientation(deviceRotation))
setDisplayOrientation(calcDisplayOrientation())
}
adjustCameraParameters()
listener.onCameraOpened()
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 ->
Expand Down Expand Up @@ -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)
Expand Down