fix some issues with camera stucking at focus stage

This commit is contained in:
tibbi 2017-06-16 21:01:03 +02:00
parent adca5e743c
commit 7acdcd60e6
3 changed files with 50 additions and 42 deletions

View File

@ -8,9 +8,9 @@ import android.net.Uri
import android.os.AsyncTask import android.os.AsyncTask
import android.os.Environment import android.os.Environment
import android.util.Log import android.util.Log
import com.simplemobiletools.camera.Preview.Companion.config
import com.simplemobiletools.camera.activities.MainActivity import com.simplemobiletools.camera.activities.MainActivity
import com.simplemobiletools.camera.extensions.compensateDeviceRotation import com.simplemobiletools.camera.extensions.compensateDeviceRotation
import com.simplemobiletools.camera.extensions.config
import com.simplemobiletools.camera.extensions.getOutputMediaFile import com.simplemobiletools.camera.extensions.getOutputMediaFile
import com.simplemobiletools.camera.extensions.getPreviewRotation import com.simplemobiletools.camera.extensions.getPreviewRotation
import com.simplemobiletools.commons.extensions.getFileDocument 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 data = params[0]
val photoFile = File(path) val photoFile = File(path)
if (activity.needsStupidWritePermissions(path)) { if (activity.needsStupidWritePermissions(path)) {
if (config.treeUri.isEmpty()) { if (activity.config.treeUri.isEmpty()) {
activity.runOnUiThread { activity.runOnUiThread {
activity.toast(R.string.save_error_internal_storage) 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 "" return ""
} }
var document = activity.getFileDocument(path) var document = activity.getFileDocument(path)

View File

@ -23,42 +23,43 @@ import java.io.IOException
import java.util.* import java.util.*
class Preview : ViewGroup, SurfaceHolder.Callback, MediaScannerConnection.OnScanCompletedListener { class Preview : ViewGroup, SurfaceHolder.Callback, MediaScannerConnection.OnScanCompletedListener {
companion object { var mCamera: Camera? = null
var mCamera: Camera? = null private val TAG = Preview::class.java.simpleName
private val TAG = Preview::class.java.simpleName private val FOCUS_AREA_SIZE = 100
private val FOCUS_AREA_SIZE = 100 private val PHOTO_PREVIEW_LENGTH = 500L
private val PHOTO_PREVIEW_LENGTH = 500L private val REFOCUS_PERIOD = 3000L
lateinit var mSurfaceHolder: SurfaceHolder lateinit var mSurfaceHolder: SurfaceHolder
lateinit var mSurfaceView: SurfaceView lateinit var mSurfaceView: SurfaceView
lateinit var mActivity: MainActivity lateinit var mActivity: MainActivity
lateinit var mCallback: PreviewListener lateinit var mCallback: PreviewListener
lateinit var mScreenSize: Point lateinit var mScreenSize: Point
lateinit var config: Config lateinit var config: Config
private var mSupportedPreviewSizes: List<Camera.Size>? = null private var mSupportedPreviewSizes: List<Camera.Size>? = null
private var mPreviewSize: Camera.Size? = null private var mPreviewSize: Camera.Size? = null
private var mParameters: Camera.Parameters? = null private var mParameters: Camera.Parameters? = null
private var mRecorder: MediaRecorder? = null private var mRecorder: MediaRecorder? = null
private var mTargetUri: Uri? = null private var mTargetUri: Uri? = null
private var mScaleGestureDetector: ScaleGestureDetector? = null private var mScaleGestureDetector: ScaleGestureDetector? = null
private var mZoomRatios: List<Int>? = null private var mZoomRatios: List<Int>? = null
private var mCurrVideoPath = "" private var mCurrVideoPath = ""
private var mCanTakePicture = false private var mCanTakePicture = false
private var mIsRecording = false private var mIsRecording = false
private var mIsVideoMode = false private var mIsVideoMode = false
private var mIsSurfaceCreated = false private var mIsSurfaceCreated = false
private var mSwitchToVideoAsap = false private var mSwitchToVideoAsap = false
private var mSetupPreviewAfterMeasure = false private var mSetupPreviewAfterMeasure = false
private var mIsSixteenToNine = false private var mIsSixteenToNine = false
private var mWasZooming = false private var mWasZooming = false
private var mIsPreviewShown = false private var mIsPreviewShown = false
private var mLastClickX = 0 private var mLastClickX = 0
private var mLastClickY = 0 private var mLastClickY = 0
private var mCurrCameraId = 0 private var mCurrCameraId = 0
private var mMaxZoom = 0 private var mMaxZoom = 0
private var mRotationAtCapture = 0 private var mRotationAtCapture = 0
} private var mIsFocusing = false
private var autoFocusHandler = Handler()
constructor(context: Context) : super(context) constructor(context: Context) : super(context)
@ -292,6 +293,7 @@ class Preview : ViewGroup, SurfaceHolder.Callback, MediaScannerConnection.OnScan
mCamera!!.takePicture(null, null, takePictureCallback) mCamera!!.takePicture(null, null, takePictureCallback)
} }
mCanTakePicture = false mCanTakePicture = false
mIsFocusing = false
} }
private val takePictureCallback = Camera.PictureCallback { data, cam -> private val takePictureCallback = Camera.PictureCallback { data, cam ->
@ -315,12 +317,16 @@ class Preview : ViewGroup, SurfaceHolder.Callback, MediaScannerConnection.OnScan
mActivity.toggleBottomButtons(false) mActivity.toggleBottomButtons(false)
mCamera?.startPreview() mCamera?.startPreview()
mCanTakePicture = true mCanTakePicture = true
focusArea(false, false)
} }
private fun focusArea(takePictureAfter: Boolean, showFocusRect: Boolean = true) { private fun focusArea(takePictureAfter: Boolean, showFocusRect: Boolean = true) {
if (mCamera == null) if (mCamera == null || (mIsFocusing && !takePictureAfter))
return return
if (takePictureAfter)
mIsFocusing = true
mCamera!!.cancelAutoFocus() mCamera!!.cancelAutoFocus()
if (mParameters!!.maxNumFocusAreas > 0) { if (mParameters!!.maxNumFocusAreas > 0) {
if (mLastClickX == 0 && mLastClickY == 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 mParameters!!.focusMode = Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE
camera.parameters = mParameters camera.parameters = mParameters
if (takePictureAfter) { if (takePictureAfter) {
takePicture() takePicture()
} else if (!mIsVideoMode || !mIsRecording) { } else if (!mIsVideoMode || !mIsRecording) {
Handler().postDelayed({ autoFocusHandler.removeCallbacksAndMessages(null)
autoFocusHandler.postDelayed({
focusArea(false, false) focusArea(false, false)
}, 3000) }, REFOCUS_PERIOD)
} }
} }
} }

View File

@ -5,9 +5,9 @@ import android.support.v7.app.AlertDialog
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
import com.simplemobiletools.camera.Config import com.simplemobiletools.camera.Config
import com.simplemobiletools.camera.Preview
import com.simplemobiletools.camera.R import com.simplemobiletools.camera.R
import com.simplemobiletools.camera.activities.SimpleActivity import com.simplemobiletools.camera.activities.SimpleActivity
import com.simplemobiletools.camera.extensions.config
import com.simplemobiletools.camera.extensions.getAspectRatio import com.simplemobiletools.camera.extensions.getAspectRatio
import com.simplemobiletools.commons.dialogs.RadioGroupDialog import com.simplemobiletools.commons.dialogs.RadioGroupDialog
import com.simplemobiletools.commons.extensions.setupDialogStuff 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) { class ChangeResolutionDialog(val activity: SimpleActivity, val config: Config, val camera: Camera, val callback: () -> Unit) {
var dialog: AlertDialog var dialog: AlertDialog
val isBackCamera = Preview.config.lastUsedCamera == Camera.CameraInfo.CAMERA_FACING_BACK val isBackCamera = activity.config.lastUsedCamera == Camera.CameraInfo.CAMERA_FACING_BACK
init { init {
val view = LayoutInflater.from(activity).inflate(R.layout.dialog_change_resolution, null).apply { val view = LayoutInflater.from(activity).inflate(R.layout.dialog_change_resolution, null).apply {