亦余心之所善兮,虽九死其犹未悔。这篇文章主要讲述Android Camera2 拍照——对焦模式相关的知识,希望能为你提供帮助。
原文: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); } }
首先将对焦模式设置为AutoFocusMode.AUTO。AutoFocusMode是定义枚举类型,其定义如下:【Android Camera2 拍照——对焦模式】
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; } } }
用来实现自定义对焦模式和CameraMetadata中对焦模式的对应。其中AUTO即对应手动对焦然后通过builder.set(CaptureRequest.CONTROL_AF_MODE, mControlAFMode.switchToCamera2FocusMode()); 将对焦模式切换为手动对焦。在确定定位区域之后,通过函sendRepeatPreviewRequest恢复到
CameraMetadata.CONTROL_AF_MODE_CONTINUOUS_PICTURE;
源码参考:https://github.com/gengqifu/361Camera,欢迎fork/star。
推荐阅读
- Android Camera2 拍照——切换摄像头,延时拍摄和闪光模式
- 大规模集群要求swappiness内核参数调优
- Android Camera2拍照——使用SurfaceView
- Android Camera2 拍照——使用TextureView
- if __name__ == "__main__": tf.app.run()
- mac下创建安卓应用hello-world
- Android Camera2 拍照入门学习
- app自动化测试中的相关api
- app操作的一些命令