问题
I want to make simple app that is making captures for timelapse. Finally I have made it. But I am wondering whether it is possible to lock focus only once, on first photo? And later use that saved value about focus (if possible)? Generally it is a good idea? I thought that every several seconds calling lockFocus()
is too expensive. While device is placed fixed, motionless.
Actually I have code like to google sample code. I am setting CaptureRequest.Builder
field CONTROL_AF_TRIGGER
to CONTROL_AF_TRIGGER_START
and in CaptureCallback
I am checking for CaptureResult.CONTROL_AF_STATE
, if ok
then capture final photo. Maybe get in first photo the LENS_FOCUS_DISTANCE
and later somehow set this value.
PS: Additionally I don't show preview to user and whole camera logic is running in Service and in other thread. Also I have implemented ImageReader
and more.
My code is:
// 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);
}
来源:https://stackoverflow.com/questions/41128863/android-set-camera2-focus-only-once-on-beginning-of-timelapse