public Size getBestPreviewSize(List<Size> sizes, final Size desired) {
        // Sample of supported preview sizes:
        // http://www.kirill.org/ar/ar.php

        if (desired == null) {
            return sizes.get(0);
        }

        Collections.sort(sizes, new Comparator<Size>() {
            @Override
            public int compare(Size a, Size b) {
                Size ascaled = scale(a, desired);
                int aScale = ascaled.width - a.width;
                Size bscaled = scale(b, desired);
                int bScale = bscaled.width - b.width;

                if (aScale == 0 && bScale == 0) {
                    // Both no scaling, pick the smaller one
                    return a.compareTo(b);
                } else if (aScale == 0) {
                    // No scaling for a; pick a
                    return -1;
                } else if (bScale == 0) {
                    // No scaling for b; pick b
                    return 1;
                } else if (aScale < 0 && bScale < 0) {
                    // Both downscaled. Pick the smaller one (less downscaling).
                    return a.compareTo(b);
                } else if (aScale > 0 && bScale > 0) {
                    // Both upscaled. Pick the larger one (less upscaling).
                    return -a.compareTo(b);
                } else if (aScale < 0) {
                    // a downscaled, b upscaled. Pick a.
                    return -1;
                } else {
                    // a upscaled, b downscaled. Pick b.
                    return 1;
                }
            }
        });

        Log.i(TAG, "Viewfinder size: " + desired);
        Log.i(TAG, "Preview in order of preference: " + sizes);

        return sizes.get(0);
    }
Choose the best preview size, based on our display size.

We prefer:
1. no scaling
2. least downscaling
3. least upscaling

We do not care much about aspect ratio, since we just crop away extra pixels. We only choose
the size to minimize scaling.

In the future we may consider choosing the biggest possible preview size, to maximize the
resolution we have for decoding. We need more testing to see whether or not that is feasible.

@param sizes supported preview sizes, containing at least one size. Sizes are in natural camera orientation.
@param desired The desired display size, in the same orientation
@return the best preview size, never null
zxing-android-embedded/src/com/journeyapps/barcodescanner/camera/LegacyPreviewScalingStrategy.java