Are you familiar with the focus mode of android Camera?

Do you know which focus mode to set in which scenarios?

This article introduces the following 2 points, and we learn together ~

What kinds of focusing modes are there? How to use various focusing modes?Copy the code

What kinds of focusing modes are there?

1) Obtain the focusing mode supported by the device

Google provides us with an interface to query the current focus mode supported by the device

Camera1 get focus mode interface:

----- Camera.java

        public String getFocusMode() {
            return get(KEY_FOCUS_MODE);
        }
Copy the code

Camera2 Capture focus mode interface:

----- CameraCharacteristics.java

    @PublicKey
    public static final Key<int[]> CONTROL_AF_AVAILABLE_MODES =
            new Key<int[]>("android.control.afAvailableModes", int[].class);
Copy the code

2) Introduction of various focusing modes

Here is only a few commonly used focusing mode, detailed explanation of the introduction, you can view the attached source content at the end of the article. We usually use the following four focusing modes.

    public static final String FOCUS_MODE_AUTO = "auto";
    public static final String FOCUS_MODE_FIXED = "fixed";
    public static final String FOCUS_MODE_CONTINUOUS_VIDEO = "continuous-video";
    public static final String FOCUS_MODE_CONTINUOUS_PICTURE = "continuous-picture";
Copy the code
FOCUS_MODE_AUTO: autoFocus. This mode only triggers focus once, and only when the autoFocus interface is called after preview is enabled. This mode can be used for touch focus and photo focus. FOCUS_MODE_FIXED: fixed focus. Some cameras do not support focusing. FOCUS_MODE_CONTINUOUS_VIDEO: FOCUS_MODE_CONTINUOUS_VIDEO: this mode can be used during recording, and the focus will continue. Parameter takes effect after setting. FOCUS_MODE_CONTINUOUS_PICTURE: FOCUS_MODE_CONTINUOUS_PICTURE: FOCUS_MODE_CONTINUOUS_VIDEO is faster than FOCUS_MODE_CONTINUOUS_VIDEO.Copy the code

How to use various focusing modes?

The second point above gives a brief introduction to the four common focusing modes. We also know that in addition to auto mode, FOCUS_MODE_CONTINUOUS_VIDEO and FOCUS_MODE_CONTINUOUS_PICTURE modes, It takes effect after camera parameter is set.

Let’s take a look at the call interfaces Google provides:

1) Camera1

      public final void autoFocus(AutoFocusCallback cb)
      {
        ...
       }

      public void setFocusMode(String value) {
            ...
      }

      public void setParameters(Parameters params) {
         ...
       }

      public final void cancelAutoFocus()
      {
        ...
      }
Copy the code

2) Camera2

request.set(CaptureRequest.CONTROL_AF_MODE, int focusMode);
Copy the code

The attached:

An introduction to the various focusing modes? (Paste Android source code inside the introduction, write enough detailed)

/** * Auto-focus mode. Applications should call {@link * #autoFocus(AutoFocusCallback)} to start the focus in this mode.  */ public static final String FOCUS_MODE_AUTO = "auto"; /** * Focus is set at infinity. Applications should not call * {@link #autoFocus(AutoFocusCallback)} in this mode. */ public static final String FOCUS_MODE_INFINITY = "infinity"; /** * Macro (close-up) focus mode. Applications should call * {@link #autoFocus(AutoFocusCallback)} to start the focus in this * mode. */ public static final String FOCUS_MODE_MACRO = "macro"; /** * Focus is fixed. The camera is always in this mode if the focus is not * adjustable. If the camera has auto-focus, this mode can fix the * focus, which is usually at hyperfocal distance. Applications should * not call {@link #autoFocus(AutoFocusCallback)} in this mode. */ public static final String FOCUS_MODE_FIXED = "fixed"; /** @hide * Normal focus mode. Applications should call * {@link #autoFocus(AutoFocusCallback)} to start the focus in this * mode. */ public static final String FOCUS_MODE_NORMAL = "normal"; /** * Extended depth of field (EDOF). Focusing is done digitally and * continuously. Applications should not call {@link  * #autoFocus(AutoFocusCallback)} in this mode. */ public static final String FOCUS_MODE_EDOF = "edof"; /** * Continuous auto focus mode intended for video recording. The camera * continuously tries to focus. This is the best choice for video * recording because the focus changes smoothly . Applications still can * call {@link #takePicture(Camera.ShutterCallback, * Camera.PictureCallback, Camera.PictureCallback)} in this mode but the * subject may not be in focus. Auto focus starts when the parameter is * set. * * <p>Since API level 14, applications can call {@link * #autoFocus(AutoFocusCallback)} in this mode. The focus callback will * immediately return  with a boolean that indicates whether the focus is * sharp or not. The focus position is locked after autoFocus call. If * applications want to resume the continuous focus, cancelAutoFocus * must be called. Restarting the preview will not resume the continuous * autofocus. To stop continuous focus, applications should change the * focus mode to other modes. * * @see #FOCUS_MODE_CONTINUOUS_PICTURE */ public static final String FOCUS_MODE_CONTINUOUS_VIDEO = "continuous-video"; /** * Continuous auto focus mode intended for taking pictures. The camera * continuously tries to focus. The speed of focus change is more * aggressive than {@link #FOCUS_MODE_CONTINUOUS_VIDEO}. Auto focus * starts when the parameter is set. * * <p>Applications can call {@link #autoFocus(AutoFocusCallback)} in * this mode. If the autofocus is in the middle of scanning, the focus * callback will return when it completes. If the autofocus is not * scanning, the focus callback will immediately return with a boolean * that indicates whether the focus is sharp or not. The apps can then * decide if they want to take a picture immediately or to change the * focus mode to auto, and run a full autofocus cycle. The focus * position is locked after autoFocus call. If applications want to * resume the continuous focus, cancelAutoFocus must be called. * Restarting the preview will not resume the continuous autofocus. To * stop continuous focus, applications should change the focus mode to * other modes. * * @see #FOCUS_MODE_CONTINUOUS_VIDEO */ public static final  String FOCUS_MODE_CONTINUOUS_PICTURE = "continuous-picture";Copy the code

I have been engaged in Android Camera related development for 5 years

Now I work in Shenzhen

Welcome to follow my wechat official account “Xiaochi Notes”.

We learn and communicate together

– 2020.01.04 shenzhen