标签:.com switch surf 注册 views private date list star
原文:Android Camera2 拍照(四)——对焦模式
本篇将重点介绍使用Camera2 API进行手动对焦的设置,以及在手动对焦与自动对焦模式之间切换。
首先我们要实现点击preview区域进行手动对焦,这通过对TextureView注册touch事件实现。
mTextureView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
int actionMasked = MotionEventCompat.getActionMasked(event);
int fingerX, fingerY;
int length = (int) (getResources().getDisplayMetrics().density * 80);
switch (actionMasked) {
case MotionEvent.ACTION_DOWN:
fingerX = (int) event.getX();
fingerY = (int) event.getY();
LogUtil.d("onTouch: x->" + fingerX + ",y->" + fingerY);
mIvFocus.setX(fingerX - length / 2);
mIvFocus.setY(fingerY - length / 2);
mIvFocus.setVisibility(View.VISIBLE);
triggerFocusArea(fingerX, fingerY);
break;
}
return false;
}
});
triggerFocusArea将实现手动对焦。
private void triggerFocusArea(float x, float y) {
CameraManager manager = (CameraManager) getActivity().getSystemService(Context.CAMERA_SERVICE);
try {
CameraCharacteristics characteristics
= manager.getCameraCharacteristics(mCameraId);
Integer sensorOrientation = characteristics.get(
CameraCharacteristics.SENSOR_ORIENTATION);
sensorOrientation = sensorOrientation == null ? 0 : sensorOrientation;
Rect cropRegion = AutoFocusHelper.cropRegionForZoom(characteristics, 1f);
mAERegions = AutoFocusHelper.aeRegionsForNormalizedCoord(x, y, cropRegion, sensorOrientation);
mAFRegions = AutoFocusHelper.afRegionsForNormalizedCoord(x, y, cropRegion, sensorOrientation);
// Step 1: Request single frame CONTROL_AF_TRIGGER_START.
CaptureRequest.Builder builder;
builder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
builder.addTarget(mPreviewSurface);
builder.set(CaptureRequest.CONTROL_MODE, CameraMetadata.CONTROL_MODE_AUTO);
mControlAFMode = AutoFocusMode.AUTO;
builder.set(CaptureRequest.CONTROL_AF_MODE, mControlAFMode.switchToCamera2FocusMode());
builder.set(CaptureRequest.CONTROL_AF_TRIGGER, CaptureRequest.CONTROL_AF_TRIGGER_START);
mCaptureSession.capture(builder.build(), mPreCaptureCallback, mBackgroundHandler);
// Step 2: Call repeatingPreview to update mControlAFMode.
sendRepeatPreviewRequest();
resumeContinuousAFAfterDelay(DELAY_TIME_RESUME_CONTINUOUS_AF);
} catch (CameraAccessException ex) {
Log.e(TAG, "Could not execute preview request.", ex);
}
}
enum AutoFocusMode {
/**
* System is continuously focusing.
*/
CONTINUOUS_PICTURE,
/**
* System is running a triggered scan.
*/
AUTO;
int switchToCamera2FocusMode() {
switch (this) {
case CONTINUOUS_PICTURE:
return CameraMetadata.CONTROL_AF_MODE_CONTINUOUS_PICTURE;
case AUTO:
return CameraMetadata.CONTROL_AF_MODE_AUTO;
default:
return CameraMetadata.CONTROL_AF_MODE_CONTINUOUS_PICTURE;
}
}
}
然后通过builder.set(CaptureRequest.CONTROL_AF_MODE, mControlAFMode.switchToCamera2FocusMode());将对焦模式切换为手动对焦。在确定定位区域之后,通过函sendRepeatPreviewRequest恢复到
CameraMetadata.CONTROL_AF_MODE_CONTINUOUS_PICTURE;
源码参考:https://github.com/gengqifu/361Camera,欢迎fork/star。
标签:.com switch surf 注册 views private date list star
原文地址:https://www.cnblogs.com/lonelyxmas/p/9175777.html