我的应用使用Camera2 API尽快拍摄帧。我们有一个开始/停止按钮来开始/停止捕获帧。焦点要么设置为自动,要么在捕获之前完成。如果在捕获之前完成了焦点,那么在捕获期间必须禁用它。
我用它来成为焦点:
public void focusAt(Point point) {
if(mCanAe || mCanAf) {
int areaSize = 200;
int ll = ((point.x * mActiveArraySize.right) - areaSize) / mTextureView.getWidth();
int rr = ((point.y * mActiveArraySize.bottom) - areaSize) / mTextureView.getHeight();
int focusLeft = clamp(ll, 0, mActiveArraySize.right);
int focusBottom = clamp(rr, 0, mActiveArraySize.bottom);
Rect newRect = new Rect(focusLeft, focusBottom, focusLeft + areaSize, focusBottom + areaSize);
MeteringRectangle meteringRectangle = new MeteringRectangle(newRect, 1);
final MeteringRectangle[] meteringRectangleArr = { meteringRectangle };
mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER, CameraMetadata.CONTROL_AF_TRIGGER_CANCEL);
try {
mCaptureSession.capture(mPreviewRequestBuilder.build(), new CameraCaptureSession.CaptureCallback() {
@Override
public void onCaptureCompleted(@NonNull CameraCaptureSession session, @NonNull CaptureRequest request, @NonNull TotalCaptureResult result) {
if (mCanAe) {
mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AE_REGIONS, meteringRectangleArr );
}
if (mCanAf) {
mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_REGIONS, meteringRectangleArr );
mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_AUTO);
mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER, CameraMetadata.CONTROL_AF_TRIGGER_START);
}
try {
mCaptureSession.setRepeatingRequest(mPreviewRequestBuilder.build(), null, mBackgroundHandler);
}
catch (CameraAccessException e) {
e.printStackTrace();
}
}
}, mBackgroundHandler);
}
catch (CameraAccessException e) { e.printStackTrace(); }
}
}
在捕获过程中无法调用此函数。我认为这已经足够了,但有时在捕捉过程中,Camera2会尝试对焦。
有没有办法禁用焦点?
其他问题,是否必须在捕获前锁定焦点?似乎没有这样做可以正常工作,但我想确定。
编辑:抓住我这样做:
private void capture() {
try {
final CaptureRequest.Builder captureBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_ZERO_SHUTTER_LAG);
captureBuilder.addTarget(mImageReader.getSurface());
// Orientation
int rotation = getActivity().getWindowManager().getDefaultDisplay().getRotation();
captureBuilder.set(CaptureRequest.JPEG_ORIENTATION, getOrientation(rotation));
mCaptureSession.capture(captureBuilder.build(), null, mTakeImageBackgroundHandler);
}
catch (CameraAccessException e) {
e.printStackTrace();
}
保存帧后立即调用capture()方法。