当我开始相机预览时,特别是在室内时,使用takePicture
拍摄的预览帧和照片最初是黑暗的。在下一秒钟,相机的曝光将自动调整,直到曝光最佳。当我在手机上打开相机应用程序时也会出现这种情况。
我试图在曝光正确后尽快拍照。优选地,我可以在曝光良好时注册要调用的回调。我怎么能这样做?
答案 0 :(得分:1)
不幸的是,具有旧版相机驱动程序的设备似乎不支持此功能。因此,最好的选择似乎是开始预览,然后等待大约一秒钟。
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
}
}
};