Skip to content

Commit 7acdcd6

Browse files
committed
fix some issues with camera stucking at focus stage
1 parent adca5e7 commit 7acdcd6

3 files changed

Lines changed: 52 additions & 44 deletions

File tree

app/src/main/kotlin/com/simplemobiletools/camera/PhotoProcessor.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ import android.net.Uri
88
import android.os.AsyncTask
99
import android.os.Environment
1010
import android.util.Log
11-
import com.simplemobiletools.camera.Preview.Companion.config
1211
import com.simplemobiletools.camera.activities.MainActivity
1312
import com.simplemobiletools.camera.extensions.compensateDeviceRotation
13+
import com.simplemobiletools.camera.extensions.config
1414
import com.simplemobiletools.camera.extensions.getOutputMediaFile
1515
import com.simplemobiletools.camera.extensions.getPreviewRotation
1616
import com.simplemobiletools.commons.extensions.getFileDocument
@@ -49,11 +49,11 @@ class PhotoProcessor(val activity: MainActivity, val uri: Uri?, val currCameraId
4949
val data = params[0]
5050
val photoFile = File(path)
5151
if (activity.needsStupidWritePermissions(path)) {
52-
if (config.treeUri.isEmpty()) {
52+
if (activity.config.treeUri.isEmpty()) {
5353
activity.runOnUiThread {
5454
activity.toast(R.string.save_error_internal_storage)
5555
}
56-
config.savePhotosFolder = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).toString()
56+
activity.config.savePhotosFolder = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).toString()
5757
return ""
5858
}
5959
var document = activity.getFileDocument(path)

app/src/main/kotlin/com/simplemobiletools/camera/Preview.kt

Lines changed: 47 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -23,42 +23,43 @@ import java.io.IOException
2323
import java.util.*
2424

2525
class Preview : ViewGroup, SurfaceHolder.Callback, MediaScannerConnection.OnScanCompletedListener {
26-
companion object {
27-
var mCamera: Camera? = null
28-
private val TAG = Preview::class.java.simpleName
29-
private val FOCUS_AREA_SIZE = 100
30-
private val PHOTO_PREVIEW_LENGTH = 500L
31-
32-
lateinit var mSurfaceHolder: SurfaceHolder
33-
lateinit var mSurfaceView: SurfaceView
34-
lateinit var mActivity: MainActivity
35-
lateinit var mCallback: PreviewListener
36-
lateinit var mScreenSize: Point
37-
lateinit var config: Config
38-
private var mSupportedPreviewSizes: List<Camera.Size>? = null
39-
private var mPreviewSize: Camera.Size? = null
40-
private var mParameters: Camera.Parameters? = null
41-
private var mRecorder: MediaRecorder? = null
42-
private var mTargetUri: Uri? = null
43-
private var mScaleGestureDetector: ScaleGestureDetector? = null
44-
private var mZoomRatios: List<Int>? = null
45-
46-
private var mCurrVideoPath = ""
47-
private var mCanTakePicture = false
48-
private var mIsRecording = false
49-
private var mIsVideoMode = false
50-
private var mIsSurfaceCreated = false
51-
private var mSwitchToVideoAsap = false
52-
private var mSetupPreviewAfterMeasure = false
53-
private var mIsSixteenToNine = false
54-
private var mWasZooming = false
55-
private var mIsPreviewShown = false
56-
private var mLastClickX = 0
57-
private var mLastClickY = 0
58-
private var mCurrCameraId = 0
59-
private var mMaxZoom = 0
60-
private var mRotationAtCapture = 0
61-
}
26+
var mCamera: Camera? = null
27+
private val TAG = Preview::class.java.simpleName
28+
private val FOCUS_AREA_SIZE = 100
29+
private val PHOTO_PREVIEW_LENGTH = 500L
30+
private val REFOCUS_PERIOD = 3000L
31+
32+
lateinit var mSurfaceHolder: SurfaceHolder
33+
lateinit var mSurfaceView: SurfaceView
34+
lateinit var mActivity: MainActivity
35+
lateinit var mCallback: PreviewListener
36+
lateinit var mScreenSize: Point
37+
lateinit var config: Config
38+
private var mSupportedPreviewSizes: List<Camera.Size>? = null
39+
private var mPreviewSize: Camera.Size? = null
40+
private var mParameters: Camera.Parameters? = null
41+
private var mRecorder: MediaRecorder? = null
42+
private var mTargetUri: Uri? = null
43+
private var mScaleGestureDetector: ScaleGestureDetector? = null
44+
private var mZoomRatios: List<Int>? = null
45+
46+
private var mCurrVideoPath = ""
47+
private var mCanTakePicture = false
48+
private var mIsRecording = false
49+
private var mIsVideoMode = false
50+
private var mIsSurfaceCreated = false
51+
private var mSwitchToVideoAsap = false
52+
private var mSetupPreviewAfterMeasure = false
53+
private var mIsSixteenToNine = false
54+
private var mWasZooming = false
55+
private var mIsPreviewShown = false
56+
private var mLastClickX = 0
57+
private var mLastClickY = 0
58+
private var mCurrCameraId = 0
59+
private var mMaxZoom = 0
60+
private var mRotationAtCapture = 0
61+
private var mIsFocusing = false
62+
private var autoFocusHandler = Handler()
6263

6364
constructor(context: Context) : super(context)
6465

@@ -292,6 +293,7 @@ class Preview : ViewGroup, SurfaceHolder.Callback, MediaScannerConnection.OnScan
292293
mCamera!!.takePicture(null, null, takePictureCallback)
293294
}
294295
mCanTakePicture = false
296+
mIsFocusing = false
295297
}
296298

