如何在android中使用Intent打开前置摄像头? [英] How can I open front camera using Intent in android?

查看:96
本文介绍了如何在android中使用Intent打开前置摄像头?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做一个只需要打开前置摄像头的应用,如何使用intent来实现?

private void captureImage() {意图意图 = 新意图(MediaStore.ACTION_IMAGE_CAPTURE);fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);intent.putExtra(MediaStore.EXTRA_OUTPUT,fileUri);intent.putExtra("android.intent.extras.CAMERA_FACING", 1);//启动图像捕获 IntentstartActivityForResult(意图,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(意图,CAMERA_PHOTO_REQUEST_CODE);

其他/替代解决方案

private Camera openFrontFacingCameraGingerbread() {int cameraCount = 0;相机凸轮 = 空;Camera.CameraInfo cameraInfo = new Camera.CameraInfo();cameraCount = Camera.getNumberOfCameras();for (int camIdx = 0; camIdx < cameraCount; camIdx++) {Camera.getCameraInfo(camIdx, cameraInfo);如果(cameraInfo.looking == Camera.CameraInfo.CAMERA_FACING_FRONT){尝试 {cam = Camera.open(camIdx);} catch (RuntimeException e) {Log.e(TAG, "相机无法打开:" + e.toString());}}}返回凸轮;}

AndroidManifest.xml文件中添加这些权限

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

<块引用>

仅适用于 Gingerbread(2.3) 及以上 Android 版本.

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

1.android-Camera2Basic

2.相机示例 2

3.Vogella 示例

希望能帮到你..

I want to make a app in which I have to open only front camera, How can i do it using 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);

Other/Alternate Solution

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;
}

add these permissions in the AndroidManifest.xml file

<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.

otherwise you can also check these example

1. android-Camera2Basic

2. Camera Example 2

3. Vogella example

hope it helps you..

这篇关于如何在android中使用Intent打开前置摄像头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