【问题标题】:select the front camera [duplicate]选择前置摄像头[重复]
【发布时间】:2015-10-29 16:29:44
【问题描述】:

我正在使用以下代码拍照:

private void dispatchTakePictureIntent() {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    // Ensure that there's a camera activity to handle the intent
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
        // Create the File where the photo should go
        File photoFile = null;
        try {
            photoFile = createImageFile();
        } catch (IOException ex) {
            // Error occurred while creating the File
            //...
        }
        // Continue only if the File was successfully created
        if (photoFile != null) {
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
                    Uri.fromFile(photoFile));
            startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
        }
    }
}

有没有办法选择前置摄像头?

【问题讨论】:

    标签: android android-camera


    【解决方案1】:

    你可以试试这个。

    takePictureIntent.putExtra("android.intent.extras.CAMERA_FACING", 1);
    

    但并非所有相机应用都支持该标志

    【讨论】:

      【解决方案2】:

      您可以使用 Camera.CameraInfo.CAMERA_FACING_FRONT

      int cameraCount = 0;
      Camera cam = null;
      Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
      cameraCount = Camera.getNumberOfCameras();
      for (int camIdx = 0; camIdx < cameraCount; camIdx++) {
          Camera.getCameraInfo(camIdx, cameraInfo);
          if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
              try {
                  cam = Camera.open(camIdx);
              } catch (RuntimeException e) {
                  Log.e(TAG, "Camera failed to open: " + e.getLocalizedMessage());
              }
          }
      }
      

      并在 AndroidManifest.xml 中添加权限

      <uses-permission android:name="android.permission.CAMERA" />
      <uses-feature android:name="android.hardware.camera" android:required="false" />
      <uses-feature android:name="android.hardware.camera.front" android:required="false"/>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-07-01
        • 2013-05-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-09-20
        • 1970-01-01
        • 2014-09-28
        相关资源
        最近更新 更多