|
| 1 | +// Copyright 2013 The Flutter Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +package io.flutter.plugins.camera; |
| 6 | + |
| 7 | +import android.hardware.camera2.CameraCaptureSession; |
| 8 | +import android.hardware.camera2.CameraCaptureSession.CaptureCallback; |
| 9 | +import android.hardware.camera2.CaptureRequest; |
| 10 | +import android.hardware.camera2.CaptureResult; |
| 11 | +import android.hardware.camera2.TotalCaptureResult; |
| 12 | +import android.util.Log; |
| 13 | +import androidx.annotation.NonNull; |
| 14 | +import io.flutter.plugins.camera.types.CaptureTimeoutsWrapper; |
| 15 | + |
| 16 | +/** |
| 17 | + * A callback object for tracking the progress of a {@link android.hardware.camera2.CaptureRequest} |
| 18 | + * submitted to the camera device. |
| 19 | + */ |
| 20 | +class CameraCaptureCallback extends CaptureCallback { |
| 21 | + private static final String TAG = "CameraCaptureCallback"; |
| 22 | + private final CameraCaptureStateListener cameraStateListener; |
| 23 | + private CameraState cameraState; |
| 24 | + private final CaptureTimeoutsWrapper captureTimeouts; |
| 25 | + |
| 26 | + private CameraCaptureCallback( |
| 27 | + @NonNull CameraCaptureStateListener cameraStateListener, |
| 28 | + @NonNull CaptureTimeoutsWrapper captureTimeouts) { |
| 29 | + cameraState = CameraState.STATE_PREVIEW; |
| 30 | + this.cameraStateListener = cameraStateListener; |
| 31 | + this.captureTimeouts = captureTimeouts; |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * Creates a new instance of the {@link CameraCaptureCallback} class. |
| 36 | + * |
| 37 | + * @param cameraStateListener instance which will be called when the camera state changes. |
| 38 | + * @param captureTimeouts specifying the different timeout counters that should be taken into |
| 39 | + * account. |
| 40 | + * @return a configured instance of the {@link CameraCaptureCallback} class. |
| 41 | + */ |
| 42 | + public static CameraCaptureCallback create( |
| 43 | + @NonNull CameraCaptureStateListener cameraStateListener, |
| 44 | + @NonNull CaptureTimeoutsWrapper captureTimeouts) { |
| 45 | + return new CameraCaptureCallback(cameraStateListener, captureTimeouts); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Gets the current {@link CameraState}. |
| 50 | + * |
| 51 | + * @return the current {@link CameraState}. |
| 52 | + */ |
| 53 | + public CameraState getCameraState() { |
| 54 | + return cameraState; |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Sets the {@link CameraState}. |
| 59 | + * |
| 60 | + * @param state the camera is currently in. |
| 61 | + */ |
| 62 | + public void setCameraState(@NonNull CameraState state) { |
| 63 | + cameraState = state; |
| 64 | + } |
| 65 | + |
| 66 | + private void process(CaptureResult result) { |
| 67 | + Integer aeState = result.get(CaptureResult.CONTROL_AE_STATE); |
| 68 | + Integer afState = result.get(CaptureResult.CONTROL_AF_STATE); |
| 69 | + |
| 70 | + if (cameraState != CameraState.STATE_PREVIEW) { |
| 71 | + Log.d( |
| 72 | + TAG, |
| 73 | + "CameraCaptureCallback | state: " |
| 74 | + + cameraState |
| 75 | + + " | afState: " |
| 76 | + + afState |
| 77 | + + " | aeState: " |
| 78 | + + aeState); |
| 79 | + } |
| 80 | + |
| 81 | + switch (cameraState) { |
| 82 | + case STATE_PREVIEW: |
| 83 | + { |
| 84 | + // We have nothing to do when the camera preview is working normally. |
| 85 | + break; |
| 86 | + } |
| 87 | + case STATE_WAITING_FOCUS: |
| 88 | + { |
| 89 | + if (afState == null) { |
| 90 | + return; |
| 91 | + } else if (afState == CaptureResult.CONTROL_AF_STATE_FOCUSED_LOCKED |
| 92 | + || afState == CaptureResult.CONTROL_AF_STATE_NOT_FOCUSED_LOCKED) { |
| 93 | + handleWaitingFocusState(aeState); |
| 94 | + } else if (captureTimeouts.getPreCaptureFocusing().getIsExpired()) { |
| 95 | + Log.w(TAG, "Focus timeout, moving on with capture"); |
| 96 | + handleWaitingFocusState(aeState); |
| 97 | + } |
| 98 | + |
| 99 | + break; |
| 100 | + } |
| 101 | + case STATE_WAITING_PRECAPTURE_START: |
| 102 | + { |
| 103 | + // CONTROL_AE_STATE can be null on some devices |
| 104 | + if (aeState == null |
| 105 | + || aeState == CaptureResult.CONTROL_AE_STATE_CONVERGED |
| 106 | + || aeState == CaptureResult.CONTROL_AE_STATE_PRECAPTURE |
| 107 | + || aeState == CaptureResult.CONTROL_AE_STATE_FLASH_REQUIRED) { |
| 108 | + setCameraState(CameraState.STATE_WAITING_PRECAPTURE_DONE); |
| 109 | + } else if (captureTimeouts.getPreCaptureMetering().getIsExpired()) { |
| 110 | + Log.w(TAG, "Metering timeout waiting for pre-capture to start, moving on with capture"); |
| 111 | + |
| 112 | + setCameraState(CameraState.STATE_WAITING_PRECAPTURE_DONE); |
| 113 | + } |
| 114 | + break; |
| 115 | + } |
| 116 | + case STATE_WAITING_PRECAPTURE_DONE: |
| 117 | + { |
| 118 | + // CONTROL_AE_STATE can be null on some devices |
| 119 | + if (aeState == null || aeState != CaptureResult.CONTROL_AE_STATE_PRECAPTURE) { |
| 120 | + cameraStateListener.onConverged(); |
| 121 | + } else if (captureTimeouts.getPreCaptureMetering().getIsExpired()) { |
| 122 | + Log.w( |
| 123 | + TAG, "Metering timeout waiting for pre-capture to finish, moving on with capture"); |
| 124 | + cameraStateListener.onConverged(); |
| 125 | + } |
| 126 | + |
| 127 | + break; |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + private void handleWaitingFocusState(Integer aeState) { |
| 133 | + // CONTROL_AE_STATE can be null on some devices |
| 134 | + if (aeState == null || aeState == CaptureRequest.CONTROL_AE_STATE_CONVERGED) { |
| 135 | + cameraStateListener.onConverged(); |
| 136 | + } else { |
| 137 | + cameraStateListener.onPrecapture(); |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + @Override |
| 142 | + public void onCaptureProgressed( |
| 143 | + @NonNull CameraCaptureSession session, |
| 144 | + @NonNull CaptureRequest request, |
| 145 | + @NonNull CaptureResult partialResult) { |
| 146 | + process(partialResult); |
| 147 | + } |
| 148 | + |
| 149 | + @Override |
| 150 | + public void onCaptureCompleted( |
| 151 | + @NonNull CameraCaptureSession session, |
| 152 | + @NonNull CaptureRequest request, |
| 153 | + @NonNull TotalCaptureResult result) { |
| 154 | + process(result); |
| 155 | + } |
| 156 | + |
| 157 | + /** An interface that describes the different state changes implementers can be informed about. */ |
| 158 | + interface CameraCaptureStateListener { |
| 159 | + |
| 160 | + /** Called when the {@link android.hardware.camera2.CaptureRequest} has been converged. */ |
| 161 | + void onConverged(); |
| 162 | + |
| 163 | + /** |
| 164 | + * Called when the {@link android.hardware.camera2.CaptureRequest} enters the pre-capture state. |
| 165 | + */ |
| 166 | + void onPrecapture(); |
| 167 | + } |
| 168 | +} |
0 commit comments