安卓(Android) 怎么获取相机(Camera)最佳预览大小

2019年10月27日22:42:14安卓(Android) 怎么获取相机(Camera)最佳预览大小已关闭评论 389 1015字阅读3分23秒

1.简介

安卓(Android) 怎么获取相机(Camera)最佳预览大小?——请看下文。

2.示例代码

/**
 * 版权所有 编程十万个怎么办(www.tah1986.com)
 */


import android.hardware.Camera;

import java.util.List;

public class Main {
    private static final double ASPECT_TOLERANCE = 0.1;

    public static Camera.Size getOptimalPreviewSize(int displayOrientation,
            int width, int height, Camera.Parameters parameters) {
        double targetRatio = (double) width / height;
        List<Camera.Size> sizes = parameters.getSupportedPreviewSizes();
        Camera.Size optimalSize = null;
        double minDiff = Double.MAX_VALUE;
        int targetHeight = height;

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

        

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

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

        
        

        if (optimalSize == null) {
            minDiff = Double.MAX_VALUE;

            for (Camera.Size size : sizes) {
                if (Math.abs(size.height - targetHeight) < minDiff) {
                    optimalSize = size;
                    minDiff = Math.abs(size.height - targetHeight);
                }
            }
        }

        return (optimalSize);
    }
}

 
继续阅读
编程十万个怎么办