Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implement Tap to Focus in Camera2 API

Tags:

android

i want to implement tap to focus feature in my custom camera. This is the basic code provided by Google https://github.com/googlesamples/android-Camera2Basic

Here's the code snippet where i think i should add my feature If anyone has implemented the Camera2 API please help!

  private void lockFocus() {
    try {
        // This is how to tell the camera to lock focus.
        mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER,
                CameraMetadata.CONTROL_AF_TRIGGER_START);
        // Tell #mCaptureCallback to wait for the lock.
        mState = STATE_WAITING_LOCK;
        mCaptureSession.capture(mPreviewRequestBuilder.build(), mCaptureCallback,
                mBackgroundHandler);
    } catch (CameraAccessException e) {
        e.printStackTrace();
    }
}
like image 683
Veeresh Avatar asked Oct 15 '15 14:10

Veeresh


2 Answers

  1. Use the onTouch listener to get the point where the user touch the screen.
  2. Calculate a/some MeteringRectangle(s) based on that position.
  3. Use this metering rectangles to set the CaptureRequest.CONTROL_AF_REGION & CaptureRequest.CONTROL_AE_REGION

  4. set the CaptureRequest.CONTROL_AF_MODE to CaptureRequest.CONTROL_AF_MODE_AUTO

  5. CaptureRequest.CONTROL_AF_TRIGGER to CameraMetadata.CONTROL_AF_TRIGGER_START
  6. CaptureRequest.CONTROL_AE_PRECAPTURE_TRIGGER to CameraMetadata.CONTROL_AE_TRIGGER_START

  7. Then build capture request


Here you can find a full example.


like image 114
Manuel Schmitzberger Avatar answered Nov 17 '22 16:11

Manuel Schmitzberger


You'll need to set the autofocus and auto-exposure region to the area tapped by the user.

The keys are CONTROL_AF_REGIONS and CONTROL_AE_REGIONS.

The units for them are in the sensor active array coordinate system, so you'll have to translate from your UI touch coordinates to coordinates relative to your preview view, and from there, to the active array coordinates.

If the aspect ratio of your preview matches that of the sensor, then that's straightforward; if not, you'll have to adjust for the crop that is done to create the preview output. The best diagram for how the cropping works is currently here. Note that if you're also applying zoom, you'll want to include the zoom factor as well in your calculations.

Once you've calculated the region, you'll probably want to set the AF mode to AUTO (instead of CONTINUOUS_PICTURE which is usually used for normal preview), and then trigger AF. Once you converge AF (look at the AF state in the capture results, wait for AF_STATE_FOCUSED_LOCKED), you're good to take a picture that's in focus. If you want to return to normal operation after some time or the user cancels the touch to focus, switch AF mode back to CONTINUOUS_PICTURE.

like image 39
Eddy Talvala Avatar answered Nov 17 '22 18:11

Eddy Talvala