@@ -7,6 +7,7 @@ import android.graphics.SurfaceTexture
7
7
import android.hardware.camera2.*
8
8
import android.media.ImageReader
9
9
import android.support.annotation.RequiresApi
10
+ import android.util.Log
10
11
import android.view.Surface
11
12
import com.camerakit.api.CameraApi
12
13
import com.camerakit.api.CameraAttributes
@@ -39,6 +40,7 @@ class Camera2(eventsDelegate: CameraEvents, context: Context) :
39
40
private var flash: CameraFlash = CameraFlash .OFF
40
41
private var previewStarted = false
41
42
private var cameraFacing: CameraFacing = CameraFacing .BACK
43
+ private var waitingFrames: Int = 0
42
44
43
45
@Synchronized
44
46
override fun open (facing : CameraFacing ) {
@@ -57,13 +59,14 @@ class Camera2(eventsDelegate: CameraEvents, context: Context) :
57
59
override fun onDisconnected (cameraDevice : CameraDevice ) {
58
60
cameraDevice.close()
59
61
this @Camera2.cameraDevice = null
62
+ this @Camera2.captureSession = null
60
63
onCameraClosed()
61
64
}
62
65
63
66
override fun onError (cameraDevice : CameraDevice , error : Int ) {
64
67
cameraDevice.close()
65
68
this @Camera2.cameraDevice = null
66
-
69
+ this @Camera2.captureSession = null
67
70
}
68
71
}, cameraHandler)
69
72
}
@@ -117,10 +120,14 @@ class Camera2(eventsDelegate: CameraEvents, context: Context) :
117
120
val captureSession = captureSession
118
121
this .captureSession = null
119
122
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
+ }
124
131
}
125
132
previewStarted = false
126
133
}
@@ -150,10 +157,14 @@ class Camera2(eventsDelegate: CameraEvents, context: Context) :
150
157
val previewRequestBuilder = previewRequestBuilder
151
158
val captureSession = captureSession
152
159
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
+ }
157
168
}
158
169
}
159
170
@@ -236,6 +247,13 @@ class Camera2(eventsDelegate: CameraEvents, context: Context) :
236
247
val afState = result.get(CaptureResult .CONTROL_AF_STATE )
237
248
if (CaptureResult .CONTROL_AF_STATE_FOCUSED_LOCKED == afState || CaptureResult .CONTROL_AF_STATE_NOT_FOCUSED_LOCKED == afState) {
238
249
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++
239
257
}
240
258
}
241
259
STATE_WAITING_PRECAPTURE -> {
0 commit comments