Android Camera開發系列:設置對焦模式模式


你對android camera的對焦模式熟悉嗎? 知道什么場景下該設置哪種對焦模式嗎?

本文針對下面2點展開介紹,和大家一起學習~

一、有哪幾種對焦模式?
二、如何使用各種對焦模式?

一、有哪幾種對焦模式?

1)獲取設備支持的對焦模式

Google為我們提供了查詢當前設備支持的對焦模式的接口~

Camera1獲取對焦模式接口:

----- Camera.java

        public String getFocusMode() {
            return get(KEY_FOCUS_MODE);
        }

Camera2獲取對焦模式接口:

----- CameraCharacteristics.java

    @PublicKey
    public static final Key<int[]> CONTROL_AF_AVAILABLE_MODES =
            new Key<int[]>("android.control.afAvailableModes", int[].class);
2)各種對焦模式的介紹

這里只介紹常用的幾種對焦模式,詳解的介紹,可以查看文末附的源碼內容。
我們常用的也就下面4種對焦模式。

    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";
   FOCUS_MODE_AUTO:自動對焦,這個只會觸發一次對焦,並且是需要在預覽開啟后,調用autoFocus接口后才會觸發,
像觸點對焦、和拍照對焦都可以用到該模式;
   FOCUS_MODE_FIXED:定焦,有些攝像頭本身不支持對焦;
   FOCUS_MODE_CONTINUOUS_VIDEO:錄像的時候,可以采用該模式,會持續對焦,設置parameter參數后就會生效;
   FOCUS_MODE_CONTINUOUS_PICTURE :拍照的時候,可以采用該模式,會持續對焦,設置parameter參數后就會生效,對焦速度相對 
   FOCUS_MODE_CONTINUOUS_VIDEO 會快點;

三、如何使用各種對焦模式?

上面第二點針對4種常見的對焦模式,做了簡單的介紹,我們也知道,除了auto模式,像 FOCUS_MODE_CONTINUOUS_VIDEO、FOCUS_MODE_CONTINUOUS_PICTURE 模式,是在設置camera parameter參數后就生效。

下面來看下google給我們提供了哪些調用接口:

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

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

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

      public final void cancelAutoFocus()
      {
        ...
      }
2)Camera2
request.set(CaptureRequest.CONTROL_AF_MODE, int focusMode);

附:

各種對焦模式的介紹?(貼的Android源碼里面的介紹,寫的夠詳細)
       /**
         * 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";

=======================================================================

*本人從事Android Camera相關開發已有5年,
*目前在深圳上班,
*小伙伴記得點我頭像關注,也可以關注我的微信公眾號【小馳筆記】,希望和更多的小伙伴一起交流 ~

---- 2020.01.04 深圳


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM