欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android自定義相機(jī)Camera實(shí)現(xiàn)手動對焦的方法示例

 更新時間:2019年06月11日 14:10:14   作者:FynnJason  
這篇文章主要介紹了Android自定義相機(jī)Camera實(shí)現(xiàn)手動對焦的方法示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

前言

我采用的是Camera來實(shí)現(xiàn)自定義相機(jī)的,如果你使用的是Camera2,那本文將不適用你。為了減少篇幅,本文主要講解手動對焦的實(shí)現(xiàn)方式,前提是你已經(jīng)能實(shí)現(xiàn)自定義相機(jī)的預(yù)覽等基礎(chǔ)功能。

目錄

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

 一、手動對焦的方法

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

  • 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ù)的范圍,請看下圖,將更加清晰明了:

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

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

用戶點(diǎn)擊屏幕,實(shí)際是點(diǎn)擊的預(yù)覽畫面的區(qū)域,拍照功能大家肯定都知道,這個就不多解釋,那么我們直接通過setOnTouchListener方法對View進(jìn)行監(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我們就可以獲取到用戶點(diǎn)擊屏幕時,相對于View的坐標(biāo)值了

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

之前說過,Area映射區(qū)域是[-1000,-1000]到[1000,1000],那么通過下面的坐標(biāo)換算公式,我們就可以得到點(diǎn)擊預(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)實(shí)現(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è)置相機(jī)參數(shù)
          mCamera.autoFocus(mAutoFocusCallback); // 開啟對焦
        } catch (Exception e) {
        }
        return false;
      }
    });

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

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論