Skip to content

Commit

Permalink
[camera] Added timeout to Android pre-capture sequence (flutter#3558)
Browse files Browse the repository at this point in the history
* Added timeout for waiting pre-capture state Android

* Fix analysis warning and bumped version
  • Loading branch information
mvanbeusekom authored and gabeschine committed Apr 21, 2021
1 parent d6ceb4c commit 7a48941
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
9 changes: 9 additions & 0 deletions packages/camera/camera/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@

## 0.7.0+4

* Fix crash when taking picture with orientation lock

## 0.7.0+3

* Clockwise rotation of focus point in android

## 0.7.0+2

* Fix example reference in README.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import android.os.Build.VERSION_CODES;
import android.os.Handler;
import android.os.Looper;
import android.os.SystemClock;
import android.util.Log;
import android.util.Range;
import android.util.Rational;
Expand Down Expand Up @@ -73,6 +74,9 @@ interface ErrorCallback {
public class Camera {
private static final String TAG = "Camera";

/** Timeout for the pre-capture sequence. */
private static final long PRECAPTURE_TIMEOUT_MS = 1000;

private final SurfaceTextureEntry flutterTexture;
private final CameraManager cameraManager;
private final DeviceOrientationManager deviceOrientationListener;
Expand Down Expand Up @@ -105,6 +109,7 @@ public class Camera {
private boolean useAutoFocus = true;
private Range<Integer> fpsRange;
private PlatformChannel.DeviceOrientation lockedCaptureOrientation;
private long preCaptureStartTime;

private static final HashMap<String, Integer> supportedImageFormats;
// Current supported outputs
Expand Down Expand Up @@ -503,11 +508,16 @@ private void processCapture(CaptureResult result) {
|| aeState == CaptureRequest.CONTROL_AE_STATE_FLASH_REQUIRED
|| aeState == CaptureRequest.CONTROL_AE_STATE_CONVERGED) {
pictureCaptureRequest.setState(State.waitingPreCaptureReady);
setPreCaptureStartTime();
}
break;
case waitingPreCaptureReady:
if (aeState == null || aeState != CaptureRequest.CONTROL_AE_STATE_PRECAPTURE) {
runPictureCapture();
} else {
if (hitPreCaptureTimeout()) {
unlockAutoFocus();
}
}
}
}
Expand Down Expand Up @@ -1142,6 +1152,20 @@ public void stopImageStream() throws CameraAccessException {
startPreview();
}

/** Sets the time the pre-capture sequence started. */
private void setPreCaptureStartTime() {
preCaptureStartTime = SystemClock.elapsedRealtime();
}

/**
* Check if the timeout for the pre-capture sequence has been reached.
*
* @return true if the timeout is reached; otherwise false is returned.
*/
private boolean hitPreCaptureTimeout() {
return (SystemClock.elapsedRealtime() - preCaptureStartTime) > PRECAPTURE_TIMEOUT_MS;
}

private void closeCaptureSession() {
if (cameraCaptureSession != null) {
cameraCaptureSession.close();
Expand Down
4 changes: 2 additions & 2 deletions packages/camera/camera/test/camera_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1213,8 +1213,8 @@ class MockCameraPlatform extends Mock
@override
Future<int> createCamera(
CameraDescription description,
ResolutionPreset resolutionPreset, {
bool enableAudio,
ResolutionPreset? resolutionPreset, {
bool enableAudio = false,
}) =>
mockPlatformException
? throw PlatformException(code: 'foo', message: 'bar')
Expand Down

0 comments on commit 7a48941

Please sign in to comment.