我想制作一个简单的应用程序来捕捉延时摄影。最后我做到了。但我想知道是否可以在第一张照片上只锁定一次焦点?然后使用保存的关于焦点的值(如果可能的话)?一般来说,这是一个好主意吗?我认为每隔几秒钟打电话lockFocus()
太贵了。当设备固定放置时,一动不动。
其实我有类似谷歌示例代码的代码。我正在将CaptureRequest.Builder
字段设置CONTROL_AF_TRIGGER
为CONTROL_AF_TRIGGER_START
并且CaptureCallback
我正在检查CaptureResult.CONTROL_AF_STATE
,如果ok
然后捕获最终照片。也许进入第一张照片,LENS_FOCUS_DISTANCE
然后以某种方式设置此值。
PS:此外,我没有向用户显示预览,整个相机逻辑正在服务和其他线程中运行。我也实施ImageReader
了等等。
我的代码是:
// before is `CameraManager`, setting outputs, opening camera - all ok
public void makePhoto() { // this I call from outer
// plus some observer, not important conditions for this issue
createCameraPreviewSession();
}
private void createCameraPreviewSession() {
mPreviewRequestBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
mPreviewRequestBuilder.addTarget(previewSurface);
mCameraDevice.createCaptureSession(Arrays.asList(previewSurface, mImageReader.getSurface()), new CameraCaptureSession.StateCallback() {
public void onConfigured(CameraCaptureSession session) {
mCaptureSession = session;
mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE);
mPreviewRequest = mPreviewRequestBuilder.build();
startedRepeatedRequest = false; // simple flag to start lockFocus after below request has started, once
mCaptureSession.setRepeatingRequest(mPreviewRequest, mCaptureCallback, null);
}
}
}
private CameraCaptureSession.CaptureCallback mCaptureCallback = new CameraCaptureSession.CaptureCallback() {
public void onCaptureProgressed(CameraCaptureSession session, CaptureRequest request, CaptureResult partialResult) {
process(partialResult);
}
private void process(CaptureResult result) {
if(!startedRepeatedRequest) {
// this is called only once, on the beginning of session
lockFocus();
startedRepeatedRequest = true;
}
switch(mState) {
case STATE_PREVIEW:
break;
case STATE_WAITING_LOCK:
Integer afState = result.get(CaptureResult.CONTROL_AF_STATE);
if (afState == null) {
captureStillPicture();
} else if (CaptureResult.CONTROL_AF_STATE_FOCUSED_LOCKED == afState || CaptureResult.CONTROL_AF_STATE_NOT_FOCUSED_LOCKED == afState) {
Integer aeState = result.get(CaptureResult.CONTROL_AE_STATE);
if (aeState == null || aeState == CaptureResult.CONTROL_AE_STATE_CONVERGED) {
mState = STATE_PICTURE_TAKEN;
captureStillPicture(); // this leads to final capture photo
} else {
runPrecaptureSequence();
}
}
break;
// There are also flags for STATE_PRECAPTURE, and STATE_WAITING_NON_PRECAPTURE but this doesn't matter here
}
}
}
private void lockFocus() {
mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER, CameraMetadata.CONTROL_AF_TRIGGER_START);
mState = STATE_WAITING_LOCK;
mCaptureSession.capture(mPreviewRequestBuilder.build(), mCaptureCallback, null);
}
private void captureStillPicture() {
final CaptureRequest.Builder captureBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE);
Surface surfaceTarget = mImageReader.getSurface();
captureBuilder.addTarget(surfaceTarget);
captureBuilder.set(CaptureRequest.JPEG_QUALITY, (byte) 90);
captureBuilder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE);
// callback for final capture
CameraCaptureSession.CaptureCallback CaptureCallback = new CameraCaptureSession.CaptureCallback() {
mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER, CameraMetadata.CONTROL_AF_TRIGGER_CANCEL);
mCaptureSession.capture(mPreviewRequestBuilder.build(), mCaptureCallback, null);
mState = STATE_PREVIEW;
session.close();
}
mCaptureSession.stopRepeating();
mCaptureSession.capture(captureBuilder.build(), CaptureCallback, null);
}