From the list of image sizes, select the one which is closest to the specified size - Android android.graphics

Android examples for android.graphics:Image Load Save

Description

From the list of image sizes, select the one which is closest to the specified size

Demo Code

import android.content.Context;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import java.util.List;

public class Main{

    /**//from   w ww . j  av  a 2 s  .  c  om
     * From the list of image sizes, select the one which is closest to the specified size.
     */
    public static int closest(List<Camera.Size> sizes, int width, int height) {
        int best = -1;
        int bestScore = Integer.MAX_VALUE;

        for (int i = 0; i < sizes.size(); i++) {
            Camera.Size s = sizes.get(i);

            int dx = s.width - width;
            int dy = s.height - height;

            int score = dx * dx + dy * dy;
            if (score < bestScore) {
                best = i;
                bestScore = score;
            }
        }

        return best;
    }

}

Related Tutorials