標籤:.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); } }
首先將對焦模式設定為AutoFocusMode.AUTO。AutoFocusMode是定義枚舉類型,其定義如下:
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 拍照(四)——對焦模式