中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

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

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

Android調用前后攝像頭同時工作實例代碼

發布時間:2020-10-07 21:04:41 來源:腳本之家 閱讀:175 作者:賴床的貓 欄目:移動開發

硬件環境:小米4

Android版本:6.0

咱們先看效果圖:

Android調用前后攝像頭同時工作實例代碼

我把代碼貼出來:

AndroidMainfest.xml文件(需要新增camera權限):

  <uses-permission android:name="android.permission.CAMERA" />

activity_main.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.bihu.qdemopush.MainActivity">

<LinearLayout
  android:layout_width="match_parent"
  android:layout_height="200dp"
  android:orientation="horizontal">
  <SurfaceView
    android:id="@+id/surfaceview1"
    android:layout_width="0dp"
    android:layout_weight="1"
    android:layout_height="fill_parent" />
  <SurfaceView
    android:id="@+id/surfaceview2"
    android:layout_width="0dp"
    android:layout_weight="1"
    android:layout_marginLeft="5dp"
    android:layout_height="fill_parent" />
</LinearLayout>
</LinearLayout>

MainActivity.java文件:主要是對2個surfaceView的顯示渲染

public class MainActivity extends AppCompatActivity {

SurfaceView surfaceview1, surfaceview2;
SurfaceHolder surfaceholder1, surfaceholder2;
String TAG = "MainActivity";
private Camera camera1 = null, camera2;
Camera.Parameters parameters;

@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  surfaceview1 = (SurfaceView) findViewById(R.id.surfaceview1);
  surfaceview2 = (SurfaceView) findViewById(R.id.surfaceview2);
  surfaceholder1 = surfaceview1.getHolder();
  surfaceholder1.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
  surfaceholder1.addCallback(new surfaceholderCallbackBack());

  surfaceholder2 = surfaceview2.getHolder();
  surfaceholder2.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
  surfaceholder2.addCallback(new surfaceholderCallbackFont());
}

@Override
protected void onDestroy() {
  Log.e(TAG, "onDestroy");
  super.onDestroy();
}

/**
 * 后置攝像頭回調
 */
class surfaceholderCallbackBack implements SurfaceHolder.Callback {
  @Override
  public void surfaceCreated(SurfaceHolder holder) {
    // 獲取camera對象
    int cameraCount = Camera.getNumberOfCameras();
    if (cameraCount > 0) {
      camera1 = Camera.open(0);
      try {
        // 設置預覽監聽
        camera1.setPreviewDisplay(holder);
        Camera.Parameters parameters = camera1.getParameters();

        if (MainActivity.this.getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE) {
          parameters.set("orientation", "portrait");
          camera1.setDisplayOrientation(90);
          parameters.setRotation(90);
        } else {
          parameters.set("orientation", "landscape");
          camera1.setDisplayOrientation(0);
          parameters.setRotation(0);
        }
        camera1.setParameters(parameters);
        // 啟動攝像頭預覽
        camera1.startPreview();
        System.out.println("camera.startpreview");

      } catch (IOException e) {
        e.printStackTrace();
        camera1.release();
        System.out.println("camera.release");
      }
    }
  }

  @Override
  public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
    camera1.autoFocus(new Camera.AutoFocusCallback() {
      @Override
      public void onAutoFocus(boolean success, Camera camera) {
        if (success) {
          initCamera();// 實現相機的參數初始化
          camera.cancelAutoFocus();// 只有加上了這一句,才會自動對焦。
        }
      }
    });

  }

  @Override
  public void surfaceDestroyed(SurfaceHolder holder) {

  }

  // 相機參數的初始化設置
  private void initCamera() {
    parameters = camera1.getParameters();
    parameters.setPictureFormat(PixelFormat.JPEG);
    parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);// 1連續對焦
    setDispaly(parameters, camera1);
    camera1.setParameters(parameters);
    camera1.startPreview();
    camera1.cancelAutoFocus();// 2如果要實現連續的自動對焦,這一句必須加上
  }

  // 控制圖像的正確顯示方向
  private void setDispaly(Camera.Parameters parameters, Camera camera) {
    if (Integer.parseInt(Build.VERSION.SDK) >= 8) {
      setDisplayOrientation(camera, 90);
    } else {
      parameters.setRotation(90);
    }

  }

  // 實現的圖像的正確顯示
  private void setDisplayOrientation(Camera camera, int i) {
    Method downPolymorphic;
    try {
      downPolymorphic = camera.getClass().getMethod("setDisplayOrientation", new Class[]{int.class});
      if (downPolymorphic != null) {
        downPolymorphic.invoke(camera, new Object[]{i});
      }
    } catch (Exception e) {
      Log.e("Came_e", "圖像出錯");
    }
  }
}

