意图未设置相机参数

5
我将从我的应用程序中以外部意图打开相机应用程序。 我正在使用以下代码来调用相机,并且以下是我的条件:
  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" />

我是否遗漏了什么?

2个回答

9

不幸的是,使用Intent与相机配合使用时,唯一可以设置的额外参数是

MediaStore.EXTRA_OUTPUT

Eg

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

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

相机正面意图附加项有时可能有效:

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

在查看Android源代码时,发现了一些“test”方法在Util类文件中,但并未正式记录在文档中。(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;
}

在照片模块中,使用以下方法:
(照片模块)
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吗?从这里没有办法进行闪光灯和分辨率设置吗? - Lost
如果是这样,那么这个操作是如何实现的呢?action.putExtra("android.intent.extras.CAMERA_FACING", 1)。它成功地在前置和后置摄像头之间切换。 - Lost
是的,我认为这取决于您使用的特定相机应用程序是否支持额外的意图。http://developer.android.com/guide/topics/media/camera.html#intent-image - frogmanx
2
是的,我认为这有时取决于手机的API级别和制造商自己相机应用程序的实现。 - frogmanx
好的。所以目前我认为我唯一的选择是使用SurfaceView并实例化相机对象,并将所有参数传递给它.. 对吗? - Lost
有没有好的教程可以帮助我在Surface View中打开相机?如果您愿意,我可以提出一个新问题吗? - Lost

0
intent.putExtra("android.intent.extras.CAMERA_FACING", android.hardware.Camera.CameraInfo.CAMERA_FACING_FRONT);

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

它作为一个广泛使用的Cordova插件(在此链接的第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); - Hugo

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接