pass rotated resolution to ImageCaptureUsecase

- filter supported resolutions so 0MP resolutions are not selected. CameraX cannot apply these resolutions
This commit is contained in:
darthpaul 2022-07-13 00:54:14 +01:00
parent d3d67ec4a4
commit d8ea6d5175
4 changed files with 32 additions and 21 deletions

View File

@ -22,7 +22,7 @@ class ChangeResolutionDialogX(
private val isFrontCamera: Boolean, private val isFrontCamera: Boolean,
private val photoResolutions: List<MySize> = listOf(), private val photoResolutions: List<MySize> = listOf(),
private val videoResolutions: List<VideoQuality>, private val videoResolutions: List<VideoQuality>,
private val callback: () -> Unit private val callback: () -> Unit,
) { ) {
private var dialog: AlertDialog private var dialog: AlertDialog
private val config = activity.config private val config = activity.config
@ -42,12 +42,16 @@ class ChangeResolutionDialogX(
} }
private fun setupPhotoResolutionPicker(view: View) { 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 var selectionIndex = if (isFrontCamera) config.frontPhotoResIndex else config.backPhotoResIndex
selectionIndex = Math.max(selectionIndex, 0) selectionIndex = selectionIndex.coerceAtLeast(0)
view.change_resolution_photo_holder.setOnClickListener { view.change_resolution_photo_holder.setOnClickListener {
RadioGroupDialog(activity, items, selectionIndex) { RadioGroupDialog(activity, ArrayList(items), selectionIndex) {
selectionIndex = it as Int selectionIndex = it as Int
Log.w(TAG, "setupPhotoResolutionPicker: selectionIndex=$it") Log.w(TAG, "setupPhotoResolutionPicker: selectionIndex=$it")
view.change_resolution_photo.text = items[selectionIndex].title view.change_resolution_photo.text = items[selectionIndex].title
@ -90,15 +94,4 @@ class ChangeResolutionDialogX(
} }
view.change_resolution_video.text = items.getOrNull(selectionIndex)?.title view.change_resolution_video.text = items.getOrNull(selectionIndex)?.title
} }
private fun getFormattedResolutions(resolutions: List<MySize>): ArrayList<RadioItem> {
val items = ArrayList<RadioItem>(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
}
} }

View File

@ -10,11 +10,8 @@ import android.util.Log
import android.util.Size import android.util.Size
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import androidx.camera.core.CameraSelector import androidx.camera.core.CameraSelector
import androidx.camera.video.Quality
import com.simplemobiletools.camera.extensions.config import com.simplemobiletools.camera.extensions.config
import com.simplemobiletools.camera.extensions.toCameraXQuality
import com.simplemobiletools.camera.models.CameraSelectorImageQualities import com.simplemobiletools.camera.models.CameraSelectorImageQualities
import com.simplemobiletools.camera.models.CameraSelectorVideoQualities
import com.simplemobiletools.camera.models.MySize import com.simplemobiletools.camera.models.MySize
class ImageQualityManager( class ImageQualityManager(
@ -69,7 +66,7 @@ class ImageQualityManager(
val index = if (cameraSelector == CameraSelector.DEFAULT_FRONT_CAMERA) config.frontPhotoResIndex else config.backPhotoResIndex val index = if (cameraSelector == CameraSelector.DEFAULT_FRONT_CAMERA) config.frontPhotoResIndex else config.backPhotoResIndex
return imageQualities.filter { it.camSelector == cameraSelector } return imageQualities.filter { it.camSelector == cameraSelector }
.flatMap { it.qualities } .flatMap { it.qualities }
.sortedByDescending { it.pixels} .sortedByDescending { it.pixels }
.distinctBy { it.pixels } .distinctBy { it.pixels }
.map { Size(it.width, it.height) } .map { Size(it.width, it.height) }
.also { .also {
@ -85,5 +82,6 @@ class ImageQualityManager(
.flatMap { it.qualities } .flatMap { it.qualities }
.sortedByDescending { it.pixels } .sortedByDescending { it.pixels }
.distinctBy { it.pixels } .distinctBy { it.pixels }
.filter { it.megaPixels != "0.0" }
} }
} }

View File

@ -5,6 +5,7 @@ import android.content.Context
import android.hardware.SensorManager import android.hardware.SensorManager
import android.hardware.display.DisplayManager import android.hardware.display.DisplayManager
import android.util.Log import android.util.Log
import android.util.Size
import android.view.* import android.view.*
import android.view.GestureDetector.SimpleOnGestureListener import android.view.GestureDetector.SimpleOnGestureListener
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
@ -74,7 +75,7 @@ class CameraXPreview(
in 225 until 315 -> Surface.ROTATION_90 in 225 until 315 -> Surface.ROTATION_90
else -> Surface.ROTATION_0 else -> Surface.ROTATION_0
} }
Log.i(TAG, "onOrientationChanged: rotation=$rotation")
preview?.targetRotation = rotation preview?.targetRotation = rotation
imageCapture?.targetRotation = rotation imageCapture?.targetRotation = rotation
videoCapture?.targetRotation = rotation videoCapture?.targetRotation = rotation
@ -194,13 +195,24 @@ class CameraXPreview(
.setTargetRotation(rotation) .setTargetRotation(rotation)
.apply { .apply {
imageQualityManager.getUserSelectedResolution(cameraSelector)?.let { resolution -> imageQualityManager.getUserSelectedResolution(cameraSelector)?.let { resolution ->
val rotatedResolution = getRotatedResolution(rotation, resolution)
Log.i(TAG, "buildImageCapture: rotation=$rotation")
Log.i(TAG, "buildImageCapture: resolution=$resolution") Log.i(TAG, "buildImageCapture: resolution=$resolution")
setTargetResolution(resolution) Log.i(TAG, "buildImageCapture: rotatedResolution=$rotatedResolution")
setTargetResolution(rotatedResolution)
} ?: setTargetAspectRatio(aspectRatio) } ?: setTargetAspectRatio(aspectRatio)
} }
.build() .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 { private fun buildPreview(aspectRatio: Int, rotation: Int): Preview {
return Preview.Builder() return Preview.Builder()
.setTargetRotation(rotation) .setTargetRotation(rotation)

View File

@ -5,8 +5,16 @@ import android.util.Size
import com.simplemobiletools.camera.R import com.simplemobiletools.camera.R
data class MySize(val width: Int, val height: Int) { data class MySize(val width: Int, val height: Int) {
companion object {
private const val ONE_MEGA_PIXEL = 1000000
}
val ratio = width / height.toFloat() val ratio = width / height.toFloat()
val pixels: Int = width * height val pixels: Int = width * height
val megaPixels: String = String.format("%.1f", (width * height.toFloat()) / ONE_MEGA_PIXEL)
fun isSixteenToNine() = ratio == 16 / 9f fun isSixteenToNine() = ratio == 16 / 9f
private fun isFiveToThree() = ratio == 5 / 3f private fun isFiveToThree() = ratio == 5 / 3f