码迷,mamicode.com
首页 > 移动开发 > 详细

android 自定义照相机Camera黑屏 (转至 http://blog.csdn.net/chuchu521/article/details/8089058)

时间:2014-07-18 17:10:01      阅读:297      评论:0      收藏:0      [点我收藏+]

标签:android   blog   http   strong   os   width   

对于一些手机,像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 自定义照相机Camera黑屏 (转至 http://blog.csdn.net/chuchu521/article/details/8089058),布布扣,bubuko.com

android 自定义照相机Camera黑屏 (转至 http://blog.csdn.net/chuchu521/article/details/8089058)

标签:android   blog   http   strong   os   width   

原文地址:http://www.cnblogs.com/kobe8/p/3850889.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!