Selects the optimal picture size with flags to toggle filtering criteria. - Android Media

Android examples for Media:Picture

Description

Selects the optimal picture size with flags to toggle filtering criteria.

Demo Code

/*// ww w  .ja  v a2 s.co  m
 * This file is part of Flying PhotoBooth.
 * 
 * Flying PhotoBooth is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Flying PhotoBooth is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Flying PhotoBooth.  If not, see <http://www.gnu.org/licenses/>.
 */
//package com.java2s;

import android.hardware.Camera.Size;

import java.util.List;

public class Main {
    /**
     * The aspect ratio tolerance used to select the optimal picture size.
     */
    private static final double PICTURE_ASPECT_RATIO_TOLERANCE = 0.4d;

    /**
     * Selects the optimal picture size with flags to toggle filtering criteria.
     *
     * @param previewSizes       the list of supported preview sizes.
     * @param pictureSizes       the list of supported picture sizes.
     * @param targetWidth        the target picture width.
     * @param targetHeight       the target picture height.
     * @param filterMinSize      true to block sizes smaller than target dimensions; false otherwise.
     * @param filterAspectRatio  true to block sizes above the aspect ratio tolerance; false otherwise.
     * @param filterPreviewSizes true to block sizes without a corresponding preview size; false otherwise.
     * @return the optimal supported picture size; or null if failed.
     */
    private static Size selectPictureSize(List<Size> previewSizes,
            List<Size> pictureSizes, final int targetWidth,
            final int targetHeight, boolean filterMinSize,
            boolean filterAspectRatio, boolean filterPreviewSizes) {
        Size optimalSize = null;

        double targetAspectRatio = (double) targetWidth / targetHeight;
        int minArea = Integer.MAX_VALUE;
        for (Size size : pictureSizes) {
            // Block sizes smaller than target dimensions.
            if (filterMinSize
                    && (size.width < targetWidth || size.height < targetHeight)) {
                continue;
            }

            // Block sizes above the aspect ratio tolerance.
            double aspectRatio = (double) size.width / size.height;
            if (filterAspectRatio
                    && (Math.abs(aspectRatio - targetAspectRatio) > PICTURE_ASPECT_RATIO_TOLERANCE)) {
                continue;
            }

            // Block sizes without a corresponding preview size.
            if (filterPreviewSizes && !previewSizes.contains(size)) {
                continue;
            }

            // Select smallest size.
            int area = size.width * size.height;
            if (area < minArea) {
                optimalSize = size;
                minArea = area;
            }
        }

        return optimalSize;
    }
}

Related Tutorials