【问题标题】:How can I open front camera using Intent in android?如何在android中使用Intent打开前置摄像头?
【发布时间】:2017-01-18 07:11:38
【问题描述】:

我想制作一个只需要打开前置摄像头的应用程序,我该如何使用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);
}

【问题讨论】:

  • 你会发布打开前置摄像头的完整解决方案吗?

标签: android android-studio android-camera


【解决方案1】:

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

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

否则您也可以查看这些示例

1. android-Camera2Basic

2. Camera Example 2

3. Vogella example

希望对你有帮助..

【讨论】:

  • 我使用摄像头 API 打开前置摄像头,但在捕获图像后,它会在顺时针旋转 90 度后保存图像。如何正确保存图像。
  • 也许这个answer会帮助你
【解决方案2】:

标准安卓相机

使用Intent 打开标准的安卓相机应用程序。

从不使用 android.intent.extras.CAMERA_FACING 属性 - 这是一个未记录的功能,从某些 android 版本开始停止工作

相机 API

要打开前置摄像头,您应该使用 Camera API - 执行诸如选择前置摄像头、在视图中显示预览以及手动拍照等操作。 @skydroid 的回答显示了如何找到前置摄像头。请注意,Camera.open() 不会像您预期的那样为用户打开相机,您应该手动显示预览。

另外请注意,由于 API 级别 21,Camera API 已被弃用,文档建议改用 Camera 2 API。但是相机 API 仍然可以正常工作,如果您还想支持旧版本(

【讨论】:

  • 问题依然存在:“如何在android中使用Intent打开前置摄像头?” ??????
【解决方案3】:
try this:

 Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
 intent.putExtra("android.intent.extras.CAMERA_FACING", 1);
                File outPutFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + Util.SD_CARD_PATH);
                if (!outPutFile.exists()) {
                    outPutFile.mkdirs();
                }
                capturedImageUri = Uri.fromFile(File.createTempFile("packagename" + System.currentTimeMillis(), ".jpg", outPutFile));

                intent.putExtra(MediaStore.EXTRA_OUTPUT, capturedImageUri);
                startActivityForResult(intent, Util.REQUEST_CAMERA);

【讨论】:

  • 什么是 captureImageUri ?
  • 它和你的 fileuri 一样。它将单击的图像存储在该 uri 中
  • 这是我的 SD 卡路径..你可以给我们你想存储它的地方
  • 什么是 Util.?请帮助我。
  • 将其替换为“/urappname”..util 是我的自定义类
猜你喜欢
  • 2018-10-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-28
  • 1970-01-01
  • 2018-02-10
  • 1970-01-01
相关资源
最近更新 更多