91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

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

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

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

天峨县| 晋城| 宜川县| 汪清县| 罗源县| 五河县| 石狮市| 抚远县| 察哈| 光山县| 绥棱县| 天峨县| 建湖县| 牟定县| 土默特右旗| 广东省| 沁源县| 平潭县| 义马市| 湟中县| 广宗县| 泰州市| 博罗县| 大渡口区| 曲麻莱县| 崇明县| 稷山县| 金湖县| 岳西县| 孝义市| 清镇市| 托克逊县| 定日县| 自贡市| 九江县| 诏安县| 延庆县| 封开县| 乌兰浩特市| 延吉市| 若羌县|