297299
private val takePictureCallback = Camera.PictureCallback { data, cam ->
@@ -315,12 +317,16 @@ class Preview : ViewGroup, SurfaceHolder.Callback, MediaScannerConnection.OnScan
315317
mActivity.toggleBottomButtons(false)
316318
mCamera?.startPreview()
317319
mCanTakePicture = true
320+
focusArea(false, false)
318321
}
319322

320323
private fun focusArea(takePictureAfter: Boolean, showFocusRect: Boolean = true) {
321-
if (mCamera == null)
324+
if (mCamera == null || (mIsFocusing && !takePictureAfter))
322325
return
323326

327+
if (takePictureAfter)
328+
mIsFocusing = true
329+
324330
mCamera!!.cancelAutoFocus()
325331
if (mParameters!!.maxNumFocusAreas > 0) {
326332
if (mLastClickX == 0 && mLastClickY == 0) {
@@ -344,12 +350,14 @@ class Preview : ViewGroup, SurfaceHolder.Callback, MediaScannerConnection.OnScan
344350
mParameters!!.focusMode = Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE
345351

346352
camera.parameters = mParameters
353+
347354
if (takePictureAfter) {
348355
takePicture()
349356
} else if (!mIsVideoMode || !mIsRecording) {
350-
Handler().postDelayed({
357+
autoFocusHandler.removeCallbacksAndMessages(null)
358+
autoFocusHandler.postDelayed({
351359
focusArea(false, false)
352-
}, 3000)
360+
}, REFOCUS_PERIOD)
353361
}
354362
}
355363
}

app/src/main/kotlin/com/simplemobiletools/camera/dialogs/ChangeResolutionDialog.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import android.support.v7.app.AlertDialog
55
import android.view.LayoutInflater
66
import android.view.View
77
import com.simplemobiletools.camera.Config
8-
import com.simplemobiletools.camera.Preview
98
import com.simplemobiletools.camera.R
109
import com.simplemobiletools.camera.activities.SimpleActivity
10+
import com.simplemobiletools.camera.extensions.config
1111
import com.simplemobiletools.camera.extensions.getAspectRatio
1212
import com.simplemobiletools.commons.dialogs.RadioGroupDialog
1313
import com.simplemobiletools.commons.extensions.setupDialogStuff
@@ -16,7 +16,7 @@ import kotlinx.android.synthetic.main.dialog_change_resolution.view.*
1616

1717
class ChangeResolutionDialog(val activity: SimpleActivity, val config: Config, val camera: Camera, val callback: () -> Unit) {
1818
var dialog: AlertDialog
19-
val isBackCamera = Preview.config.lastUsedCamera == Camera.CameraInfo.CAMERA_FACING_BACK
19+
val isBackCamera = activity.config.lastUsedCamera == Camera.CameraInfo.CAMERA_FACING_BACK
2020

2121
init {
2222
val view = LayoutInflater.from(activity).inflate(R.layout.dialog_change_resolution, null).apply {

0 commit comments

Comments
 (0)