class surfaceholderCallbackFont implements SurfaceHolder.Callback {
  @Override
  public void surfaceCreated(SurfaceHolder holder) {
    // 獲取camera對象
    int cameraCount = Camera.getNumberOfCameras();
    if (cameraCount == 2) {
      camera2 = Camera.open(1);
    }
    try {
      // 設置預覽監聽
      camera2.setPreviewDisplay(holder);
      Camera.Parameters parameters = camera2.getParameters();

      if (MainActivity.this.getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE) {
        parameters.set("orientation", "portrait");
        camera2.setDisplayOrientation(90);
        parameters.setRotation(90);
      } else {
        parameters.set("orientation", "landscape");
        camera2.setDisplayOrientation(0);
        parameters.setRotation(0);
      }
      camera2.setParameters(parameters);
      // 啟動攝像頭預覽
      camera2.startPreview();
      System.out.println("camera.startpreview");

    } catch (IOException e) {
      e.printStackTrace();
      camera2.release();
      System.out.println("camera.release");
    }
  }

  @Override
  public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
    camera2.autoFocus(new Camera.AutoFocusCallback() {
      @Override
      public void onAutoFocus(boolean success, Camera camera) {
        if (success) {
          parameters = camera2.getParameters();
          parameters.setPictureFormat(PixelFormat.JPEG);
          parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);// 1連續對焦
          setDispaly(parameters, camera2);
          camera2.setParameters(parameters);
          camera2.startPreview();
          camera2.cancelAutoFocus();// 2如果要實現連續的自動對焦,這一句必須加上
          camera.cancelAutoFocus();// 只有加上了這一句,才會自動對焦。
        }
      }
    });

  }

  @Override
  public void surfaceDestroyed(SurfaceHolder holder) {

  }

  // 控制圖像的正確顯示方向
  private void setDispaly(Camera.Parameters parameters, Camera camera) {
    if (Integer.parseInt(Build.VERSION.SDK) >= 8) {
      setDisplayOrientation(camera, 90);
    } else {
      parameters.setRotation(90);
    }

  }

  // 實現的圖像的正確顯示
  private void setDisplayOrientation(Camera camera, int i) {
    Method downPolymorphic;
    try {
      downPolymorphic = camera.getClass().getMethod("setDisplayOrientation", new Class[]{int.class});
      if (downPolymorphic != null) {
        downPolymorphic.invoke(camera, new Object[]{i});
      }
    } catch (Exception e) {
      Log.e("Came_e", "圖像出錯");
    }
  }
}
}

可能出現的問題:

1、沒有權限,需要手動打開,代碼里由于追求簡便,沒有主動申請,只在mainfest中注冊了

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

灌云县| 枣庄市| 周宁县| 兴和县| 太康县| 杭锦旗| 平谷区| 平舆县| 新竹县| 墨脱县| 兴城市| 和静县| 新巴尔虎右旗| 龙岩市| 吐鲁番市| 海丰县| 大邑县| 抚宁县| 庆安县| 浦江县| 罗源县| 焦作市| 丹江口市| 鹤峰县| 吉水县| 绵竹市| 冷水江市| 通山县| 齐齐哈尔市| 绍兴县| 平泉县| 贺兰县| 新邵县| 无锡市| 亚东县| 边坝县| 满洲里市| 佛冈县| 榆社县| 新兴县| 泽普县|