Selects the optimal preview size based on the target aspect ratio and size. - Android Camera

Android examples for Camera:Camera Preview

Description

Selects the optimal preview size based on the target aspect ratio and size.

Demo Code

/*//from   ww  w.j  a v a2  s  .c  o 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/>.
 */
import android.app.Activity;
import android.hardware.Camera;
import android.hardware.Camera.CameraInfo;
import android.hardware.Camera.Size;
import android.view.Surface;
import java.util.List;

public class Main{
    /**
     * The aspect ratio tolerance used to select the optimal preview size.
     */
    private static final double PREVIEW_ASPECT_RATIO_TOLERANCE = 0.1d;
    /**
     * Selects the optimal preview size based on the target aspect ratio and size.
     *
     * @param previewSizes the list of supported preview sizes.
     * @param targetWidth  the target preview width.
     * @param targetHeight the target preview height.
     * @return the optimal supported preview size; or null if an empty list is passed.
     */
    public static Size getOptimalPreviewSize(List<Size> previewSizes,
            int targetWidth, int targetHeight) {
        // Check for null or empty list.
        if (previewSizes == null || previewSizes.isEmpty()) {
            return null;
        }

        Size optimalSize = null;

        double targetAspectRatio = (double) targetWidth / targetHeight;
        int minDiff = Integer.MAX_VALUE;

        /*
         * Try to match aspect ratio and size.
         */
        for (Size size : previewSizes) {
            // Block aspect ratios that do not match.
            double aspectRatio = (double) size.width / size.height;
            if (Math.abs(aspectRatio - targetAspectRatio) > PREVIEW_ASPECT_RATIO_TOLERANCE) {
                continue;
            }

            int diff = Math.max(Math.abs(size.width - targetWidth),
                    Math.abs(size.height - targetHeight));
            if (diff < minDiff) {
                optimalSize = size;
                minDiff = diff;
            }
        }

        /*
         * Try to match size.
         */
        if (optimalSize == null) {
            minDiff = Integer.MAX_VALUE;
            for (Size size : previewSizes) {
                int diff = Math.max(Math.abs(size.width - targetWidth),
                        Math.abs(size.height - targetHeight));
                if (diff < minDiff) {
                    optimalSize = size;
                    minDiff = diff;
                }
            }
        }

        LogsHelper.slog(CameraHelper.class, "getOptimalPreviewSize",
                "size=[" + optimalSize.width + ", " + optimalSize.height
                        + "]");

        return optimalSize;
    }
}

Related Tutorials