如何检测相机何时完成曝光?

时间:2014-12-10 10:23:52

标签: android camera android-camera

背景

当我开始相机预览时,特别是在室内时,使用takePicture拍摄的预览帧和照片最初是黑暗的。在下一秒钟,相机的曝光将自动调整,直到曝光最佳。当我在手机上打开相机应用程序时也会出现这种情况。

问题

我试图在曝光正确后尽快拍照。优选地,我可以在曝光良好时注册要调用的回调。我怎么能这样做?

我尝试了什么

  • 将相机的曝光补偿配置为最大值。 (没有帮助;在相机预览开始后,它仍需要大约一秒才能正常曝光。)
  • 自动对焦API,但由于相机不支持自动对焦,因此无法正常工作。
  • 硬编码延迟一秒钟以等待曝光。这不是最理想的,因为在户外日光下,不需要这么多时间。

1 个答案:

答案 0 :(得分:1)

旧版相机API

不幸的是,具有旧版相机驱动程序的设备似乎不支持此功能。因此,最好的选择似乎是开始预览,然后等待大约一秒钟。

camera2 API

使用camera2 API,您可以通过检查CaptureResult.CONTROL_AE_STATE的值来检查自动曝光状态。

示例代码

首先开始捕获:

// Auto-exposure only seems to start after the first picture has been taken
// And on the Pixel 3 XL, it finishes fastest when you make a lot of captures
captureSession.setRepeatingRequest(request, captureCallback, null);

并使用以下样板代码监控自动曝光:

private CaptureResult firstResult;

private final CameraCaptureSession.CaptureCallback captureCallback = new CameraCaptureSession.CaptureCallback() {

    @Override
    public void onCaptureCompleted(@NonNull CameraCaptureSession session, @NonNull CaptureRequest request, @NonNull TotalCaptureResult result) {
        super.onCaptureCompleted(session, request, result);

        if (firstResult == null)
            firstResult = result;

        boolean aeAcquired;
        Integer aeState = lastResult.get(CaptureResult.CONTROL_AE_STATE);
        if (aeState == null) {
            // This camera doesn't support monitoring of auto-exposure, so we'll just have to wait a bit and then assume it's adjusted.
            long nanosSinceFirstResult = result.get(CaptureResult.SENSOR_TIMESTAMP) - firstResult.get(CaptureResult.SENSOR_TIMESTAMP);
            long millisSinceFirstResult = nanosSinceFirstResult / 1000 / 1000;
            aeAcquired = millisSinceFirstResult >= 750;
        } else {
            aeAcquired = aeState == CameraMetadata.CONTROL_AE_STATE_CONVERGED || aeState == CameraMetadata.CONTROL_AE_STATE_FLASH_REQUIRED;
        }

        if (aeAcquired) {
            // Auto-exposure has finished
        }   
    }

};