From 7acdcd60e69eab97a816c7de5691d6aa1992dda4 Mon Sep 17 00:00:00 2001 From: tibbi Date: Fri, 16 Jun 2017 21:01:03 +0200 Subject: [PATCH] fix some issues with camera stucking at focus stage --- .../camera/PhotoProcessor.kt | 6 +- .../com/simplemobiletools/camera/Preview.kt | 82 ++++++++++--------- .../camera/dialogs/ChangeResolutionDialog.kt | 4 +- 3 files changed, 50 insertions(+), 42 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/camera/PhotoProcessor.kt b/app/src/main/kotlin/com/simplemobiletools/camera/PhotoProcessor.kt index c815360f..1dc07b41 100644 --- a/app/src/main/kotlin/com/simplemobiletools/camera/PhotoProcessor.kt +++ b/app/src/main/kotlin/com/simplemobiletools/camera/PhotoProcessor.kt @@ -8,9 +8,9 @@ import android.net.Uri import android.os.AsyncTask import android.os.Environment import android.util.Log -import com.simplemobiletools.camera.Preview.Companion.config import com.simplemobiletools.camera.activities.MainActivity import com.simplemobiletools.camera.extensions.compensateDeviceRotation +import com.simplemobiletools.camera.extensions.config import com.simplemobiletools.camera.extensions.getOutputMediaFile import com.simplemobiletools.camera.extensions.getPreviewRotation import com.simplemobiletools.commons.extensions.getFileDocument @@ -49,11 +49,11 @@ class PhotoProcessor(val activity: MainActivity, val uri: Uri?, val currCameraId val data = params[0] val photoFile = File(path) if (activity.needsStupidWritePermissions(path)) { - if (config.treeUri.isEmpty()) { + if (activity.config.treeUri.isEmpty()) { activity.runOnUiThread { activity.toast(R.string.save_error_internal_storage) } - config.savePhotosFolder = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).toString() + activity.config.savePhotosFolder = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).toString() return "" } var document = activity.getFileDocument(path) diff --git a/app/src/main/kotlin/com/simplemobiletools/camera/Preview.kt b/app/src/main/kotlin/com/simplemobiletools/camera/Preview.kt index c30a12ea..3e8fcc57 100644 --- a/app/src/main/kotlin/com/simplemobiletools/camera/Preview.kt +++ b/app/src/main/kotlin/com/simplemobiletools/camera/Preview.kt @@ -23,42 +23,43 @@ import java.io.IOException import java.util.* class Preview : ViewGroup, SurfaceHolder.Callback, MediaScannerConnection.OnScanCompletedListener { - companion object { - var mCamera: Camera? = null - private val TAG = Preview::class.java.simpleName - private val FOCUS_AREA_SIZE = 100 - private val PHOTO_PREVIEW_LENGTH = 500L + var mCamera: Camera? = null + private val TAG = Preview::class.java.simpleName + private val FOCUS_AREA_SIZE = 100 + private val PHOTO_PREVIEW_LENGTH = 500L + private val REFOCUS_PERIOD = 3000L - lateinit var mSurfaceHolder: SurfaceHolder - lateinit var mSurfaceView: SurfaceView - lateinit var mActivity: MainActivity - lateinit var mCallback: PreviewListener - lateinit var mScreenSize: Point - lateinit var config: Config - private var mSupportedPreviewSizes: List? = null - private var mPreviewSize: Camera.Size? = null - private var mParameters: Camera.Parameters? = null - private var mRecorder: MediaRecorder? = null - private var mTargetUri: Uri? = null - private var mScaleGestureDetector: ScaleGestureDetector? = null - private var mZoomRatios: List? = null + lateinit var mSurfaceHolder: SurfaceHolder + lateinit var mSurfaceView: SurfaceView + lateinit var mActivity: MainActivity + lateinit var mCallback: PreviewListener + lateinit var mScreenSize: Point + lateinit var config: Config + private var mSupportedPreviewSizes: List? = null + private var mPreviewSize: Camera.Size? = null + private var mParameters: Camera.Parameters? = null + private var mRecorder: MediaRecorder? = null + private var mTargetUri: Uri? = null + private var mScaleGestureDetector: ScaleGestureDetector? = null + private var mZoomRatios: List? = null - private var mCurrVideoPath = "" - private var mCanTakePicture = false - private var mIsRecording = false - private var mIsVideoMode = false - private var mIsSurfaceCreated = false - private var mSwitchToVideoAsap = false - private var mSetupPreviewAfterMeasure = false - private var mIsSixteenToNine = false - private var mWasZooming = false - private var mIsPreviewShown = false - private var mLastClickX = 0 - private var mLastClickY = 0 - private var mCurrCameraId = 0 - private var mMaxZoom = 0 - private var mRotationAtCapture = 0 - } + private var mCurrVideoPath = "" + private var mCanTakePicture = false + private var mIsRecording = false + private var mIsVideoMode = false + private var mIsSurfaceCreated = false + private var mSwitchToVideoAsap = false + private var mSetupPreviewAfterMeasure = false + private var mIsSixteenToNine = false + private var mWasZooming = false + private var mIsPreviewShown = false + private var mLastClickX = 0 + private var mLastClickY = 0 + private var mCurrCameraId = 0 + private var mMaxZoom = 0 + private var mRotationAtCapture = 0 + private var mIsFocusing = false + private var autoFocusHandler = Handler() constructor(context: Context) : super(context) @@ -292,6 +293,7 @@ class Preview : ViewGroup, SurfaceHolder.Callback, MediaScannerConnection.OnScan mCamera!!.takePicture(null, null, takePictureCallback) } mCanTakePicture = false + mIsFocusing = false } private val takePictureCallback = Camera.PictureCallback { data, cam -> @@ -315,12 +317,16 @@ class Preview : ViewGroup, SurfaceHolder.Callback, MediaScannerConnection.OnScan mActivity.toggleBottomButtons(false) mCamera?.startPreview() mCanTakePicture = true + focusArea(false, false) } private fun focusArea(takePictureAfter: Boolean, showFocusRect: Boolean = true) { - if (mCamera == null) + if (mCamera == null || (mIsFocusing && !takePictureAfter)) return + if (takePictureAfter) + mIsFocusing = true + mCamera!!.cancelAutoFocus() if (mParameters!!.maxNumFocusAreas > 0) { if (mLastClickX == 0 && mLastClickY == 0) { @@ -344,12 +350,14 @@ class Preview : ViewGroup, SurfaceHolder.Callback, MediaScannerConnection.OnScan mParameters!!.focusMode = Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE camera.parameters = mParameters + if (takePictureAfter) { takePicture() } else if (!mIsVideoMode || !mIsRecording) { - Handler().postDelayed({ + autoFocusHandler.removeCallbacksAndMessages(null) + autoFocusHandler.postDelayed({ focusArea(false, false) - }, 3000) + }, REFOCUS_PERIOD) } } } diff --git a/app/src/main/kotlin/com/simplemobiletools/camera/dialogs/ChangeResolutionDialog.kt b/app/src/main/kotlin/com/simplemobiletools/camera/dialogs/ChangeResolutionDialog.kt index c03dc1de..bf60fcf5 100644 --- a/app/src/main/kotlin/com/simplemobiletools/camera/dialogs/ChangeResolutionDialog.kt +++ b/app/src/main/kotlin/com/simplemobiletools/camera/dialogs/ChangeResolutionDialog.kt @@ -5,9 +5,9 @@ import android.support.v7.app.AlertDialog import android.view.LayoutInflater import android.view.View import com.simplemobiletools.camera.Config -import com.simplemobiletools.camera.Preview import com.simplemobiletools.camera.R import com.simplemobiletools.camera.activities.SimpleActivity +import com.simplemobiletools.camera.extensions.config import com.simplemobiletools.camera.extensions.getAspectRatio import com.simplemobiletools.commons.dialogs.RadioGroupDialog import com.simplemobiletools.commons.extensions.setupDialogStuff @@ -16,7 +16,7 @@ import kotlinx.android.synthetic.main.dialog_change_resolution.view.* class ChangeResolutionDialog(val activity: SimpleActivity, val config: Config, val camera: Camera, val callback: () -> Unit) { var dialog: AlertDialog - val isBackCamera = Preview.config.lastUsedCamera == Camera.CameraInfo.CAMERA_FACING_BACK + val isBackCamera = activity.config.lastUsedCamera == Camera.CameraInfo.CAMERA_FACING_BACK init { val view = LayoutInflater.from(activity).inflate(R.layout.dialog_change_resolution, null).apply {