From fa9eb58012b28fc8275e8f3f659765fdce3f5b95 Mon Sep 17 00:00:00 2001 From: tibbi Date: Sun, 26 Mar 2017 12:11:22 +0200 Subject: [PATCH] move camera init into a standalone function --- .../com/simplemobiletools/camera/Preview.kt | 58 ++++++++++--------- 1 file changed, 31 insertions(+), 27 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/camera/Preview.kt b/app/src/main/kotlin/com/simplemobiletools/camera/Preview.kt index 53023691..01e6df69 100644 --- a/app/src/main/kotlin/com/simplemobiletools/camera/Preview.kt +++ b/app/src/main/kotlin/com/simplemobiletools/camera/Preview.kt @@ -123,39 +123,43 @@ class Preview : ViewGroup, SurfaceHolder.Callback, MediaScannerConnection.OnScan releaseCamera() mCamera = newCamera - if (mCamera != null) { - mParameters = mCamera!!.parameters - mMaxZoom = mParameters!!.maxZoom - mZoomRatios = mParameters!!.zoomRatios - mSupportedPreviewSizes = mParameters!!.supportedPreviewSizes.sortedByDescending { it.width * it.height } - refreshPreview() - - val focusModes = mParameters!!.supportedFocusModes - if (focusModes.contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE)) - mParameters!!.focusMode = Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE - - mCamera!!.setDisplayOrientation(mActivity.getPreviewRotation(cameraId)) - mCamera!!.parameters = mParameters - - if (mCanTakePicture) { - try { - mCamera!!.setPreviewDisplay(mSurfaceHolder) - } catch (e: IOException) { - Log.e(TAG, "setCamera setPreviewDisplay " + e.message) - return false - } - } - - mCallback.setFlashAvailable(hasFlash(mCamera)) - } - - if (mIsVideoMode) { + if (initCamera() && mIsVideoMode) { initRecorder() } return true } + private fun initCamera(): Boolean { + if (mCamera == null) + return false + + mParameters = mCamera!!.parameters + mMaxZoom = mParameters!!.maxZoom + mZoomRatios = mParameters!!.zoomRatios + mSupportedPreviewSizes = mParameters!!.supportedPreviewSizes.sortedByDescending { it.width * it.height } + refreshPreview() + + val focusModes = mParameters!!.supportedFocusModes + if (focusModes.contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE)) + mParameters!!.focusMode = Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE + + mCamera!!.setDisplayOrientation(mActivity.getPreviewRotation(mCurrCameraId)) + mCamera!!.parameters = mParameters + + if (mCanTakePicture) { + try { + mCamera!!.setPreviewDisplay(mSurfaceHolder) + } catch (e: IOException) { + Log.e(TAG, "setCamera setPreviewDisplay " + e.message) + return false + } + } + + mCallback.setFlashAvailable(hasFlash(mCamera)) + return true + } + private fun refreshPreview() { mIsSixteenToNine = getSelectedResolution().isSixteenToNine() mSetupPreviewAfterMeasure = true