当前位置: 移动技术网 > IT编程>移动开发>Android > Android自定义照相机Camera出现黑屏的解决方法

Android自定义照相机Camera出现黑屏的解决方法

2019年07月24日  | 移动技术网IT编程  | 我要评论

bigtom,first love 香澄,现任英国国王

本文实例讲述了android自定义照相机camera出现黑屏的解决方法。分享给大家供大家参考,具体如下:

对于一些手机,像htc,当自定义camera时,调用camera.parameters的 parameters.setpreviewsize(width, height)方法时,如果width和height为奇数情况下,则会出现黑屏现象,解决办法可参考sdk提供的apidemos中关于camera的 例子:

list<size> sizes = parameters.getsupportedpreviewsizes();
size optimalsize = getoptimalpreviewsize(sizes, w, h);
parameters.setpreviewsize(optimalsize.width, optimalsize.height);

同时,在htc手机中,设置parameters.setpicturesize(width,height)属性,也会导致黑屏,而在三星手机上则没有问题。还有如果设置setpreviewsize属性的宽高错误的话,拍出的照片也会存在失真等bug,所以遇到适配问题时,最好的办法就是不设置previewsize和picturesize属性

getoptimalpreviewsize方法

private size getoptimalpreviewsize(list<size> sizes, int w, int h) {
 final double aspect_tolerance = 0.05;
 double targetratio = (double) w / h;
 if (sizes == null)
 return null;
 size optimalsize = null;
 double mindiff = double.max_value;
 int targetheight = h;
 // try to find an size match aspect ratio and size
 for (size size : sizes) {
 double ratio = (double) size.width / size.height;
 if (math.abs(ratio - targetratio) > aspect_tolerance)
 continue;
 if (math.abs(size.height - targetheight) < mindiff) {
 optimalsize = size;
 mindiff = math.abs(size.height - targetheight);
 }
 }
 // cannot find the one match the aspect ratio, ignore the requirement
 if (optimalsize == null) {
 mindiff = double.max_value;
 for (size size : sizes) {
 if (math.abs(size.height - targetheight) < mindiff) {
  optimalsize = size;
  mindiff = math.abs(size.height - targetheight);
 }
 }
 }
 return optimalsize;
}

更多关于android相关内容感兴趣的读者可查看本站专题:《android拍照与图片处理技巧总结》、《android开发入门与进阶教程》、《android多媒体操作技巧汇总(音频,视频,录音等)》、《android基本组件用法总结》、《android视图view技巧总结》、《android布局layout技巧总结》及《android控件用法总结

希望本文所述对大家android程序设计有所帮助。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网