From 60d4372ac0a50b9757d682d5d40ffdb5db0f2c0d Mon Sep 17 00:00:00 2001 From: Marc Rousavy Date: Thu, 15 Feb 2024 13:09:47 +0100 Subject: [PATCH 1/6] fix: Take Orientation into account for `PreviewView` --- .../camera/core/CameraDeviceDetails.kt | 30 +++++++++++++++++-- .../com/mrousavy/camera/core/CameraSession.kt | 21 +++++++------ .../com/mrousavy/camera/core/PreviewView.kt | 13 ++++---- .../CameraCharacteristics+getPreviewSize.kt | 29 ------------------ 4 files changed, 45 insertions(+), 48 deletions(-) delete mode 100644 package/android/src/main/java/com/mrousavy/camera/extensions/CameraCharacteristics+getPreviewSize.kt diff --git a/package/android/src/main/java/com/mrousavy/camera/core/CameraDeviceDetails.kt b/package/android/src/main/java/com/mrousavy/camera/core/CameraDeviceDetails.kt index 5dbae987f1..2ffd95921b 100644 --- a/package/android/src/main/java/com/mrousavy/camera/core/CameraDeviceDetails.kt +++ b/package/android/src/main/java/com/mrousavy/camera/core/CameraDeviceDetails.kt @@ -1,5 +1,6 @@ package com.mrousavy.camera.core +import android.content.res.Resources import android.graphics.ImageFormat import android.hardware.camera2.CameraCharacteristics import android.hardware.camera2.CameraExtensionCharacteristics @@ -9,11 +10,14 @@ import android.os.Build import android.util.Log import android.util.Range import android.util.Size +import android.view.SurfaceHolder import com.facebook.react.bridge.Arguments import com.facebook.react.bridge.ReadableArray import com.facebook.react.bridge.ReadableMap +import com.mrousavy.camera.extensions.bigger import com.mrousavy.camera.extensions.getPhotoSizes import com.mrousavy.camera.extensions.getVideoSizes +import com.mrousavy.camera.extensions.smaller import com.mrousavy.camera.extensions.toJSValue import com.mrousavy.camera.types.AutoFocusSystem import com.mrousavy.camera.types.DeviceType @@ -29,6 +33,20 @@ import kotlin.math.sqrt class CameraDeviceDetails(private val cameraManager: CameraManager, val cameraId: String) { companion object { private const val TAG = "CameraDeviceDetails" + + fun getMaximumPreviewSize(): Size { + // See https://developer.android.com/reference/android/hardware/camera2/params/StreamConfigurationMap + // According to the Android Developer documentation, PREVIEW streams can have a resolution + // of up to the phone's display's resolution, with a maximum of 1920x1080. + val display1080p = Size(1920, 1080) + val displaySize = Size( + Resources.getSystem().displayMetrics.widthPixels, + Resources.getSystem().displayMetrics.heightPixels + ) + val isHighResScreen = displaySize.bigger >= display1080p.bigger || displaySize.smaller >= display1080p.smaller + + return if (isHighResScreen) display1080p else displaySize + } } val characteristics by lazy { cameraManager.getCameraCharacteristics(cameraId) } @@ -121,6 +139,7 @@ class CameraDeviceDetails(private val cameraManager: CameraManager, val cameraId // TODO: Also add 10-bit YUV here? val videoFormat = ImageFormat.YUV_420_888 + val photoFormat = ImageFormat.JPEG // get extensions (HDR, Night Mode, ..) private fun getSupportedExtensions(): List = @@ -214,13 +233,18 @@ class CameraDeviceDetails(private val cameraManager: CameraManager, val cameraId return getFieldOfView(smallestFocalLength) } - private fun getVideoSizes(): List = characteristics.getVideoSizes(cameraId, videoFormat) - private fun getPhotoSizes(): List = characteristics.getPhotoSizes(ImageFormat.JPEG) + fun getVideoSizes(format: Int): List = characteristics.getVideoSizes(cameraId, format) + fun getPhotoSizes(): List = characteristics.getPhotoSizes(photoFormat) + fun getPreviewSizes(): List { + val maximumPreviewSize = getMaximumPreviewSize() + return cameraConfig.getOutputSizes(SurfaceHolder::class.java) + .filter { it.bigger <= maximumPreviewSize.bigger && it.smaller <= maximumPreviewSize.smaller } + } private fun getFormats(): ReadableArray { val array = Arguments.createArray() - val videoSizes = getVideoSizes() + val videoSizes = getVideoSizes(videoFormat) val photoSizes = getPhotoSizes() videoSizes.forEach { videoSize -> diff --git a/package/android/src/main/java/com/mrousavy/camera/core/CameraSession.kt b/package/android/src/main/java/com/mrousavy/camera/core/CameraSession.kt index 4d5211743c..297139b0b6 100644 --- a/package/android/src/main/java/com/mrousavy/camera/core/CameraSession.kt +++ b/package/android/src/main/java/com/mrousavy/camera/core/CameraSession.kt @@ -23,9 +23,6 @@ import com.mrousavy.camera.core.outputs.PhotoOutput import com.mrousavy.camera.core.outputs.SurfaceOutput import com.mrousavy.camera.core.outputs.VideoPipelineOutput import com.mrousavy.camera.extensions.closestToOrMax -import com.mrousavy.camera.extensions.getPhotoSizes -import com.mrousavy.camera.extensions.getPreviewTargetSize -import com.mrousavy.camera.extensions.getVideoSizes import com.mrousavy.camera.frameprocessor.Frame import com.mrousavy.camera.types.Flash import com.mrousavy.camera.types.LensFacing @@ -245,20 +242,20 @@ class CameraSession(private val context: Context, private val cameraManager: Cam codeScannerOutput = null isRunning = false - val characteristics = cameraManager.getCameraCharacteristics(cameraId) + val deviceDetails = CameraDeviceDetails(cameraManager, cameraId) val format = configuration.format Log.i(TAG, "Creating outputs for Camera #$cameraId...") - val isSelfie = characteristics.get(CameraCharacteristics.LENS_FACING) == CameraCharacteristics.LENS_FACING_FRONT + val isSelfie = deviceDetails.lensFacing == LensFacing.FRONT val outputs = mutableListOf() // Photo Output val photo = configuration.photo as? CameraConfiguration.Output.Enabled if (photo != null) { - val imageFormat = ImageFormat.JPEG - val sizes = characteristics.getPhotoSizes(imageFormat) + val imageFormat = deviceDetails.photoFormat + val sizes = deviceDetails.getPhotoSizes() val size = sizes.closestToOrMax(format?.photoSize) val maxImages = 10 @@ -278,7 +275,7 @@ class CameraSession(private val context: Context, private val cameraManager: Cam val video = configuration.video as? CameraConfiguration.Output.Enabled if (video != null) { val imageFormat = video.config.pixelFormat.toImageFormat() - val sizes = characteristics.getVideoSizes(cameraId, imageFormat) + val sizes = deviceDetails.getVideoSizes(imageFormat) val size = sizes.closestToOrMax(format?.videoSize) Log.i(TAG, "Adding ${size.width}x${size.height} Video Output in ${ImageFormatUtils.imageFormatToString(imageFormat)}...") @@ -301,7 +298,8 @@ class CameraSession(private val context: Context, private val cameraManager: Cam if (preview != null) { // Compute Preview Size based on chosen video size val videoSize = videoOutput?.size ?: format?.videoSize - val size = characteristics.getPreviewTargetSize(videoSize) + val sizes = deviceDetails.getPreviewSizes() + val size = sizes.closestToOrMax(videoSize) val enableHdr = video?.config?.enableHdr ?: false @@ -314,7 +312,8 @@ class CameraSession(private val context: Context, private val cameraManager: Cam ) outputs.add(output) // Size is usually landscape, so we flip it here - previewView?.setSurfaceSize(size.width, size.height) + val cameraOrientation = Orientation.fromRotationDegrees(deviceDetails.sensorOrientation) + previewView?.setSurfaceSize(size.width, size.height, cameraOrientation) } // CodeScanner Output @@ -327,7 +326,7 @@ class CameraSession(private val context: Context, private val cameraManager: Cam } val imageFormat = ImageFormat.YUV_420_888 - val sizes = characteristics.getVideoSizes(cameraId, imageFormat) + val sizes = deviceDetails.getVideoSizes(imageFormat) val size = sizes.closestToOrMax(Size(1280, 720)) Log.i(TAG, "Adding ${size.width}x${size.height} CodeScanner Output in ${ImageFormatUtils.imageFormatToString(imageFormat)}...") diff --git a/package/android/src/main/java/com/mrousavy/camera/core/PreviewView.kt b/package/android/src/main/java/com/mrousavy/camera/core/PreviewView.kt index df33b83e5e..c76f81b24e 100644 --- a/package/android/src/main/java/com/mrousavy/camera/core/PreviewView.kt +++ b/package/android/src/main/java/com/mrousavy/camera/core/PreviewView.kt @@ -10,9 +10,9 @@ import android.view.SurfaceHolder import android.view.SurfaceView import android.widget.FrameLayout import com.facebook.react.bridge.UiThreadUtil -import com.mrousavy.camera.extensions.getMaximumPreviewSize import com.mrousavy.camera.extensions.installHierarchyFitter import com.mrousavy.camera.extensions.resize +import com.mrousavy.camera.extensions.rotatedBy import com.mrousavy.camera.types.Orientation import com.mrousavy.camera.types.ResizeMode import kotlin.math.roundToInt @@ -23,7 +23,7 @@ import kotlinx.coroutines.withContext class PreviewView(context: Context, callback: SurfaceHolder.Callback) : FrameLayout(context), SurfaceHolder.Callback { - var size: Size = getMaximumPreviewSize() + var size: Size = CameraDeviceDetails.getMaximumPreviewSize() private set var resizeMode: ResizeMode = ResizeMode.COVER set(value) { @@ -42,6 +42,7 @@ class PreviewView(context: Context, callback: SurfaceHolder.Callback) : return Size(dpX.toInt(), dpY.toInt()) } private val surfaceView = SurfaceView(context) + private var inputOrientation: Orientation = Orientation.PORTRAIT init { Log.i(TAG, "Creating PreviewView...") @@ -66,8 +67,9 @@ class PreviewView(context: Context, callback: SurfaceHolder.Callback) : invalidate() } - suspend fun setSurfaceSize(width: Int, height: Int) { + suspend fun setSurfaceSize(width: Int, height: Int, cameraSensorOrientation: Orientation) { withContext(Dispatchers.Main) { + inputOrientation = cameraSensorOrientation surfaceView.holder.resize(width, height) } } @@ -108,9 +110,10 @@ class PreviewView(context: Context, callback: SurfaceHolder.Callback) : super.onMeasure(widthMeasureSpec, heightMeasureSpec) val viewSize = Size(MeasureSpec.getSize(widthMeasureSpec), MeasureSpec.getSize(heightMeasureSpec)) - val fittedSize = getSize(size, viewSize, resizeMode) + val surfaceSize = size.rotatedBy(inputOrientation) + val fittedSize = getSize(surfaceSize, viewSize, resizeMode) - Log.i(TAG, "PreviewView is $viewSize, rendering $size content. Resizing to: $fittedSize ($resizeMode)") + Log.i(TAG, "PreviewView is $viewSize, rendering $surfaceSize content ($inputOrientation). Resizing to: $fittedSize ($resizeMode)") setMeasuredDimension(fittedSize.width, fittedSize.height) } diff --git a/package/android/src/main/java/com/mrousavy/camera/extensions/CameraCharacteristics+getPreviewSize.kt b/package/android/src/main/java/com/mrousavy/camera/extensions/CameraCharacteristics+getPreviewSize.kt deleted file mode 100644 index ecd525cf98..0000000000 --- a/package/android/src/main/java/com/mrousavy/camera/extensions/CameraCharacteristics+getPreviewSize.kt +++ /dev/null @@ -1,29 +0,0 @@ -package com.mrousavy.camera.extensions - -import android.content.res.Resources -import android.hardware.camera2.CameraCharacteristics -import android.util.Size -import android.view.SurfaceHolder - -fun getMaximumPreviewSize(): Size { - // See https://developer.android.com/reference/android/hardware/camera2/params/StreamConfigurationMap - // According to the Android Developer documentation, PREVIEW streams can have a resolution - // of up to the phone's display's resolution, with a maximum of 1920x1080. - val display1080p = Size(1920, 1080) - val displaySize = Size( - Resources.getSystem().displayMetrics.widthPixels, - Resources.getSystem().displayMetrics.heightPixels - ) - val isHighResScreen = displaySize.bigger >= display1080p.bigger || displaySize.smaller >= display1080p.smaller - - return if (isHighResScreen) display1080p else displaySize -} - -fun CameraCharacteristics.getPreviewTargetSize(targetSize: Size?): Size { - val config = this.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP)!! - val maximumPreviewSize = getMaximumPreviewSize() - val outputSizes = config.getOutputSizes(SurfaceHolder::class.java) - .filter { it.bigger <= maximumPreviewSize.bigger && it.smaller <= maximumPreviewSize.smaller } - - return outputSizes.closestToOrMax(targetSize) -} From 7447135f6fe71c969533c79c488ebca638b1ff62 Mon Sep 17 00:00:00 2001 From: Marc Rousavy Date: Thu, 15 Feb 2024 13:12:54 +0100 Subject: [PATCH 2/6] Log --- .../java/com/mrousavy/camera/core/PreviewView.kt | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/package/android/src/main/java/com/mrousavy/camera/core/PreviewView.kt b/package/android/src/main/java/com/mrousavy/camera/core/PreviewView.kt index c76f81b24e..08bf12d7a2 100644 --- a/package/android/src/main/java/com/mrousavy/camera/core/PreviewView.kt +++ b/package/android/src/main/java/com/mrousavy/camera/core/PreviewView.kt @@ -34,6 +34,15 @@ class PreviewView(context: Context, callback: SurfaceHolder.Callback) : invalidate() } } + private var inputOrientation: Orientation = Orientation.PORTRAIT + set(value) { + field = value + UiThreadUtil.runOnUiThread { + Log.i(TAG, "Camera Input Orientation changed to $value!") + requestLayout() + invalidate() + } + } private val viewSize: Size get() { val displayMetrics = context.resources.displayMetrics @@ -42,7 +51,6 @@ class PreviewView(context: Context, callback: SurfaceHolder.Callback) : return Size(dpX.toInt(), dpY.toInt()) } private val surfaceView = SurfaceView(context) - private var inputOrientation: Orientation = Orientation.PORTRAIT init { Log.i(TAG, "Creating PreviewView...") @@ -80,7 +88,7 @@ class PreviewView(context: Context, callback: SurfaceHolder.Callback) : val viewOrientation = Orientation.PORTRAIT val rotated = Orientation.rotatePoint(point, viewSize, cameraSize, viewOrientation, sensorOrientation) - Log.i(TAG, "$point -> $sensorOrientation (in $cameraSize -> $viewSize) -> $rotated") + Log.i(TAG, "Converted layer point $point to camera point $rotated! ($sensorOrientation, $cameraSize -> $viewSize)") return rotated } From d6e9bc324dfaad42c7a663009904b3b551d797d9 Mon Sep 17 00:00:00 2001 From: Marc Rousavy Date: Thu, 15 Feb 2024 13:19:37 +0100 Subject: [PATCH 3/6] Take aspect ratio into account --- .../src/main/java/com/mrousavy/camera/core/PreviewView.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package/android/src/main/java/com/mrousavy/camera/core/PreviewView.kt b/package/android/src/main/java/com/mrousavy/camera/core/PreviewView.kt index 08bf12d7a2..590b8f480e 100644 --- a/package/android/src/main/java/com/mrousavy/camera/core/PreviewView.kt +++ b/package/android/src/main/java/com/mrousavy/camera/core/PreviewView.kt @@ -94,7 +94,7 @@ class PreviewView(context: Context, callback: SurfaceHolder.Callback) : private fun getSize(contentSize: Size, containerSize: Size, resizeMode: ResizeMode): Size { // TODO: Take sensor orientation into account here - val contentAspectRatio = contentSize.height.toDouble() / contentSize.width + val contentAspectRatio = contentSize.width.toDouble() / contentSize.height val containerAspectRatio = containerSize.width.toDouble() / containerSize.height val widthOverHeight = when (resizeMode) { From aeefae9d56f7fde974491c2807ea092161b5be96 Mon Sep 17 00:00:00 2001 From: Marc Rousavy Date: Thu, 15 Feb 2024 13:25:27 +0100 Subject: [PATCH 4/6] Reorganize code a bit --- .../camera/core/CameraDeviceDetails.kt | 7 +++-- .../com/mrousavy/camera/core/CameraSession.kt | 3 +- .../com/mrousavy/camera/core/PreviewView.kt | 4 +-- .../camera/extensions/Point+rotatedBy.kt | 30 +++++++++++++++++++ .../com/mrousavy/camera/types/Orientation.kt | 30 +------------------ 5 files changed, 39 insertions(+), 35 deletions(-) create mode 100644 package/android/src/main/java/com/mrousavy/camera/extensions/Point+rotatedBy.kt diff --git a/package/android/src/main/java/com/mrousavy/camera/core/CameraDeviceDetails.kt b/package/android/src/main/java/com/mrousavy/camera/core/CameraDeviceDetails.kt index 2ffd95921b..89ed30df9a 100644 --- a/package/android/src/main/java/com/mrousavy/camera/core/CameraDeviceDetails.kt +++ b/package/android/src/main/java/com/mrousavy/camera/core/CameraDeviceDetails.kt @@ -68,7 +68,10 @@ class CameraDeviceDetails(private val cameraManager: CameraManager, val cameraId val sensorSize by lazy { characteristics.get(CameraCharacteristics.SENSOR_INFO_PHYSICAL_SIZE)!! } val activeSize get() = characteristics.get(CameraCharacteristics.SENSOR_INFO_ACTIVE_ARRAY_SIZE)!! - val sensorOrientation = characteristics.get(CameraCharacteristics.SENSOR_ORIENTATION) ?: 0 + val sensorOrientation by lazy { + val degrees = characteristics.get(CameraCharacteristics.SENSOR_ORIENTATION) ?: 0 + return@lazy Orientation.fromRotationDegrees(degrees) + } val minFocusDistance by lazy { getMinFocusDistanceCm() } val name by lazy { val info = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) characteristics.get(CameraCharacteristics.INFO_VERSION) else null @@ -318,7 +321,7 @@ class CameraDeviceDetails(private val cameraManager: CameraManager, val cameraId map.putDouble("minExposure", exposureRange.lower.toDouble()) map.putDouble("maxExposure", exposureRange.upper.toDouble()) map.putString("hardwareLevel", hardwareLevel.unionValue) - map.putString("sensorOrientation", Orientation.fromRotationDegrees(sensorOrientation).unionValue) + map.putString("sensorOrientation", sensorOrientation.unionValue) map.putArray("formats", getFormats()) return map } diff --git a/package/android/src/main/java/com/mrousavy/camera/core/CameraSession.kt b/package/android/src/main/java/com/mrousavy/camera/core/CameraSession.kt index 297139b0b6..ee9b2e53d7 100644 --- a/package/android/src/main/java/com/mrousavy/camera/core/CameraSession.kt +++ b/package/android/src/main/java/com/mrousavy/camera/core/CameraSession.kt @@ -312,8 +312,7 @@ class CameraSession(private val context: Context, private val cameraManager: Cam ) outputs.add(output) // Size is usually landscape, so we flip it here - val cameraOrientation = Orientation.fromRotationDegrees(deviceDetails.sensorOrientation) - previewView?.setSurfaceSize(size.width, size.height, cameraOrientation) + previewView?.setSurfaceSize(size.width, size.height, deviceDetails.sensorOrientation) } // CodeScanner Output diff --git a/package/android/src/main/java/com/mrousavy/camera/core/PreviewView.kt b/package/android/src/main/java/com/mrousavy/camera/core/PreviewView.kt index 590b8f480e..969a399825 100644 --- a/package/android/src/main/java/com/mrousavy/camera/core/PreviewView.kt +++ b/package/android/src/main/java/com/mrousavy/camera/core/PreviewView.kt @@ -83,11 +83,11 @@ class PreviewView(context: Context, callback: SurfaceHolder.Callback) : } fun convertLayerPointToCameraCoordinates(point: Point, cameraDeviceDetails: CameraDeviceDetails): Point { - val sensorOrientation = Orientation.fromRotationDegrees(cameraDeviceDetails.sensorOrientation) + val sensorOrientation = cameraDeviceDetails.sensorOrientation val cameraSize = Size(cameraDeviceDetails.activeSize.width(), cameraDeviceDetails.activeSize.height()) val viewOrientation = Orientation.PORTRAIT - val rotated = Orientation.rotatePoint(point, viewSize, cameraSize, viewOrientation, sensorOrientation) + val rotated = point.rotatedBy(viewSize, cameraSize, viewOrientation, sensorOrientation) Log.i(TAG, "Converted layer point $point to camera point $rotated! ($sensorOrientation, $cameraSize -> $viewSize)") return rotated } diff --git a/package/android/src/main/java/com/mrousavy/camera/extensions/Point+rotatedBy.kt b/package/android/src/main/java/com/mrousavy/camera/extensions/Point+rotatedBy.kt new file mode 100644 index 0000000000..a3109b4060 --- /dev/null +++ b/package/android/src/main/java/com/mrousavy/camera/extensions/Point+rotatedBy.kt @@ -0,0 +1,30 @@ +package com.mrousavy.camera.extensions + +import android.graphics.Point +import android.graphics.PointF +import android.util.Log +import android.util.Size +import com.mrousavy.camera.types.Orientation + +fun Point.rotatedBy( + fromSize: Size, + toSize: Size, + fromOrientation: Orientation, + toOrientation: Orientation +): Point { + val differenceDegrees = (fromOrientation.toDegrees() + toOrientation.toDegrees()) % 360 + val difference = Orientation.fromRotationDegrees(differenceDegrees) + val normalizedPoint = PointF(this.x / fromSize.width.toFloat(), this.y / fromSize.height.toFloat()) + + val rotatedNormalizedPoint = when (difference) { + Orientation.PORTRAIT -> normalizedPoint + Orientation.PORTRAIT_UPSIDE_DOWN -> PointF(1 - normalizedPoint.x, 1 - normalizedPoint.y) + Orientation.LANDSCAPE_LEFT -> PointF(normalizedPoint.y, 1 - normalizedPoint.x) + Orientation.LANDSCAPE_RIGHT -> PointF(1 - normalizedPoint.y, normalizedPoint.x) + } + + val rotatedX = rotatedNormalizedPoint.x * toSize.width + val rotatedY = rotatedNormalizedPoint.y * toSize.height + Log.i("ROTATE", "$this -> $normalizedPoint -> $difference -> $rotatedX, $rotatedY") + return Point(rotatedX.toInt(), rotatedY.toInt()) +} diff --git a/package/android/src/main/java/com/mrousavy/camera/types/Orientation.kt b/package/android/src/main/java/com/mrousavy/camera/types/Orientation.kt index a19f7a733e..64dc5bcf78 100644 --- a/package/android/src/main/java/com/mrousavy/camera/types/Orientation.kt +++ b/package/android/src/main/java/com/mrousavy/camera/types/Orientation.kt @@ -1,9 +1,5 @@ package com.mrousavy.camera.types -import android.graphics.Point -import android.graphics.PointF -import android.util.Log -import android.util.Size import com.mrousavy.camera.core.CameraDeviceDetails enum class Orientation(override val unionValue: String) : JSUnionValue { @@ -30,7 +26,7 @@ enum class Orientation(override val unionValue: String) : JSUnionValue { } // Rotate sensor rotation by target rotation - val newRotationDegrees = (deviceDetails.sensorOrientation + rotationDegrees + 360) % 360 + val newRotationDegrees = (deviceDetails.sensorOrientation.toDegrees() + rotationDegrees + 360) % 360 return fromRotationDegrees(newRotationDegrees) } @@ -52,29 +48,5 @@ enum class Orientation(override val unionValue: String) : JSUnionValue { in 225..315 -> LANDSCAPE_RIGHT else -> PORTRAIT } - - fun rotatePoint( - point: Point, - fromSize: Size, - toSize: Size, - fromOrientation: Orientation, - toOrientation: Orientation - ): Point { - val differenceDegrees = (fromOrientation.toDegrees() + toOrientation.toDegrees()) % 360 - val difference = Orientation.fromRotationDegrees(differenceDegrees) - val normalizedPoint = PointF(point.x / fromSize.width.toFloat(), point.y / fromSize.height.toFloat()) - - val rotatedNormalizedPoint = when (difference) { - PORTRAIT -> normalizedPoint - PORTRAIT_UPSIDE_DOWN -> PointF(1 - normalizedPoint.x, 1 - normalizedPoint.y) - LANDSCAPE_LEFT -> PointF(normalizedPoint.y, 1 - normalizedPoint.x) - LANDSCAPE_RIGHT -> PointF(1 - normalizedPoint.y, normalizedPoint.x) - } - - val rotatedX = rotatedNormalizedPoint.x * toSize.width - val rotatedY = rotatedNormalizedPoint.y * toSize.height - Log.i("ROTATE", "$point -> $normalizedPoint -> $difference -> $rotatedX, $rotatedY") - return Point(rotatedX.toInt(), rotatedY.toInt()) - } } } From 57fb71ac7095eed00a04d3e1d0f6ea530658edbb Mon Sep 17 00:00:00 2001 From: Marc Rousavy Date: Thu, 15 Feb 2024 13:26:01 +0100 Subject: [PATCH 5/6] Set LANDSCAPE_LEFT as default --- .../src/main/java/com/mrousavy/camera/core/PreviewView.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package/android/src/main/java/com/mrousavy/camera/core/PreviewView.kt b/package/android/src/main/java/com/mrousavy/camera/core/PreviewView.kt index 969a399825..02ad6027bc 100644 --- a/package/android/src/main/java/com/mrousavy/camera/core/PreviewView.kt +++ b/package/android/src/main/java/com/mrousavy/camera/core/PreviewView.kt @@ -34,7 +34,7 @@ class PreviewView(context: Context, callback: SurfaceHolder.Callback) : invalidate() } } - private var inputOrientation: Orientation = Orientation.PORTRAIT + private var inputOrientation: Orientation = Orientation.LANDSCAPE_LEFT set(value) { field = value UiThreadUtil.runOnUiThread { From d19f069fa29eb46824c5b16d2f40ed8eef86065d Mon Sep 17 00:00:00 2001 From: Marc Rousavy Date: Thu, 15 Feb 2024 13:26:12 +0100 Subject: [PATCH 6/6] chore: Format --- .../java/com/mrousavy/camera/core/CameraDeviceDetails.kt | 6 +++--- .../java/com/mrousavy/camera/extensions/Point+rotatedBy.kt | 7 +------ 2 files changed, 4 insertions(+), 9 deletions(-) diff --git a/package/android/src/main/java/com/mrousavy/camera/core/CameraDeviceDetails.kt b/package/android/src/main/java/com/mrousavy/camera/core/CameraDeviceDetails.kt index 89ed30df9a..3d20edb9f3 100644 --- a/package/android/src/main/java/com/mrousavy/camera/core/CameraDeviceDetails.kt +++ b/package/android/src/main/java/com/mrousavy/camera/core/CameraDeviceDetails.kt @@ -40,8 +40,8 @@ class CameraDeviceDetails(private val cameraManager: CameraManager, val cameraId // of up to the phone's display's resolution, with a maximum of 1920x1080. val display1080p = Size(1920, 1080) val displaySize = Size( - Resources.getSystem().displayMetrics.widthPixels, - Resources.getSystem().displayMetrics.heightPixels + Resources.getSystem().displayMetrics.widthPixels, + Resources.getSystem().displayMetrics.heightPixels ) val isHighResScreen = displaySize.bigger >= display1080p.bigger || displaySize.smaller >= display1080p.smaller @@ -241,7 +241,7 @@ class CameraDeviceDetails(private val cameraManager: CameraManager, val cameraId fun getPreviewSizes(): List { val maximumPreviewSize = getMaximumPreviewSize() return cameraConfig.getOutputSizes(SurfaceHolder::class.java) - .filter { it.bigger <= maximumPreviewSize.bigger && it.smaller <= maximumPreviewSize.smaller } + .filter { it.bigger <= maximumPreviewSize.bigger && it.smaller <= maximumPreviewSize.smaller } } private fun getFormats(): ReadableArray { diff --git a/package/android/src/main/java/com/mrousavy/camera/extensions/Point+rotatedBy.kt b/package/android/src/main/java/com/mrousavy/camera/extensions/Point+rotatedBy.kt index a3109b4060..7e84e3ff95 100644 --- a/package/android/src/main/java/com/mrousavy/camera/extensions/Point+rotatedBy.kt +++ b/package/android/src/main/java/com/mrousavy/camera/extensions/Point+rotatedBy.kt @@ -6,12 +6,7 @@ import android.util.Log import android.util.Size import com.mrousavy.camera.types.Orientation -fun Point.rotatedBy( - fromSize: Size, - toSize: Size, - fromOrientation: Orientation, - toOrientation: Orientation -): Point { +fun Point.rotatedBy(fromSize: Size, toSize: Size, fromOrientation: Orientation, toOrientation: Orientation): Point { val differenceDegrees = (fromOrientation.toDegrees() + toOrientation.toDegrees()) % 360 val difference = Orientation.fromRotationDegrees(differenceDegrees) val normalizedPoint = PointF(this.x / fromSize.width.toFloat(), this.y / fromSize.height.toFloat())