如何在安卓中通过意图打开前后摄像头



我们实现相机的目的是它在micromax480p版本5.1中工作正常,但是当我们在Nexus7版本6.1中使用时,相机已打开,但我想打开一些时间 前部和后部是否可以根据需要打开。

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
    //intent.addCategory(Intent.CATEGORY_OPENABLE);
    //intent.setAction(Intent.ACTION_GET_CONTENT);
    if(camera == 1) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
            //intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.putExtra("android.intent.extras.CAMERA_FACING", android.hardware.Camera.CameraInfo.CAMERA_FACING_FRONT);
            intent.putExtra("android.intent.extras.LENS_FACING_FRONT", 1);
            intent.putExtra("android.intent.extra.USE_FRONT_CAMERA", true);
        } else {
            intent.putExtra("android.intent.extras.CAMERA_FACING", 1);
        }
    }

    // start the image capture Intent
    startActivityForResult(intent, requestCode);

我用过这样,但它默认打开了我们以前使用的相机(正面和背面(.谢谢。 对不起,我的英语通讯很弱。

要打开置摄像头:-

val cameraIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
startActivityForResult(cameraIntent, REQUEST_CODE_CAMERA)

要打开置摄像头:-

val cameraIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
when {
     Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1 && Build.VERSION.SDK_INT < Build.VERSION_CODES.O -> {
         cameraIntent.putExtra("android.intent.extras.CAMERA_FACING", CameraCharacteristics.LENS_FACING_FRONT)  // Tested on API 24 Android version 7.0(Samsung S6)
     }
     Build.VERSION.SDK_INT >= Build.VERSION_CODES.O -> {
         cameraIntent.putExtra("android.intent.extras.CAMERA_FACING", CameraCharacteristics.LENS_FACING_FRONT) // Tested on API 27 Android version 8.0(Nexus 6P)
         cameraIntent.putExtra("android.intent.extra.USE_FRONT_CAMERA", true)
     }
     else -> cameraIntent.putExtra("android.intent.extras.CAMERA_FACING", 1)  // Tested API 21 Android version 5.0.1(Samsung S4)
}
startActivityForResult(cameraIntent, REQUEST_CODE_CAMERA)

我无法使其适用于 API 28 及更高版本。此外,在某些设备中无法直接打开前置摄像头(取决于制造商(。

尝试下面的代码

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) { 
    intent.putExtra("android.intent.extras.LENS_FACING_FRONT", 1);
} else { 
    intent.putExtra("android.intent.extras.CAMERA_FACING", 1); 
}

要调用前置摄像头,您可以使用:

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);    
intent.putExtra("android.intent.extras.CAMERA_FACING", 1);

要调用后置摄像头,您可以使用:

Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
startActivity(intent);

您需要在AndroidManifest.xml中设置相机的权限:

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

这对我来说是工作

btn_camera.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
                    intent.putExtra("android.intent.extras.CAMERA_FACING", android.hardware.Camera.CameraInfo.CAMERA_FACING_FRONT);
                    intent.putExtra("android.intent.extras.LENS_FACING_FRONT", 1);
                    intent.putExtra("android.intent.extra.USE_FRONT_CAMERA", true);
                } else {
                    intent.putExtra("android.intent.extras.CAMERA_FACING", 1);
                }
                Uri fileUri = // your fileUri
                if(fileUri == null) {
                    Toasty.error(MainActivity.this, "Erro diretorio", Toast.LENGTH_SHORT, true).show();
                } else {
                    intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
                }
                startActivityForResult(intent, 100);
            }
        });

试试这个打开后置摄像头

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE_SECURE );

Android SDK <= 2.2 仅支持使用单个摄像头(第一个后置摄像头(。对于 2.3+,您可以遍历摄像头并找出哪个是前置摄像头(如果可用(。会像...

Camera cam = null;
Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
for (int camNo = 0; camNo < Camera.getNumberOfCameras(); camNo++) {
    CameraInfo camInfo = new CameraInfo();
    Camera.getCameraInfo(camNo, camInfo);
    if (camInfo.facing.equals(Camera.CameraInfo.CAMERA_FACING_FRONT)) {
        cam = Camera.open(camNo);
    }
}
if (cam == null) {
   // no front-facing camera, use the first back-facing camera instead.
   // you may instead wish to inform the user of an error here...
   cam = Camera.open();
}
// ... do stuff with Camera cam ...

您还需要将这些添加到清单中:

<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" />

最新更新