From 3d7ec35e3b2e0f794babef7cb82d08cf2cb6a162 Mon Sep 17 00:00:00 2001 From: tibbi Date: Tue, 29 May 2018 20:38:55 +0200 Subject: [PATCH] handle auto flash too --- .../camera/views/PreviewCameraTwo.kt | 21 ++++++++----------- 1 file changed, 9 insertions(+), 12 deletions(-) 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 67d1e609..5f887de2 100644 --- a/app/src/main/kotlin/com/simplemobiletools/camera/views/PreviewCameraTwo.kt +++ b/app/src/main/kotlin/com/simplemobiletools/camera/views/PreviewCameraTwo.kt @@ -282,7 +282,7 @@ class PreviewCameraTwo : ViewGroup, TextureView.SurfaceTextureListener, MyPrevie mCaptureSession = cameraCaptureSession try { mPreviewRequestBuilder!!.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE) - setupPreviewProperties(mPreviewRequestBuilder!!) + mPreviewRequestBuilder!!.set(CaptureRequest.FLASH_MODE, getFlashlightMode(mFlashlightState)) mPreviewRequest = mPreviewRequestBuilder!!.build() mCaptureSession!!.setRepeatingRequest(mPreviewRequest, mCaptureCallback, mBackgroundHandler) mCameraState = STATE_PREVIEW @@ -298,11 +298,6 @@ class PreviewCameraTwo : ViewGroup, TextureView.SurfaceTextureListener, MyPrevie } } - private fun setupPreviewProperties(requestBuilder: CaptureRequest.Builder) { - val flashMode = if (mFlashlightState == FLASH_ON) CameraMetadata.FLASH_MODE_TORCH else CameraMetadata.FLASH_MODE_OFF - requestBuilder.set(CaptureRequest.FLASH_MODE, flashMode) - } - private val mCaptureCallback = object : CameraCaptureSession.CaptureCallback() { private fun process(result: CaptureResult) { when (mCameraState) { @@ -362,10 +357,7 @@ class PreviewCameraTwo : ViewGroup, TextureView.SurfaceTextureListener, MyPrevie val captureBuilder = mCameraDevice!!.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE) captureBuilder.addTarget(mImageReader!!.surface) - captureBuilder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE) - val flashMode = if (mFlashlightState == FLASH_ON) CameraMetadata.FLASH_MODE_TORCH else CameraMetadata.FLASH_MODE_OFF - captureBuilder.set(CaptureRequest.FLASH_MODE, flashMode) val rotation = mActivity.windowManager.defaultDisplay.rotation captureBuilder.set(CaptureRequest.JPEG_ORIENTATION, getOrientation(rotation)) @@ -390,7 +382,7 @@ class PreviewCameraTwo : ViewGroup, TextureView.SurfaceTextureListener, MyPrevie private fun unlockFocus() { try { mPreviewRequestBuilder!!.set(CaptureRequest.CONTROL_AF_TRIGGER, CameraMetadata.CONTROL_AF_TRIGGER_CANCEL) - setupPreviewProperties(mPreviewRequestBuilder!!) + mPreviewRequestBuilder!!.set(CaptureRequest.FLASH_MODE, getFlashlightMode(mFlashlightState)) mCaptureSession!!.capture(mPreviewRequestBuilder!!.build(), mCaptureCallback, mBackgroundHandler) mCameraState = STATE_PREVIEW mCaptureSession!!.setRepeatingRequest(mPreviewRequest, mCaptureCallback, mBackgroundHandler) @@ -411,6 +403,12 @@ class PreviewCameraTwo : ViewGroup, TextureView.SurfaceTextureListener, MyPrevie lockFocus() } + private fun getFlashlightMode(state: Int) = when (state) { + FLASH_OFF -> CameraMetadata.FLASH_MODE_OFF + FLASH_ON -> CameraMetadata.FLASH_MODE_TORCH + else -> CameraMetadata.FLASH_MODE_SINGLE + } + override fun setTargetUri(uri: Uri) { mTargetUri = uri } @@ -466,8 +464,7 @@ class PreviewCameraTwo : ViewGroup, TextureView.SurfaceTextureListener, MyPrevie override fun checkFlashlight() { if (mCameraState == STATE_PREVIEW) { - val flashMode = if (mFlashlightState == FLASH_ON) CameraMetadata.FLASH_MODE_TORCH else CameraMetadata.FLASH_MODE_OFF - mPreviewRequestBuilder!!.set(CaptureRequest.FLASH_MODE, flashMode) + mPreviewRequestBuilder!!.set(CaptureRequest.FLASH_MODE, getFlashlightMode(mFlashlightState)) mCaptureSession!!.setRepeatingRequest(mPreviewRequestBuilder!!.build(), mCaptureCallback, mBackgroundHandler) mActivity.updateFlashlightState(mFlashlightState) }