Android:在最新的三星Galaxy设备上获得最佳相机预览尺寸的问题

时间:2018-02-12 22:24:36

标签: android camera android-camera landscape

对于我的Android应用,我使用以下代码来获得最佳预览屏幕尺寸。这适用于大多数设备,但我特别在Samsung Galaxy 7和7 Edge上遇到此代码的一些问题。预览在纵向模式下工作正常,但在横向模式下,预览屏幕缩小并离开屏幕。我在这里做错了什么,或者我必须以不同的方式处理三星设备吗?

private Camera.Size getOptimalPreviewSize(List<Camera.Size> previewSizes, int w, int h) {
    final double ASPECT_TOLERANCE = 0.1;
    double targetRatio = (double) h / w;

    if (sizes == null)
        return null;

    Camera.Size optimalSize = null;
    double minDiff = Double.MAX_VALUE;
    double minDist = Double.MAX_VALUE;

    int targetHeight = h;

    for (Camera.Size size : sizes) {
        double ratio = (double) size.height / size.width;
        if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE)
            continue;

        //Selecting the preview size for which the difference between the ratio and the target ratio is minimum AND
        //The difference between the target height AND preview size height and the target width and the preview size width is minimum
        if (Math.abs(ratio - targetRatio) < minDiff) {

            minDiff = Math.abs(ratio - targetRatio);
            double distance = Math.sqrt(Math.pow(size.width - w, 2) + Math.pow(size.height - h, 2));

            if (distance < minDist) {
                optimalSize = size;
                minDist = distance;
            }
        }
    }

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

0 个答案:

没有答案