Android中的相机预览质量很差 [英] Camera preview quality in Android is poor

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

问题描述

我正在 Android 中制作相机应用,并使用以下函数来获取预览大小:

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;
}

我这样设置尺寸:

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

但问题是当我运行我的应用程序时,相机预览质量真的很差.如何获得与在手机上运行默认相机应用时相同的预览质量,换句话说,我希望在我的应用中获得高分辨率的相机预览.

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.

我什至不确定预览大小是否是导致此问题的原因.

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

注意:获取预览大小的函数取自 Android Docs 的示例程序.

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

推荐答案

那个算法不是最好的.

我的 CWAC-Camera 库中的默认算法现在是:

  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

从最高分辨率的预览开始,然后逐步向下进行

Starts with the highest resolution previews and works its way down

可以通过 closeEnough 进行定制,以选择更高分辨率,而不是与预览区域的纵横比最佳匹配

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

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

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