@@ -7,6 +7,7 @@ import android.graphics.SurfaceTexture
77import android.hardware.camera2.*
88import android.media.ImageReader
99import android.support.annotation.RequiresApi
10+ import android.util.Log
1011import android.view.Surface
1112import com.camerakit.api.CameraApi
1213import com.camerakit.api.CameraAttributes
@@ -39,6 +40,7 @@ class Camera2(eventsDelegate: CameraEvents, context: Context) :
3940 private var flash: CameraFlash = CameraFlash .OFF
4041 private var previewStarted = false
4142 private var cameraFacing: CameraFacing = CameraFacing .BACK
43+ private var waitingFrames: Int = 0
4244
4345 @Synchronized
4446 override fun open (facing : CameraFacing ) {
@@ -57,13 +59,14 @@ class Camera2(eventsDelegate: CameraEvents, context: Context) :
5759 override fun onDisconnected (cameraDevice : CameraDevice ) {
5860 cameraDevice.close()
5961 this @Camera2.cameraDevice = null
62+ this @Camera2.captureSession = null
6063 onCameraClosed()
6164 }
6265
6366 override fun onError (cameraDevice : CameraDevice , error : Int ) {
6467 cameraDevice.close()
6568 this @Camera2.cameraDevice = null
66-
69+ this @Camera2.captureSession = null
6770 }
6871 }, cameraHandler)
6972 }
@@ -117,10 +120,14 @@ class Camera2(eventsDelegate: CameraEvents, context: Context) :
117120 val captureSession = captureSession
118121 this .captureSession = null
119122 if (captureSession != null ) {
120- captureSession.stopRepeating()
121- captureSession.abortCaptures()
122- captureSession.close()
123- onPreviewStopped()
123+ try {
124+ captureSession.stopRepeating()
125+ captureSession.abortCaptures()
126+ captureSession.close()
127+ } catch (e: Exception ) {
128+ } finally {
129+ onPreviewStopped()
130+ }
124131 }
125132 previewStarted = false
126133 }
@@ -150,10 +157,14 @@ class Camera2(eventsDelegate: CameraEvents, context: Context) :
150157 val previewRequestBuilder = previewRequestBuilder
151158 val captureSession = captureSession
152159 if (previewRequestBuilder != null && captureSession != null ) {
153- previewRequestBuilder.set(CaptureRequest .CONTROL_AF_TRIGGER , CameraMetadata .CONTROL_AF_TRIGGER_START )
154- captureState = STATE_WAITING_LOCK
155- captureSession.capture(previewRequestBuilder.build(), captureCallback, cameraHandler)
156- previewRequestBuilder.set(CaptureRequest .CONTROL_AF_TRIGGER , null )
160+ try {
161+ previewRequestBuilder.set(CaptureRequest .CONTROL_AF_TRIGGER , CameraMetadata .CONTROL_AF_TRIGGER_START )
162+ captureState = STATE_WAITING_LOCK
163+ waitingFrames = 0
164+ captureSession.capture(previewRequestBuilder.build(), captureCallback, cameraHandler)
165+ previewRequestBuilder.set(CaptureRequest .CONTROL_AF_TRIGGER , null )
166+ } catch (e: Exception ) {
167+ }
157168 }
158169 }
159170
@@ -236,6 +247,13 @@ class Camera2(eventsDelegate: CameraEvents, context: Context) :
236247 val afState = result.get(CaptureResult .CONTROL_AF_STATE )
237248 if (CaptureResult .CONTROL_AF_STATE_FOCUSED_LOCKED == afState || CaptureResult .CONTROL_AF_STATE_NOT_FOCUSED_LOCKED == afState) {
238249 runPreCaptureSequence()
250+ } else if (null == afState || CaptureResult .CONTROL_AF_STATE_INACTIVE == afState) {
251+ captureStillPicture()
252+ } else if (waitingFrames >= 5 ) {
253+ waitingFrames = 0
254+ captureStillPicture()
255+ } else {
256+ waitingFrames++
239257 }
240258 }
241259 STATE_WAITING_PRECAPTURE -> {
0 commit comments