Merge pull request #233 from ThibG/fixes/front-camera

Try selecting compatible preview sizes even if the aspect ratio doesn't match
This commit is contained in:
Tibor Kaputa 2019-10-28 23:06:10 +01:00 committed by GitHub
commit b1265bc960
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 21 additions and 5 deletions

View File

@ -386,14 +386,24 @@ class CameraPreview : ViewGroup, TextureView.SurfaceTextureListener, MyPreview {
private fun chooseOptimalPreviewSize(choices: Array<Size>, textureViewWidth: Int, textureViewHeight: Int, maxWidth: Int, maxHeight: Int, selectedResolution: MySize): Size {
val bigEnough = ArrayList<Size>()
val notBigEnough = ArrayList<Size>()
val bigEnoughIncorrectAR = ArrayList<Size>()
val notBigEnoughIncorrectAR = ArrayList<Size>()
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")
}
}