Android 连接一上 Camera.setParameters() 上的 RuntimeException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2046523/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
RuntimeException on Camera.setParameters() on nexus one
提问by Erik B
I copied the code from the answer hereand I still am getting a RuntimeException: setParameters failed error on my nexus one. My manifest file has camera and wake_lock permissions. This works on the emulator, and on the droid I don't get the error but it does have a rotation problem.
我从这里的答案中复制了代码,但仍然在我的 nexus 上收到 RuntimeException: setParameters failed 错误。我的清单文件具有相机和唤醒锁定权限。这适用于模拟器,而在 droid 上我没有收到错误,但确实存在旋转问题。
回答by Roman Nurik
You're most likely requsting an invalid preview size. If you check the results of adb logcatyou'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 getSupportedPreviewSizesin the Camera.Parametersobject returned by Camera.getParameters.
解决方案是请求与您想要的最接近的可用预览尺寸;您可以通过调用获得预览可用尺寸的列表getSupportedPreviewSizes中Camera.Parameters对象通过返回Camera.getParameters。
回答by Sam
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);
回答by Andrew Coonce
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);without first verifying that flash modes were supported by checking that parameters.getFlashMode() != null.
对于它的价值,我的问题的根源最终是我试图调用parameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);而没有首先通过检查parameters.getFlashMode() != null.
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.
回答by aaronmarino
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
回答by ColossalChris
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. Good luck.
等等。所有这些都包含在一个很好的 try{}catch(){} 中,效果很好。祝你好运。
Here is the getOptimalPreview Size from this great tutorial:
这是这个很棒的教程中的 getOptimalPreview 大小:
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;
}
回答by bang_chen
Some open source camera project like opencameraalways use try-catchto call Camera.setParameters:
一些像opencamera这样的开源相机项目总是使用try-catchCamera.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 getParametersbefore you call setParameterslike this:
另外,getParameters在你打电话之前总是得到最新的setParameters:
void setRotation(int rotation) {
Camera.Parameters parameters = this.getParameters();
parameters.setRotation(rotation);
setCameraParameters(parameters);
}
回答by Long Thay
the solution from Sam is correct but the output image is still zoomed a little bit on several tablet devices. 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. Here is implemented method in CameraHost:
Sam 的解决方案是正确的,但输出图像在几个平板设备上仍然放大了一点。我在互联网上找到的最佳实践之一,我们应该在 Camera host 中设置,以便在每次恢复相机时重新使用这些属性。这是在 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);
}

