溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Android如何自定義相機Camera實現(xiàn)手動對焦

發(fā)布時間:2021-09-27 13:55:31 來源:億速云 閱讀:405 作者:小新 欄目:編程語言

這篇文章主要為大家展示了“Android如何自定義相機Camera實現(xiàn)手動對焦”,內(nèi)容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“Android如何自定義相機Camera實現(xiàn)手動對焦”這篇文章吧。

目錄

手動對焦的方法解析  實現(xiàn)用戶點擊屏幕后,設(shè)置對焦區(qū)域和測光區(qū)域

一、手動對焦的方法

手動對焦的實現(xiàn)主要通過兩個Camera方法來進行配置:

setFocusAreas 設(shè)置對焦的區(qū)域  setMeteringAreas 設(shè)置測光的區(qū)域

他們需要傳入一個Camera.Area集合,Camera.Area如圖:

/**   * Create an area with specified rectangle and weight.   *   * @param rect the bounds of the area.   * @param weight the weight of the area.   */  public Area(Rect rect, int weight) {    this.rect = rect;              this.weight = weight;        }

第一個參數(shù)是對焦和測光的區(qū)域,范圍在[-1000,-1000]到[1000,1000],第二個參數(shù)是權(quán)重,范圍在0到1000,當(dāng)傳入多個Area時,權(quán)重的大小決定著對焦或測光的優(yōu)先級,如果每次只對焦一個區(qū)域,那第二個參數(shù)直接傳入1000即可,大多數(shù)開發(fā)中也是如此。

說到第一個參數(shù)的范圍,請看下圖,將更加清晰明了:

我們可以看到,和手機屏幕的分辨率不同,Area到屏幕的映射區(qū)域是從左上角的-1000,-1000到右下角的1000,1000,中心點是0,0,我們點擊屏幕后獲取到的坐標(biāo),最終就需要轉(zhuǎn)化為映射區(qū)域的坐標(biāo),這是手動對焦最為重要的環(huán)節(jié),了解了這兩個必要的參數(shù)配置后,我們就可以開始手動對焦的實現(xiàn)了。

二、實現(xiàn)用戶點擊屏幕后,設(shè)置對焦區(qū)域和測光區(qū)域 獲取點擊預(yù)覽畫面的坐標(biāo)值

用戶點擊屏幕,實際是點擊的預(yù)覽畫面的區(qū)域,拍照功能大家肯定都知道,這個就不多解釋,那么我們直接通過setOnTouchListener方法對View進行監(jiān)聽即可

surfaceView.setOnTouchListener(new View.OnTouchListener() {      @Override      public boolean onTouch(View v, MotionEvent event) {        Log.e("MainActivity", "X坐標(biāo):" + event.getX()+",Y坐標(biāo):"+event.getY());        return false;      }    });

通過MotionEvent我們就可以獲取到用戶點擊屏幕時,相對于View的坐標(biāo)值了

將View坐標(biāo)值轉(zhuǎn)化為Area映射區(qū)域的坐標(biāo)值

之前說過,Area映射區(qū)域是[-1000,-1000]到[1000,1000],那么通過下面的坐標(biāo)換算公式,我們就可以得到點擊預(yù)覽畫面時,映射區(qū)域的坐標(biāo)值了

surfaceView.setOnTouchListener(new View.OnTouchListener() {      @Override      public boolean onTouch(View v, MotionEvent event) {        int areaX = (int) (event.getX() / surfaceView.getWidth() * 2000) - 1000; // 獲取映射區(qū)域的X坐標(biāo)        int areaY = (int) (event.getY() / surfaceView.getWidth() * 2000) - 1000; // 獲取映射區(qū)域的Y坐標(biāo)        return false;      }    });

獲取到映射區(qū)域的坐標(biāo)后,我們就要設(shè)置一個對焦的范圍了,范圍是靈活的,我這里就創(chuàng)建一個長寬是200的矩形區(qū)域

surfaceView.setOnTouchListener(new View.OnTouchListener() {      @Override      public boolean onTouch(View v, MotionEvent event) {        int areaX = (int) (event.getX() / surfaceView.getWidth() * 2000) - 1000; // 獲取映射區(qū)域的X坐標(biāo)        int areaY = (int) (event.getY() / surfaceView.getWidth() * 2000) - 1000; // 獲取映射區(qū)域的Y坐標(biāo)        // 創(chuàng)建Rect區(qū)域         Rect focusArea = new Rect();        focusArea.left = Math.max(x - 100, -1000); // 取最大或最小值,避免范圍溢出屏幕坐標(biāo)        focusArea.top = Math.max(y - 100, -1000);        focusArea.right = Math.min(x + 100, 1000);        focusArea.bottom = Math.min(y + 100, 1000);        return false;      }    });

設(shè)置對焦和測光

完成這一步,那就已經(jīng)實現(xiàn)了手動對焦了,如下代碼:

surfaceView.setOnTouchListener(new View.OnTouchListener() {      @Override      public boolean onTouch(View v, MotionEvent event) {        int areaX = (int) (event.getX() / surfaceView.getWidth() * 2000) - 1000; // 獲取映射區(qū)域的X坐標(biāo)        int areaY = (int) (event.getY() / surfaceView.getWidth() * 2000) - 1000; // 獲取映射區(qū)域的Y坐標(biāo)        // 創(chuàng)建Rect區(qū)域         Rect focusArea = new Rect();        focusArea.left = Math.max(x - 100, -1000); // 取最大或最小值,避免范圍溢出屏幕坐標(biāo)        focusArea.top = Math.max(y - 100, -1000);        focusArea.right = Math.min(x + 100, 1000);        focusArea.bottom = Math.min(y + 100, 1000);        // 創(chuàng)建Camera.Area        Camera.Area cameraArea = new Camera.Area(focusArea, 1000);        List<Camera.Area> meteringAreas = new ArrayList<Camera.Area>();        List<Camera.Area> focusAreas = new ArrayList<Camera.Area>();        if (mParameters.getMaxNumMeteringAreas() > 0) {          meteringAreas.add(cameraArea);          focusAreas.add(cameraArea);        }        mParameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO); // 設(shè)置對焦模式        mParameters.setFocusAreas(focusAreas); // 設(shè)置對焦區(qū)域        mParameters.setMeteringAreas(meteringAreas); // 設(shè)置測光區(qū)域        try {          mCamera.cancelAutoFocus(); // 每次對焦前,需要先取消對焦          mCamera.setParameters(mParameters); // 設(shè)置相機參數(shù)          mCamera.autoFocus(mAutoFocusCallback); // 開啟對焦        } catch (Exception e) {        }        return false;      }    });

相關(guān)注釋都在代碼中,手動對焦其實很簡單,計算好Area映射區(qū)域的坐標(biāo),為相機設(shè)置對焦和測光區(qū)域即可。

以上是“Android如何自定義相機Camera實現(xiàn)手動對焦”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI