From d8ea6d5175a32e685fcf496078aab1ef4b5accb2 Mon Sep 17 00:00:00 2001 From: darthpaul Date: Wed, 13 Jul 2022 00:54:14 +0100 Subject: [PATCH] pass rotated resolution to ImageCaptureUsecase - filter supported resolutions so 0MP resolutions are not selected. CameraX cannot apply these resolutions --- .../camera/dialogs/ChangeResolutionDialogX.kt | 23 +++++++------------ .../camera/helpers/ImageQualityManager.kt | 6 ++--- .../camera/implementations/CameraXPreview.kt | 16 +++++++++++-- .../simplemobiletools/camera/models/MySize.kt | 8 +++++++ 4 files changed, 32 insertions(+), 21 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/camera/dialogs/ChangeResolutionDialogX.kt b/app/src/main/kotlin/com/simplemobiletools/camera/dialogs/ChangeResolutionDialogX.kt index 1b5c69a7..0961ca35 100644 --- a/app/src/main/kotlin/com/simplemobiletools/camera/dialogs/ChangeResolutionDialogX.kt +++ b/app/src/main/kotlin/com/simplemobiletools/camera/dialogs/ChangeResolutionDialogX.kt @@ -22,7 +22,7 @@ class ChangeResolutionDialogX( private val isFrontCamera: Boolean, private val photoResolutions: List = listOf(), private val videoResolutions: List, - private val callback: () -> Unit + private val callback: () -> Unit, ) { private var dialog: AlertDialog private val config = activity.config @@ -42,12 +42,16 @@ class ChangeResolutionDialogX( } private fun setupPhotoResolutionPicker(view: View) { - val items = getFormattedResolutions(photoResolutions) + val items = photoResolutions.mapIndexed { index, resolution-> + val megapixels = resolution.megaPixels + val aspectRatio = resolution.getAspectRatio(activity) + RadioItem(index, "${resolution.width} x ${resolution.height} ($megapixels MP, $aspectRatio)") + } var selectionIndex = if (isFrontCamera) config.frontPhotoResIndex else config.backPhotoResIndex - selectionIndex = Math.max(selectionIndex, 0) + selectionIndex = selectionIndex.coerceAtLeast(0) view.change_resolution_photo_holder.setOnClickListener { - RadioGroupDialog(activity, items, selectionIndex) { + RadioGroupDialog(activity, ArrayList(items), selectionIndex) { selectionIndex = it as Int Log.w(TAG, "setupPhotoResolutionPicker: selectionIndex=$it") view.change_resolution_photo.text = items[selectionIndex].title @@ -90,15 +94,4 @@ class ChangeResolutionDialogX( } view.change_resolution_video.text = items.getOrNull(selectionIndex)?.title } - - private fun getFormattedResolutions(resolutions: List): ArrayList { - val items = ArrayList(resolutions.size) - val sorted = resolutions.sortedByDescending { it.width * it.height } - sorted.forEachIndexed { index, size -> - val megapixels = String.format("%.1f", (size.width * size.height.toFloat()) / 1000000) - val aspectRatio = size.getAspectRatio(activity) - items.add(RadioItem(index, "${size.width} x ${size.height} ($megapixels MP, $aspectRatio)")) - } - return items - } } diff --git a/app/src/main/kotlin/com/simplemobiletools/camera/helpers/ImageQualityManager.kt b/app/src/main/kotlin/com/simplemobiletools/camera/helpers/ImageQualityManager.kt index bad6f719..dda22ed7 100644 --- a/app/src/main/kotlin/com/simplemobiletools/camera/helpers/ImageQualityManager.kt +++ b/app/src/main/kotlin/com/simplemobiletools/camera/helpers/ImageQualityManager.kt @@ -10,11 +10,8 @@ import android.util.Log import android.util.Size import androidx.appcompat.app.AppCompatActivity import androidx.camera.core.CameraSelector -import androidx.camera.video.Quality import com.simplemobiletools.camera.extensions.config -import com.simplemobiletools.camera.extensions.toCameraXQuality import com.simplemobiletools.camera.models.CameraSelectorImageQualities -import com.simplemobiletools.camera.models.CameraSelectorVideoQualities import com.simplemobiletools.camera.models.MySize class ImageQualityManager( @@ -69,7 +66,7 @@ class ImageQualityManager( val index = if (cameraSelector == CameraSelector.DEFAULT_FRONT_CAMERA) config.frontPhotoResIndex else config.backPhotoResIndex return imageQualities.filter { it.camSelector == cameraSelector } .flatMap { it.qualities } - .sortedByDescending { it.pixels} + .sortedByDescending { it.pixels } .distinctBy { it.pixels } .map { Size(it.width, it.height) } .also { @@ -85,5 +82,6 @@ class ImageQualityManager( .flatMap { it.qualities } .sortedByDescending { it.pixels } .distinctBy { it.pixels } + .filter { it.megaPixels != "0.0" } } } diff --git a/app/src/main/kotlin/com/simplemobiletools/camera/implementations/CameraXPreview.kt b/app/src/main/kotlin/com/simplemobiletools/camera/implementations/CameraXPreview.kt index 89aa6499..58b37803 100644 --- a/app/src/main/kotlin/com/simplemobiletools/camera/implementations/CameraXPreview.kt +++ b/app/src/main/kotlin/com/simplemobiletools/camera/implementations/CameraXPreview.kt @@ -5,6 +5,7 @@ import android.content.Context import android.hardware.SensorManager import android.hardware.display.DisplayManager import android.util.Log +import android.util.Size import android.view.* import android.view.GestureDetector.SimpleOnGestureListener import androidx.appcompat.app.AppCompatActivity @@ -74,7 +75,7 @@ class CameraXPreview( in 225 until 315 -> Surface.ROTATION_90 else -> Surface.ROTATION_0 } - + Log.i(TAG, "onOrientationChanged: rotation=$rotation") preview?.targetRotation = rotation imageCapture?.targetRotation = rotation videoCapture?.targetRotation = rotation @@ -194,13 +195,24 @@ class CameraXPreview( .setTargetRotation(rotation) .apply { imageQualityManager.getUserSelectedResolution(cameraSelector)?.let { resolution -> + val rotatedResolution = getRotatedResolution(rotation, resolution) + Log.i(TAG, "buildImageCapture: rotation=$rotation") Log.i(TAG, "buildImageCapture: resolution=$resolution") - setTargetResolution(resolution) + Log.i(TAG, "buildImageCapture: rotatedResolution=$rotatedResolution") + setTargetResolution(rotatedResolution) } ?: setTargetAspectRatio(aspectRatio) } .build() } + private fun getRotatedResolution(rotationDegrees: Int, resolution: Size): Size { + return if (rotationDegrees == Surface.ROTATION_0 || rotationDegrees == Surface.ROTATION_180) { + Size(resolution.height, resolution.width) + } else { + Size(resolution.width, resolution.height) + } + } + private fun buildPreview(aspectRatio: Int, rotation: Int): Preview { return Preview.Builder() .setTargetRotation(rotation) diff --git a/app/src/main/kotlin/com/simplemobiletools/camera/models/MySize.kt b/app/src/main/kotlin/com/simplemobiletools/camera/models/MySize.kt index d1a77804..bd9702ea 100644 --- a/app/src/main/kotlin/com/simplemobiletools/camera/models/MySize.kt +++ b/app/src/main/kotlin/com/simplemobiletools/camera/models/MySize.kt @@ -5,8 +5,16 @@ import android.util.Size import com.simplemobiletools.camera.R data class MySize(val width: Int, val height: Int) { + companion object { + private const val ONE_MEGA_PIXEL = 1000000 + } + val ratio = width / height.toFloat() + val pixels: Int = width * height + + val megaPixels: String = String.format("%.1f", (width * height.toFloat()) / ONE_MEGA_PIXEL) + fun isSixteenToNine() = ratio == 16 / 9f private fun isFiveToThree() = ratio == 5 / 3f