嗨,我正在使用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);
这停止了重复的自动对焦序列,但它仍然没有聚焦在触摸区域上,闪光灯只闪烁不到一秒而不是正常的对焦序列.请帮我解决这个问题,或者引导我进行工作,以便集中注意力.谢谢