From 63d228a50ea25b07d8b35892b8a7134fee6669cc Mon Sep 17 00:00:00 2001 From: tibbi Date: Tue, 10 Jul 2018 11:41:29 +0200 Subject: [PATCH] use the selected resolution as preview, if nothing better is found --- .../kotlin/com/simplemobiletools/camera/models/MySize.kt | 6 ++++++ .../simplemobiletools/camera/views/PreviewCameraTwo.kt | 9 +++++---- 2 files changed, 11 insertions(+), 4 deletions(-) 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 0d3c7874..173d2c15 100644 --- a/app/src/main/kotlin/com/simplemobiletools/camera/models/MySize.kt +++ b/app/src/main/kotlin/com/simplemobiletools/camera/models/MySize.kt @@ -1,6 +1,9 @@ package com.simplemobiletools.camera.models +import android.annotation.TargetApi import android.content.Context +import android.os.Build +import android.util.Size import com.simplemobiletools.camera.R data class MySize(val width: Int, val height: Int) { @@ -41,4 +44,7 @@ data class MySize(val width: Int, val height: Int) { isTwoToOne() -> "2:1" else -> context.resources.getString(R.string.other) } + + @TargetApi(Build.VERSION_CODES.LOLLIPOP) + fun toSize() = Size(width, height) } diff --git a/app/src/main/kotlin/com/simplemobiletools/camera/views/PreviewCameraTwo.kt b/app/src/main/kotlin/com/simplemobiletools/camera/views/PreviewCameraTwo.kt index e4db7608..4caa3783 100644 --- a/app/src/main/kotlin/com/simplemobiletools/camera/views/PreviewCameraTwo.kt +++ b/app/src/main/kotlin/com/simplemobiletools/camera/views/PreviewCameraTwo.kt @@ -369,6 +369,7 @@ class PreviewCameraTwo : ViewGroup, TextureView.SurfaceTextureListener, MyPrevie } else { configMap.getOutputSizes(SurfaceTexture::class.java) } + mPreviewSize = chooseOptimalPreviewSize(outputSizes, rotatedPreviewWidth, rotatedPreviewHeight, maxPreviewWidth, maxPreviewHeight, currentResolution) mActivity.runOnUiThread { @@ -388,11 +389,11 @@ class PreviewCameraTwo : ViewGroup, TextureView.SurfaceTextureListener, MyPrevie } } - private fun chooseOptimalPreviewSize(choices: Array, textureViewWidth: Int, textureViewHeight: Int, maxWidth: Int, maxHeight: Int, aspectRatio: MySize): Size { + private fun chooseOptimalPreviewSize(choices: Array, textureViewWidth: Int, textureViewHeight: Int, maxWidth: Int, maxHeight: Int, selectedResolution: MySize): Size { val bigEnough = ArrayList() val notBigEnough = ArrayList() - val width = aspectRatio.width - val height = aspectRatio.height + val width = selectedResolution.width + val height = selectedResolution.height for (option in choices) { if (option.width <= maxWidth && option.height <= maxHeight && option.height == option.width * height / width) { if (option.width >= textureViewWidth && option.height >= textureViewHeight) { @@ -406,7 +407,7 @@ class PreviewCameraTwo : ViewGroup, TextureView.SurfaceTextureListener, MyPrevie return when { bigEnough.isNotEmpty() -> bigEnough.minBy { it.width * it.height }!! notBigEnough.isNotEmpty() -> notBigEnough.maxBy { it.width * it.height }!! - else -> choices.first() + else -> selectedResolution.toSize() } }