Android Camera Size Get getOptimalPreviewSize(List supportedSizes, int w, int h)

Here you can find the source of getOptimalPreviewSize(List supportedSizes, int w, int h)

Description

Returns a Size object containing the dimensions for an optimal preview size for the current hardware.

Parameter

Parameter Description
supportedSizes A list of Size objects representing all the known preview sizes supported by this hardware.
w The surface width.
h The surface height.

Return

Returns a Size object containing the dimensions for an optimal preview size for the current hardware.

Declaration

public static Size getOptimalPreviewSize(List<Size> supportedSizes,
        int w, int h) 

Method Source Code

//package com.java2s;
import java.util.List;

import android.hardware.Camera.Size;

public class Main {
    /**// w ww.j  a  v a 2s  . co  m
     * Returns a Size object containing the dimensions for an optimal preview
     * size for the current hardware. This code is based on that found at:
     * http://developer.android.com/resources/samples/ApiDemos/src/com/example/
     * android/apis/graphics/CameraPreview.html
     * 
     * @param supportedSizes
     *            A list of Size objects representing all the known preview
     *            sizes supported by this hardware.
     * 
     * @param w
     *            The surface width.
     * 
     * @param h
     *            The surface height.
     * 
     * @return Returns a Size object containing the dimensions for an optimal
     *         preview size for the current hardware.
     */
    public static Size getOptimalPreviewSize(List<Size> supportedSizes,
            int w, int h) {
        final double ASPECT_TOLERANCE = 0.05;
        double targetRatio = (double) w / h;
        if (supportedSizes == null)
            return null;

        Size optimalSize = null;
        double minDiff = Double.MAX_VALUE;

        int targetHeight = h;

        // Try to find an size match aspect ratio and size
        for (Size size : supportedSizes) {
            double ratio = (double) size.width / size.height;
            if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE)
                continue;
            if (Math.abs(size.height - targetHeight) < minDiff) {
                optimalSize = size;
                minDiff = Math.abs(size.height - targetHeight);
            }
        }

        // Cannot find the one match the aspect ratio, ignore the requirement
        if (optimalSize == null) {
            minDiff = Double.MAX_VALUE;
            for (Size size : supportedSizes) {
                if (Math.abs(size.height - targetHeight) < minDiff) {
                    optimalSize = size;
                    minDiff = Math.abs(size.height - targetHeight);
                }
            }
        }
        return optimalSize;
    }
}

Related

  1. getSizeRATIO(List sizes, int w, int h)
  2. getSizeSIZE(List sizes, int w, int h)
  3. getSizeWIDTH(List sizes, int w, int h)
  4. getOptimalPreviewSize( List sizes, int w, int h)
  5. getOptimalPreviewSize(List sizes, int w, int h)
  6. getOptimalPreviewSize(int displayOrientation, int width, int height, Camera.Parameters parameters)
  7. getBiggestPictureSize( Camera.Parameters parameters)
  8. toString(Iterable areas)
  9. getSmallestPictureSize( Camera.Parameters parameters)