简体   繁体   English

关联1的Camera.setParameters()上的RuntimeException

[英]RuntimeException on Camera.setParameters() on nexus one

I copied the code from the answer here and I still am getting a RuntimeException: setParameters failed error on my nexus one. 我从这里的答案中复制了代码, 仍然收到RuntimeException:我的一个setParameters失败错误。 My manifest file has camera and wake_lock permissions. 我的清单文件具有camera和awak_lock权限。 This works on the emulator, and on the droid I don't get the error but it does have a rotation problem. 这在模拟器上有效,在droid上也没有错误,但确实有旋转问题。

You're most likely requsting an invalid preview size. 您最有可能要求使用无效的预览尺寸。 If you check the results of adb logcat you'll probably see something like this: 如果检查adb logcat的结果,您可能会看到类似以下的内容:

E/QualcommCameraHardware(22732): Invalid preview size requested: 480x724

The solution is to request the closest available preview size to the one you'd like; 解决方案是请求最接近您想要的预览尺寸。 you can get a list of available preview sizes by calling getSupportedPreviewSizes in the Camera.Parameters object returned by Camera.getParameters . 您可以通过调用获得预览可用尺寸的列表getSupportedPreviewSizesCamera.Parameters对象通过返回Camera.getParameters

I corrected this by doing what Roman said, with the code: 我通过执行Roman所说的代码来更正了此问题:

   Camera.Parameters parameters = camera.getParameters();  
   List<Camera.Size> sizes = parameters.getSupportedPreviewSizes();  
   Camera.Size cs = sizes.get(0);  
   parameters.setPreviewSize(cs.width, cs.height);  
   camera.setParameters(parameters);

For what it's worth, the source of my issue ended up being that I was trying to call parameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF); 就其价值而言,问题的根源最终是我试图调用parameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF); without first verifying that flash modes were supported by checking that parameters.getFlashMode() != null . 无需先通过检查parameters.getFlashMode() != null验证是否支持Flash模式。

There's more than one cause for this poorly documented exception, so check all of your parameters and not just that you're using a supportedPreviewSize . 造成这种不良记录的异常的原因有多种,因此请检查所有参数,而不仅仅是检查是否使用了supportedPreviewSize

None of the above solved this for me. 以上都不是为我解决的。 Adding this code before setting the parameters did though. 但是在设置参数之前添加了此代码。

// stop preview before making changes
    try {
        mCamera.stopPreview();
    } catch (Exception e){
        // ignore: tried to stop a non-existent preview
    }

//now set your parameters

For me this would happen after taking a photo and the preview would freeze, until I updated my call for parameters to be the following. 对我来说,这将在拍照后发生,并且预览将冻结,直到我将参数的调用更新如下。 It is always important with this error to make sure you check all of the parameters that the camera is asking to set to make sure that every parameter you are asking the camera to set itself to is possible for the camera. 此错误始终很重要,请确保您检查了相机要求设置的所有参数,以确保相机可以要求您将相机设置为自己的每个参数。

Camera.Parameters parameters = myCamera.getParameters();

With the preview size: 预览尺寸:

if (myCamera.getParameters().getSupportedPreviewSizes() != null){
     Camera.Size previewSize = getOptimalPreviewSize(myCamera.getParameters().getSupportedPreviewSizes(), width, height);;
     parameters.setPreviewSize(previewSize.width, previewSize.height);
}

With the flash/focus modes: 使用闪光灯/对焦模式:

if(parameters.getSupportedFocusModes() != null && parameters.getSupportedFocusModes().contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE)){
     parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
}

if (parameters.getSupportedFlashModes() != null && parameters.getSupportedFlashModes().contains(Camera.Parameters.FLASH_MODE_AUTO)){
    parameters.setFlashMode(Camera.Parameters.FLASH_MODE_AUTO);

}

myCamera.setParameters(parameters);

etc. All of this wrapped in a nice try{}catch(){} works great. 等等。所有这些都包裹在一个不错的try {} catch(){}中,效果很好。 Good luck. 祝好运。

Here is the getOptimalPreview Size from this great tutorial : 这是这个很棒的教程中的getOptimalPreview Size:

private Camera.Size getOptimalPreviewSize(List<Camera.Size> sizes, int width, int height)
    {
        // Source: http://stackoverflow.com/questions/7942378/android-camera-will-not-work-startpreview-fails
        Camera.Size optimalSize = null;

        final double ASPECT_TOLERANCE = 0.1;
        double targetRatio = (double) height / width;

        // Try to find a size match which suits the whole screen minus the menu on the left.
        for (Camera.Size size : sizes){

            if (size.height != width) continue;
            double ratio = (double) size.width / size.height;
            if (ratio <= targetRatio + ASPECT_TOLERANCE && ratio >= targetRatio - ASPECT_TOLERANCE){
                optimalSize = size;
            }
        }

        // If we cannot find the one that matches the aspect ratio, ignore the requirement.
        if (optimalSize == null) {
            // TODO : Backup in case we don't get a size.
        }

        return optimalSize;
    }

Some open source camera project like opencamera always use try-catch to call Camera.setParameters: 一些开源相机项目(例如opencamera)始终使用try-catch调用Camera.setParameters:

private void setCameraParameters(Camera.Parameters parameters) {
    if( MyDebug.LOG )
        Log.d(TAG, "setCameraParameters");
    try {
        camera.setParameters(parameters);
        if( MyDebug.LOG )
            Log.d(TAG, "done");
    } catch (RuntimeException e) {
        // just in case something has gone wrong
        if( MyDebug.LOG )
            Log.d(TAG, "failed to set parameters");
        e.printStackTrace();
        count_camera_parameters_exception++;
    }
}

in addition,always get the latest getParameters before you call setParameters like this: 另外,在调用setParameters之前,总是获取最新的getParameters ,如下所示:

void setRotation(int rotation) {
    Camera.Parameters parameters = this.getParameters();
    parameters.setRotation(rotation);
    setCameraParameters(parameters);
}

the solution from Sam is correct but the output image is still zoomed a little bit on several tablet devices. Sam的解决方案是正确的,但输出图像仍在数个平板设备上放大了一点。 One of the best practices that I found on Internet, we should set in Camera host so that the properties will be re-used each time the camera is resumed. 我在Internet上找到的最佳实践之一是,我们应该在Camera host中进行设置,以便每次恢复相机时都可以重新使用这些属性。 Here is implemented method in CameraHost: 这是CameraHost中实现的方法:

@Override
        public Parameters adjustPreviewParameters(Parameters parameters) {
            List<Camera.Size> sizes = parameters.getSupportedPreviewSizes();
            Camera.Size cs = sizes.get(0);
            parameters.setPreviewSize(cs.width, cs.height);
            return super.adjustPreviewParameters(parameters);
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM