Setparameters() Fails Despite Setting Preview Size
Solution 1:
The documentation states that setParameters
throws a RuntimeException
when any of the parameters are invalid or not supported.
The parameters you are changing are the size and the format; However, your're taking the sizes from getSupportedPreviewSizes
so they mustn't be the problem. I guess the problem is with setPreviewFormat(ImageFormat.JPEG)
.
Solution 2:
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.Parametersparameters= myCamera.getParameters();
With the preview size:
if (myCamera.getParameters().getSupportedPreviewSizes() != null){
Camera.SizepreviewSize= getOptimalPreviewSize(myCamera.getParameters().getSupportedPreviewSizes(), width, height);;
parameters.setPreviewSize(previewSize.width, previewSize.height);
}
With the flash/focus modes:
if(parameters.getSupportedFocusModes()!=null&¶meters.getSupportedFocusModes().contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE)){parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);}if(parameters.getSupportedFlashModes()!=null&¶meters.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.
Here is the getOptimalPreview Size from this great tutorial:
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.SizeoptimalSize=null;
finaldoubleASPECT_TOLERANCE=0.1;
doubletargetRatio= (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;
doubleratio= (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;
}
Solution 3:
You can't tell which parameter cause runtime exception from the message text it shows. You had better check parameters one by one.
Post a Comment for "Setparameters() Fails Despite Setting Preview Size"