如何在android中使用Intent打开前置摄像头?

前端之家收集整理的这篇文章主要介绍了如何在android中使用Intent打开前置摄像头?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想制作一个我必须只打开前置摄像头的应用程序,我该如何使用意图?
private void captureImage() {
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);

    intent.putExtra(MediaStore.EXTRA_OUTPUT,fileUri);
    intent.putExtra("android.intent.extras.CAMERA_FACING",1);

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

解决方法

java的
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    Uri photoUri = Uri.fromFile(getOutputPhotoFile());
    intent.putExtra(MediaStore.EXTRA_OUTPUT,photoUri);
    intent.putExtra("android.intent.extras.CAMERA_FACING",1);
    startActivityForResult(intent,CAMERA_PHOTO_REQUEST_CODE);

其他/替代解决方

private Camera openFrontFacingCameraGingerbread() {
    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.toString());
            }
        }
    }

    return cam;
}

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

only available in Gingerbread(2.3) and Up Android Version.

否则你也可以查看这些例子

1. android-Camera2Basic

2. Camera Example 2

3. Vogella example

希望它可以帮助你..

原文链接:https://www.f2er.com/android/316635.html

猜你在找的Android相关文章