您的问题是设置 AF 区域的控件。
- 计算要设置焦点的区域
- 停止当前会话
mPreviewSession.stopRepeating()
- 启动自动对焦触发!!!
3.1。安全开始使 AF 区域 IDLE
3.2。然后启动AF触发
mCaptureRequestBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER, CameraMetadata.CONTROL_AF_TRIGGER_IDLE);
mCaptureRequestBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER, CameraMetadata.CONTROL_AF_TRIGGER_START);
-
捕获一次以应用您的设置
-
检查是否支持AF和AE区域如果支持则应用该区域
if ( isMeteringAreaAESupported()) {
//System.out.println("AE regions are supported");
mCaptureRequestBuilder.set(CaptureRequest.CONTROL_AE_REGIONS, new MeteringRectangle[]{focusArea});
}
if ( isMeteringAreaAFSupported()) {
//System.out.println("AF regions are supported");
mCaptureRequestBuilder.set(CaptureRequest.CONTROL_AF_REGIONS, new MeteringRectangle[]{focusArea});
mCaptureRequestBuilder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_AUTO);
}
-
再次捕捉一次以设置焦点
mPreviewCaptureSession.capture(mCaptureRequestBuilder.build(), mCaptureCallback, mBackgroundHandler);
-
在mCaptureCallback 内,您应该取消 AF 触发器,但文档说 AF 触发器在某些设备中可以为空,所以我确实喜欢
mCaptureRequestBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER, CameraMetadata.CONTROL_AF_TRIGGER_IDLE);
mCaptureRequestBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER, CameraMetadata.CONTROL_AF_TRIGGER_CANCEL);
mCaptureRequestBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER, null);
-
最后是 mPreviewCaptureSession.setRepeatingRequest(mCaptureRequestBuilder.build(), null, mBackgroundHandler);
编辑
这是工作示例
private void setFocusArea(int focus_point_x, int focus_point_y) throws CameraAccessException {
if (cameraId == null || mManualFocusEngaged) return;
if (mCameraManager == null){
mCameraManager = (CameraManager) mContext.getSystemService(Context.CAMERA_SERVICE);
}
MeteringRectangle focusArea = null;
if (mCameraManager != null) {
if (mCameraCharacteristics == null) {
mCameraCharacteristics = mCameraManager.getCameraCharacteristics(cameraId);
}
final Rect sensorArraySize = mCameraCharacteristics.get(CameraCharacteristics.SENSOR_INFO_ACTIVE_ARRAY_SIZE);
int y = focus_point_x;
int x = focus_point_y;
if (sensorArraySize != null) {
y = (int)(((float)focus_point_x / currentWidth) * (float)sensorArraySize.height());
x = (int)(((float)focus_point_y / currentHeight) * (float)sensorArraySize.width());
}
final int halfTouchLength = 150;
focusArea = new MeteringRectangle(Math.max(x - halfTouchLength, 0),
Math.max(y - halfTouchLength, 0),
halfTouchLength * 2,
halfTouchLength * 2,
MeteringRectangle.METERING_WEIGHT_MAX - 1);
}
CameraCaptureSession.CaptureCallback mCaptureCallback = new CameraCaptureSession.CaptureCallback() {
@Override
public void onCaptureCompleted(@NonNull CameraCaptureSession session, @NonNull CaptureRequest request, @NonNull TotalCaptureResult result) {
super.onCaptureCompleted(session, request, result);
mManualFocusEngaged = false;
if (request.getTag().equals(FOCUS_TAG)) { // previously getTag == "Focus_tag"
//the focus trigger is complete -
//resume repeating (preview surface will get frames), clear AF trigger
mCaptureRequestBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER, CameraMetadata.CONTROL_AF_TRIGGER_IDLE);
mCaptureRequestBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER, CameraMetadata.CONTROL_AF_TRIGGER_CANCEL);
mCaptureRequestBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER, null);// As documentation says AF_trigger can be null in some device
try {
mCurrentCameraCaptureSession.setRepeatingRequest(mCaptureRequestBuilder.build(), null, mBackgroundHandler);
} catch (CameraAccessException e) {
// error handling
}
}
}
@Override
public void onCaptureFailed(@NonNull CameraCaptureSession session, @NonNull CaptureRequest request, @NonNull CaptureFailure failure) {
super.onCaptureFailed(session, request, failure);
mManualFocusEngaged = false;
}
};
mCurrentCameraCaptureSession.stopRepeating(); // Destroy current session
mCaptureRequestBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER, CameraMetadata.CONTROL_AF_TRIGGER_IDLE);
mCaptureRequestBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER, CameraMetadata.CONTROL_AF_TRIGGER_START);
mCurrentCameraCaptureSession.capture(mCaptureRequestBuilder.build(), mCaptureCallback, mBackgroundHandler); //Set all settings for once
if ( isMeteringAreaAESupported()) {
mCaptureRequestBuilder.set(CaptureRequest.CONTROL_AE_REGIONS, new MeteringRectangle[]{focusArea});
}
if ( isMeteringAreaAFSupported()) {
mCaptureRequestBuilder.set(CaptureRequest.CONTROL_AF_REGIONS, new MeteringRectangle[]{focusArea});
mCaptureRequestBuilder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_AUTO);
}
mCaptureRequestBuilder.setTag(FOCUS_TAG); //it will be checked inside mCaptureCallback
mCurrentCameraCaptureSession.capture(mCaptureRequestBuilder.build(), mCaptureCallback, mBackgroundHandler);
mManualFocusEngaged = true;
}
private boolean isMeteringAreaAFSupported() { // AF stands for AutoFocus
Integer afRegion = mCameraCharacteristics.get(CameraCharacteristics.CONTROL_MAX_REGIONS_AF);
return afRegion != null && afRegion >= 1;
}
private boolean isMeteringAreaAESupported() {//AE stands for AutoExposure
Integer aeState = mCameraCharacteristics.get(CameraCharacteristics.CONTROL_MAX_REGIONS_AE);
return aeState!=null && aeState >=1;
}
希望对您有所帮助。
享受编码