get Camera Best Size using Comparator - Android Camera

Android examples for Camera:Camera Size

Description

get Camera Best Size using Comparator

Demo Code


//package com.java2s;

import android.graphics.Point;

import android.hardware.Camera;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class Main {
    private static Comparator<Camera.Size> CAMERA_SIZE_COMPARATOR = new Comparator<Camera.Size>() {
        @Override//  www .  j  ava 2s  . c o  m
        public int compare(Camera.Size lhs, Camera.Size rhs) {
            return lhs.width * lhs.height - rhs.width * rhs.height;
        }
    };

    public static Point getBestSize(final List<Camera.Size> list,
            final int width, final int height, int rotation) {
        if (list == null || list.isEmpty())
            return null;
        final boolean swap = rotation % 180 != 0;
        final int requiredWidth = swap ? height : width, requiredHeight = swap ? width
                : height;
        final ArrayList<Camera.Size> sorted = new ArrayList<>(list);
        Collections.sort(sorted, CAMERA_SIZE_COMPARATOR);
        for (final Camera.Size size : sorted) {
            if (size.width >= requiredWidth
                    && size.height >= requiredHeight)
                return new Point(size.width, size.height);
        }
        return null;
    }
}

Related Tutorials