嗨,我正在使用 camera2basic 示例来实现我的 camera2 应用程序。我找不到任何好的例子来实现触摸到使用 camera2 api 对焦。目前,我用于触摸焦点的代码是这样的:
private void setFocusArea(MotionEvent event) {
if (mCameraId == null) return;
CameraManager cm = (CameraManager)getActivity().getSystemService(Context.CAMERA_SERVICE);
CameraCharacteristics cc = null;
try {
cc = cm.getCameraCharacteristics(mCameraId);
} catch (CameraAccessException e) {
e.printStackTrace();
}
int myX = (int)event.getX();
int myY = (int)event.getY();
MeteringRectangle focusArea = new MeteringRectangle(myX-100,myY-100,200,200,MeteringRectangle.METERING_WEIGHT_DONT_CARE);
mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER, CameraMetadata.CONTROL_AF_TRIGGER_CANCEL);
try {
mCaptureSession.capture(mPreviewRequestBuilder.build(), mCaptureCallback,
mBackgroundHandler);
// After this, the camera will go back to the normal state of preview.
mState = STATE_PREVIEW;
} catch (CameraAccessException e){
// log
}
if (isMeteringAreaAESupported(cc)) {
mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AE_REGIONS,
new MeteringRectangle[]{focusArea});
}
if (isMeteringAreaAFSupported(cc)) {
mPreviewRequestBuilder
.set(CaptureRequest.CONTROL_AF_REGIONS, new MeteringRectangle[]{focusArea});
mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_MODE,
CaptureRequest.CONTROL_AF_MODE_AUTO);
}
mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER,
CameraMetadata.CONTROL_AF_TRIGGER_START);
mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AE_PRECAPTURE_TRIGGER,
CameraMetadata.CONTROL_AE_PRECAPTURE_TRIGGER_START);
try {
mCaptureSession.setRepeatingRequest(mPreviewRequestBuilder.build(), mCaptureCallback,
mBackgroundHandler);
mManualFocusEngaged = true;
} catch (CameraAccessException e) {
// error handling
}
}
但问题是它显示出奇怪的行为,在自动闪光灯开启的情况下,它会无限次地重复自动对焦序列,而且它似乎没有聚焦在触摸区域。我试过改变
mCaptureSession.setRepeatingRequest(mPreviewRequestBuilder.build(), mCaptureCallback, mBackgroundHandler);
自:
mCaptureSession.capture(mPreviewRequestBuilder.build(), mCaptureCallback, mBackgroundHandler);
这停止了重复的自动对焦序列,但它仍然没有对焦于触摸区域,闪光灯只是闪烁不到一秒钟,而不是正常的对焦序列。请帮助我解决这个问题或指导我到一个工作触摸焦点的例子。谢谢
您的问题是设置 AF 区域的控件。
- 计算要设置焦点的区域
- 停止当前会话
mPreviewSession.stopRepeating()
- 启动自动对焦触发!!
3.1. 安全开始使自动对焦区域空闲
3.2. 然后启动自动对焦触发
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 触发器在某些设备中可能为空,所以我确实喜欢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;
}
希望对您有所帮助。享受编码