camera setParameters failed 相关的错误,大部分都是因为 previewSize 或者 pictureSize 设置不合理(即底层不支持)造成的。
网上找到的解决方案
public void surfaceCreated(SurfaceHolder holder){
int PreviewWidth = 0;
int PreviewHeight = 0;
MyCameraDemo.this.cam=Camera.open(0);
WindowManager manager=(WindowManager)MyCameraDemo.this.getSystemService(Context.WINDOW_SERVICE);
Display display=manager.getDefaultDisplay();
Parameters param=MyCameraDemo.this.cam.getParameters();
List<Camera.Size> sizeList = param.getSupportedPreviewSizes();
if (sizeList.size() > 1) {
Iterator<Camera.Size> itor = sizeList.iterator();
while (itor.hasNext()) {
Camera.Size cur = itor.next();
if (cur.width >= PreviewWidth && cur.height >= PreviewHeight) {
PreviewWidth = cur.width;
PreviewHeight = cur.height;
break;
}
}
}
param.setPreviewSize(PreviewWidth,PreviewHeight);
param.setPictureSize(PreviewWidth,PreviewHeight);
param.setPreviewFrameRate(5);
param.setPictureFormat(ImageFormat.JPEG);
param.set("jpeg-quality", 85);
MyCameraDemo.this.cam.setParameters(param);
try{
MyCameraDemo.this.cam.setPreviewDisplay(MyCameraDemo.this.holder);
}catch(IOException ex){
ex.printStackTrace();
}
MyCameraDemo.this.cam.startPreview();
MyCameraDemo.this.previewRunning=true;
}