Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import android.hardware.Camera;

import java.util.List;

public class Main {
    public static Camera.Size getOptimalPreviewSize2(List<Camera.Size> sizes, double targetRatio) {
        final double SMALL_VALUE = 0.001;
        if (sizes == null)
            return null;

        Camera.Size optimalSize = null;
        double optimalRatio = 0;
        double minRatioDiff = Double.MAX_VALUE;

        // Try to find a size which is close to the targetRatio and has the biggest possible resolution
        for (Camera.Size size : sizes) {
            double ratio = (double) size.width / size.height;
            if (Math.abs(targetRatio - ratio) <= minRatioDiff) {
                if (optimalSize != null && Math.abs(ratio - optimalRatio) < SMALL_VALUE) {
                    if (size.width < optimalSize.width)
                        continue;
                }
                optimalSize = size;
                optimalRatio = (double) size.width / size.height;
                minRatioDiff = Math.abs(targetRatio - ratio);
            }
        }
        return optimalSize;
    }
}