Camera preview quality in Android is poor

后端 未结 1 555
不知归路
不知归路 2021-01-06 02:03

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

private Size getOptimalPreviewSize(List sizes,          


        
1条回答
  •  醉梦人生
    2021-01-06 02:23

    That algorithm is not the greatest.

    The default algorithm in my CWAC-Camera library is now:

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

    This:

    • Takes into account portrait versus landscape

    • Starts with the highest resolution previews and works its way down

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

    0 讨论(0)
提交回复
热议问题