【问题标题】:Intent does not set the camera parametersIntent 不设置相机参数
【发布时间】:2013-10-29 19:09:54
【问题描述】:

我正在从我的应用程序中打开相机应用程序作为外部意图。我正在使用以下代码来调用相机,以下是我的条件:

  1. 应该会打开前置摄像头。
  2. 最高画质。
  3. 闪光灯必须打开

以下是我的代码:

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

        action.putExtra("android.intent.extras.CAMERA_FACING", 1);
        action.putExtra("android.intent.extras.FLASH_MODE_ON", 1);
        action.putExtra("android.intent.extras.QUALITY_HIGH", 1);

现在,它确实打开了前置摄像头,但没有打开闪光灯,也没有将图片质量设置为高。

我的清单文件的权限部分如下所示:

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.FLASHLIGHT"/>
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<uses-feature android:name="android.hardware.camera.flash" />

我有什么遗漏的吗?

【问题讨论】:

    标签: android android-intent camera android-camera android-camera-intent


    【解决方案1】:

    不幸的是,当使用带有 Intent 的相机时,您唯一可以设置的额外参数是

    MediaStore.EXTRA_OUTPUT
    

    例如

    fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // create a file to save the image
    intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name
    

    这允许您映射相机应用程序将存储图像的位置。

    Camera Facing Intent extra 有时可以发挥作用:

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

    查看 android 源文件,在 Util 类文件中有一些“测试”方法,但没有正式记录:

    (实用程序)

    private static final String EXTRAS_CAMERA_FACING =
            "android.intent.extras.CAMERA_FACING";
    
    // This is for test only. Allow the camera to launch the specific camera.
    public static int getCameraFacingIntentExtras(Activity currentActivity) {
        int cameraId = -1;
    
        int intentCameraId =
                currentActivity.getIntent().getIntExtra(Util.EXTRAS_CAMERA_FACING, -1);
    
        if (isFrontCameraIntent(intentCameraId)) {
            // Check if the front camera exist
            int frontCameraId = CameraHolder.instance().getFrontCameraId();
            if (frontCameraId != -1) {
                cameraId = frontCameraId;
            }
        } else if (isBackCameraIntent(intentCameraId)) {
            // Check if the back camera exist
            int backCameraId = CameraHolder.instance().getBackCameraId();
            if (backCameraId != -1) {
                cameraId = backCameraId;
            }
        }
        return cameraId;
    }
    

    而在photomodule中,使用了如下方法:

    (照片模块)

    private int getPreferredCameraId(ComboPreferences preferences) {
        int intentCameraId = Util.getCameraFacingIntentExtras(mActivity);
        if (intentCameraId != -1) {
            // Testing purpose. Launch a specific camera through the intent
            // extras.
            return intentCameraId;
        } else {
            return CameraSettings.readPreferredCameraId(preferences);
        }
    }
    

    并且当相机应用初始化拍照模式时,它会调用这个方法来检查使用哪个相机:

    mCameraId = getPreferredCameraId(mPreferences);
    

    【讨论】:

    • 这意味着我必须做SurfaceLayOut?没有办法从这里进行闪光灯和分辨率设置?
    • 如果是这样,它是如何工作的? action.putExtra("android.intent.extras.CAMERA_FACING", 1).它成功地在前后摄像头之间切换。
    • 是的,我认为这取决于您使用的特定相机应用程序是否支持额外的意图。 developer.android.com/guide/topics/media/…
    • 是的,我认为有时可能取决于手机的 api 级别和制造商自己的相机应用程序实现
    • 好的。所以此刻我认为我唯一的选择是 SurfaceView 并实例化相机对象并将所有参数传递给它..正确吗?
    【解决方案2】:
    intent.putExtra("android.intent.extras.CAMERA_FACING", android.hardware.Camera.CameraInfo.CAMERA_FACING_FRONT);
    

    这是使用前置摄像头而不是默认后置摄像头开始拍摄的意图。

    这很有效,因为它在第 145 行的这个链接中的这个流行的科尔多瓦插件中使用: https://github.com/EddyVerbruggen/VideoCapturePlus-PhoneGap-Plugin/blob/master/src/android/nl/xservices/plugins/videocaptureplus/VideoCapturePlus.java

    希望这可以帮助任何面临同样问题的人。

    你也知道你是否可以设置一个意图来禁用相机控制(过滤器,在相机之间切换..等),这样它们就不会显示?

    【讨论】:

    • 那是一个常数,它总是1,所以和问题中的完全一样:action.putExtra("android.intent.extras.CAMERA_FACING", 1);
    猜你喜欢
    • 2012-05-29
    • 1970-01-01
    • 1970-01-01
    • 2012-07-10
    • 2013-08-03
    • 1970-01-01
    • 1970-01-01
    • 2013-06-18
    相关资源
    最近更新 更多