在Android相机preVIEW质量差 [英] Camera preview quality in Android is poor

查看:178
本文介绍了在Android相机preVIEW质量差的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做一个Android的相机应用,并使用了下列函数来获取preVIEW尺寸:

I am making a Camera app in Android and have used the following function to get the preview size:

private Size getOptimalPreviewSize(List<Size> sizes, int w, int h) {
    final double ASPECT_TOLERANCE = 0.1;
    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;
}

和我设置的大小是这样的:

And I set the size like this:

Size s = getOptimalPreviewSize(parameters.getSupportedPreviewSizes(), w, h);
parameters.setPreviewSize(s.width, s.height);

但问题是,当我跑我的应用程序,摄像机preVIEW质量真的很差。我如何得到相同的preVIEW质量,我得到我的时候我的手机上运行我的默认相机应用,换句话说,我想在我的应用程序高分辨率摄像头preVIEW。

But the problem is that when I run my app, the camera preview quality is really poor. How do i get the same preview quality as I get when I run my default camera app on my phone, in other words, I want a high resolution Camera preview in my app.

我甚至不知道preVIEW大小是否是此问题的原因。

I am even not sure whether the preview size is the cause of this problem.

请注意:获得preVIEW大小的功能是从Android电子文档样本程序采取

NOTE: The function to get the preview size is taken from sample programs from Android Docs.

推荐答案

这算法是不是最大的。

我CWAC-相机库现在是:

  public static Camera.Size getBestAspectPreviewSize(int displayOrientation,
                                                     int width,
                                                     int height,
                                                     Camera.Parameters parameters,
                                                     double closeEnough) {
    double targetRatio=(double)width / height;
    Camera.Size optimalSize=null;
    double minDiff=Double.MAX_VALUE;

    if (displayOrientation == 90 || displayOrientation == 270) {
      targetRatio=(double)height / width;
    }

    List<Size> sizes=parameters.getSupportedPreviewSizes();

    Collections.sort(sizes,
                     Collections.reverseOrder(new SizeComparator()));

    for (Size size : sizes) {
      double ratio=(double)size.width / size.height;

      if (Math.abs(ratio - targetRatio) < minDiff) {
        optimalSize=size;
        minDiff=Math.abs(ratio - targetRatio);
      }

      if (minDiff < closeEnough) {
        break;
      }
    }

    return(optimalSize);
  }


  • 考虑到肖像与风景

  • Takes into account portrait versus landscape

启动最高分辨率previews上下工作的方式

Starts with the highest resolution previews and works its way down

可以通过 closeEnough 进行调整,以选择更高的分辨率,而不是最佳匹配preVIEW区的长宽比

Can be tailored via closeEnough to opt for higher resolution as opposed to best matching the aspect ratio of the preview area

这篇关于在Android相机preVIEW质量差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