Android camera2 api touch to focus example?

Hi, I am using camera2basic example to implement camera2 application. I cannot find a good example for a touch implementation to focus on camera2 api. Currently, the code I use to touch to focus is as follows:

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 } } 

But the problem is that it shows strange behavior, while the auto-flash continues to repeat the autofocus sequence for an unlimited time, and this does not seem to focus on the affected area. I tried to change

 mCaptureSession.setRepeatingRequest(mPreviewRequestBuilder.build(), mCaptureCallback, mBackgroundHandler); 

in

 mCaptureSession.capture(mPreviewRequestBuilder.build(), mCaptureCallback, mBackgroundHandler); 

this stopped the repeating autofocus sequence, but still does not focus on the affected area, and the flash just blinks for less than a second instead of the usual focus sequence. Please help me with this or advise me to work with a focus example. Thanks

+6
source share
1 answer

Your problem is setting up AF region control.

  1. Calculate the region you want to set Focus
  2. stop current session mPreviewSession.stopRepeating()
  3. Launch an AF trigger!

    3.1. Feel free to start making the AF region inactive

    3.2. then start the AF trigger

      mCaptureRequestBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER, CameraMetadata.CONTROL_AF_TRIGGER_IDLE); mCaptureRequestBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER, CameraMetadata.CONTROL_AF_TRIGGER_START); 
  4. Grab once to apply your settings

  5. Check if AF and AE regions are supported or not, if supported apply this region

     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); } 
  6. Grab once again to set focus

      mPreviewCaptureSession.capture(mCaptureRequestBuilder.build(), mCaptureCallback, mBackgroundHandler); 
  7. inside mCaptureCallback you should cancel the AF trigger, but the documentation says that the AF trigger may be null on some devices, so I liked

     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); 
  8. The last thing - it mPreviewCaptureSession.setRepeatingRequest(mCaptureRequestBuilder.build(), null, mBackgroundHandler); mBackgroundHandler);

Hope it helps. Enjoy coding

+3
source

Source: https://habr.com/ru/post/1262791/


All Articles