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

对于一些手机,像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),,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。