From bb83a47f4d917f4071fbe3e6c9df1d807e789740 Mon Sep 17 00:00:00 2001 From: Thibaut Girka Date: Wed, 2 Oct 2019 10:35:36 +0200 Subject: [PATCH] Try selecting compatible preview sizes even if the aspect ratio doesn't match --- .../camera/views/CameraPreview.kt | 26 +++++++++++++++---- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/camera/views/CameraPreview.kt b/app/src/main/kotlin/com/simplemobiletools/camera/views/CameraPreview.kt index 87768743..e072fbe7 100644 --- a/app/src/main/kotlin/com/simplemobiletools/camera/views/CameraPreview.kt +++ b/app/src/main/kotlin/com/simplemobiletools/camera/views/CameraPreview.kt @@ -386,14 +386,24 @@ class CameraPreview : ViewGroup, TextureView.SurfaceTextureListener, MyPreview { private fun chooseOptimalPreviewSize(choices: Array, textureViewWidth: Int, textureViewHeight: Int, maxWidth: Int, maxHeight: Int, selectedResolution: MySize): Size { val bigEnough = ArrayList() val notBigEnough = ArrayList() + val bigEnoughIncorrectAR = ArrayList() + val notBigEnoughIncorrectAR = ArrayList() 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) { - bigEnough.add(option) + if (option.width <= maxWidth && option.height <= maxHeight) { + if (option.height == option.width * height / width) { + if (option.width >= textureViewWidth && option.height >= textureViewHeight) { + bigEnough.add(option) + } else { + notBigEnough.add(option) + } } else { - notBigEnough.add(option) + if (option.width >= textureViewWidth && option.height >= textureViewHeight) { + bigEnoughIncorrectAR.add(option) + } else { + notBigEnoughIncorrectAR.add(option) + } } } } @@ -401,6 +411,8 @@ class CameraPreview : ViewGroup, TextureView.SurfaceTextureListener, MyPreview { return when { bigEnough.isNotEmpty() -> bigEnough.minBy { it.width * it.height }!! notBigEnough.isNotEmpty() -> notBigEnough.maxBy { it.width * it.height }!! + bigEnoughIncorrectAR.isNotEmpty() -> bigEnoughIncorrectAR.minBy { it.width * it.height }!! + notBigEnoughIncorrectAR.isNotEmpty() -> notBigEnoughIncorrectAR.maxBy { it.width * it.height }!! else -> selectedResolution.toSize() } } @@ -457,10 +469,13 @@ class CameraPreview : ViewGroup, TextureView.SurfaceTextureListener, MyPreview { } mCameraState = STATE_PREVIEW } catch (e: Exception) { + mActivity.showErrorToast("Error in onConfigure callback: $e") } } - override fun onConfigureFailed(cameraCaptureSession: CameraCaptureSession) {} + override fun onConfigureFailed(cameraCaptureSession: CameraCaptureSession) { + mActivity.showErrorToast("Failed configuring capture session") + } } try { @@ -479,6 +494,7 @@ class CameraPreview : ViewGroup, TextureView.SurfaceTextureListener, MyPreview { mCameraDevice!!.createCaptureSession(Arrays.asList(surface, mImageReader!!.surface), stateCallback, null) } } catch (e: Exception) { + mActivity.showErrorToast("Error setting up capture session: $e") } }