camera的最佳分辨率

BAITOCC 发布于 2014/09/26 15:14
阅读 653
收藏 0
android自定义的camera,其中有部分代码是设置camera找到最佳分辨率的。当横屏时camera匹配的最佳分辨率是1280*720,竖屏时camera匹配的最佳分辨率就成了960*720了,同一个手机,竖屏时 camera匹配的最佳分辨率不应该也是1280*720吗?有哪位大神知道是哪出了问题了么。
加载中
0
os_jason
os_jason
设置camera分辨率,是先获取到camera支持的那些个分辨率,会有很多,根据需求设置对应的分辨率,不知道你那是怎么计算的最佳分辨率,感觉像是横屏下采用16:9的分辨率,竖屏下采用的是4:3的分辨率。
0
B
BAITOCC

下面是获取最佳分辨率的整个方法:

private Point findBestPreviewSizeValue(Camera.Parameters parameters, Point screenResolution) {



    List<Camera.Size> rawSupportedSizes = parameters.getSupportedPreviewSizes();
    if (rawSupportedSizes == null) {
      Log.w(TAG, "Device returned no supported preview sizes; using default");
      Camera.Size defaultSize = parameters.getPreviewSize();
      return new Point(defaultSize.width, defaultSize.height);
    }


    // Sort by size, descending
    List<Camera.Size> supportedPreviewSizes = new ArrayList<Camera.Size>(rawSupportedSizes);
    Collections.sort(supportedPreviewSizes, new Comparator<Camera.Size>() {
      @Override
      public int compare(Camera.Size a, Camera.Size b) {
        int aPixels = a.height * a.width;
        int bPixels = b.height * b.width;
        if (bPixels < aPixels) {
          return -1;
        }
        if (bPixels > aPixels) {
          return 1;
        }
        return 0;
      }
    });


    if (Log.isLoggable(TAG, Log.INFO)) {
      StringBuilder previewSizesString = new StringBuilder();
      for (Camera.Size supportedPreviewSize : supportedPreviewSizes) {
        previewSizesString.append(supportedPreviewSize.width).append('x')
            .append(supportedPreviewSize.height).append(' ');
      }
      Log.i(TAG, "Supported preview sizes: " + previewSizesString);
    }


    Point bestSize = null;
    float screenAspectRatio = (float) screenResolution.x / (float) screenResolution.y;


    float diff = Float.POSITIVE_INFINITY;
    
    for (Camera.Size supportedPreviewSize : supportedPreviewSizes) {
      int realWidth = supportedPreviewSize.width;
      int realHeight = supportedPreviewSize.height;
      int pixels = realWidth * realHeight;
      if (pixels < MIN_PREVIEW_PIXELS || pixels > MAX_PREVIEW_PIXELS) {
        continue;
      }
      boolean isCandidatePortrait = realWidth < realHeight;
      int maybeFlippedWidth = isCandidatePortrait ? realHeight : realWidth;
      int maybeFlippedHeight = isCandidatePortrait ? realWidth : realHeight;
      if (maybeFlippedWidth == screenResolution.x && maybeFlippedHeight == screenResolution.y) {
        Point exactPoint = new Point(realWidth, realHeight);
        Log.i(TAG, "Found preview size exactly matching screen size: " + exactPoint);
        return exactPoint;
      }
      float aspectRatio = (float) maybeFlippedWidth / (float) maybeFlippedHeight;
      float newDiff = Math.abs(aspectRatio - screenAspectRatio);
      if (newDiff < diff) {
        bestSize = new Point(realWidth, realHeight);
        diff = newDiff;
      }
    }


    if (bestSize == null) {
      Camera.Size defaultSize = parameters.getPreviewSize();
      bestSize = new Point(defaultSize.width, defaultSize.height);
      Log.i(TAG, "No suitable preview sizes, using default: " + bestSize);
    }


    Log.i(TAG, "Found best approximate preview size: " + bestSize);
    return bestSize;

  }

我的camera支持的分辨率:1920x1088 1280x720 960x720 800x480 720x480 768x432 640x480 576x432 480x320 384x288 352x288 320x240 240x160 176x144 

不知道是哪的问题,横屏时最佳分辨率是1280x720,竖屏时最佳分辨率就成了960x720

返回顶部
顶部