mirror of
				https://github.com/SimpleMobileTools/Simple-Camera.git
				synced 2025-06-27 09:02:59 +02:00 
			
		
		
		
	Try selecting compatible preview sizes even if the aspect ratio doesn't match
This commit is contained in:
		| @@ -386,21 +386,33 @@ class CameraPreview : ViewGroup, TextureView.SurfaceTextureListener, MyPreview { | |||||||
|     private fun chooseOptimalPreviewSize(choices: Array<Size>, textureViewWidth: Int, textureViewHeight: Int, maxWidth: Int, maxHeight: Int, selectedResolution: MySize): Size { |     private fun chooseOptimalPreviewSize(choices: Array<Size>, textureViewWidth: Int, textureViewHeight: Int, maxWidth: Int, maxHeight: Int, selectedResolution: MySize): Size { | ||||||
|         val bigEnough = ArrayList<Size>() |         val bigEnough = ArrayList<Size>() | ||||||
|         val notBigEnough = ArrayList<Size>() |         val notBigEnough = ArrayList<Size>() | ||||||
|  |         val bigEnoughIncorrectAR = ArrayList<Size>() | ||||||
|  |         val notBigEnoughIncorrectAR = ArrayList<Size>() | ||||||
|         val width = selectedResolution.width |         val width = selectedResolution.width | ||||||
|         val height = selectedResolution.height |         val height = selectedResolution.height | ||||||
|         for (option in choices) { |         for (option in choices) { | ||||||
|             if (option.width <= maxWidth && option.height <= maxHeight && option.height == option.width * height / width) { |             if (option.width <= maxWidth && option.height <= maxHeight) { | ||||||
|  |                 if (option.height == option.width * height / width) { | ||||||
|                     if (option.width >= textureViewWidth && option.height >= textureViewHeight) { |                     if (option.width >= textureViewWidth && option.height >= textureViewHeight) { | ||||||
|                         bigEnough.add(option) |                         bigEnough.add(option) | ||||||
|                     } else { |                     } else { | ||||||
|                         notBigEnough.add(option) |                         notBigEnough.add(option) | ||||||
|                     } |                     } | ||||||
|  |                 } else { | ||||||
|  |                     if (option.width >= textureViewWidth && option.height >= textureViewHeight) { | ||||||
|  |                         bigEnoughIncorrectAR.add(option) | ||||||
|  |                     } else { | ||||||
|  |                         notBigEnoughIncorrectAR.add(option) | ||||||
|  |                     } | ||||||
|  |                 } | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
|  |  | ||||||
|         return when { |         return when { | ||||||
|             bigEnough.isNotEmpty() -> bigEnough.minBy { it.width * it.height }!! |             bigEnough.isNotEmpty() -> bigEnough.minBy { it.width * it.height }!! | ||||||
|             notBigEnough.isNotEmpty() -> notBigEnough.maxBy { 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() |             else -> selectedResolution.toSize() | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| @@ -457,10 +469,13 @@ class CameraPreview : ViewGroup, TextureView.SurfaceTextureListener, MyPreview { | |||||||
|                     } |                     } | ||||||
|                     mCameraState = STATE_PREVIEW |                     mCameraState = STATE_PREVIEW | ||||||
|                 } catch (e: Exception) { |                 } 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 { |         try { | ||||||
| @@ -479,6 +494,7 @@ class CameraPreview : ViewGroup, TextureView.SurfaceTextureListener, MyPreview { | |||||||
|                 mCameraDevice!!.createCaptureSession(Arrays.asList(surface, mImageReader!!.surface), stateCallback, null) |                 mCameraDevice!!.createCaptureSession(Arrays.asList(surface, mImageReader!!.surface), stateCallback, null) | ||||||
|             } |             } | ||||||
|         } catch (e: Exception) { |         } catch (e: Exception) { | ||||||
|  |           mActivity.showErrorToast("Error setting up capture session: $e") | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user