How to lock focus in camera2 api, android?

2019-02-08 14:17发布

问题:

I am trying to lock focus after my custom camera finds focus. First it AF mode set to auto:

builder.set(CaptureRequest.CONTROL_AF_MODE,
                    CaptureRequest.CONTROL_AF_MODE_AUTO);

And After touching the preview it finds focus distance, and I have to lock AF and AE using this code:

builder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE);
builder.set(CaptureRequest.CONTROL_AE_LOCK, true);

Locking AE works fine in any devices. Locking AF is working on Nexus5 and Nexus 5x. But as for Samsung S5 and S6, it keeps try to search focus.

What is the best way to lock focus?

回答1:

In order to lock the AF you have to take care of requesting the AF_TRIGGER only once by using capture() instead of repeatingRequest() (if not it enters in an af request loop and remains always trying to focus, but some nexus fix this in its FW, so some devices as Nexus 5 focus well even it shouldn't)

So, the correct order will be:

  • Set CONTROL_AF_MODE to CONTROL_AF_MODE_AUTO (via session.setRepeatingRequest()) and the AF_REGIONS and the AE_REGIONS if you want

  • Wait until you check that the CONTROL_AF_MODE is already in auto by checking the totalCaptureRequest from the CaptureCallback.

  • Set the AF_TRIGGER_START in the builder along with the CONTROL_AF_MODE_AUTObut this time instead of using session.setRepeatingRequest() use session.capture().

  • Inmediately after that, set the AF_TRIGGER to set the AF_TRIGGER_IDLE (not cancel!) an use again session.setRepeatingRequest() along with the CONTROL_AF_MODE_AUTO.

  • Wait until it has focused,you will receive FOCUSED_LOCKED or NOT_FOCUSED_LOCKED.

The PASSIVE_FOCUSED state is only when the CONTROL_AF_MODE is in continuous picture not in auto!

Take care of being really in auto focus mode before performing the trigger.

You should use always session.capture() with all triggers (with CONTROL_AE_PRECAPTURE_TRIGGER too) but always after that remember to put the triggers to IDLE (not cancel) in a session.repeatingRequest()



回答2:

You can't locus the focus on CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE.

You should put your AF mode on CONTROL_AF_MODE_AUTO adn wait for FOCUSED_LOCKED state during your AF trigger. You can check how the Android focus machine works on enter link description here