Android Camera Size Get getUsefulSize(List sizes, int w, int h, int type)

Here you can find the source of getUsefulSize(List sizes, int w, int h, int type)

Description

get Useful Size

Declaration

public static Size getUsefulSize(List<Size> sizes, int w, int h,
            int type) 

Method Source Code

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

import android.hardware.Camera.Size;

public class Main {
    public static final int SIZE = 0, RATIO = 1, WIDTH = 2, LB = 3;

    public static Size getUsefulSize(List<Size> sizes, int w, int h,
            int type) {
        Size size;/*from  w  ww .ja v a  2  s.  c o  m*/
        switch (type) {
        case SIZE:
            size = getSizeSIZE(sizes, w, h);
            break;
        case RATIO:
            size = getSizeRATIO(sizes, w, h);
            break;
        case WIDTH:
            size = getSizeWIDTH(sizes, w, h);
            break;
        case LB:
            size = getSizeLB(sizes, w, h);
            break;
        default:
            size = getSizeSIZE(sizes, w, h);
            break;
        }
        return size;
    }

    private static Size getSizeSIZE(List<Size> sizes, int w, int h) {
        Size re = null;
        int wantSize = w * h;
        int minGap = wantSize;
        for (Size size : sizes) {
            if (getSizeGap(size, wantSize) < minGap) {
                re = size;
                minGap = getSizeGap(size, wantSize);
            }
        }
        return re;
    }

    private static Size getSizeRATIO(List<Size> sizes, int w, int h) {
        Size re = null;
        double wantRatio = (double) w / (double) h;
        double minGap = wantRatio;
        for (Size size : sizes) {
            if (getRatioGap(size, wantRatio) < minGap) {
                minGap = getRatioGap(size, wantRatio);
                re = size;
            }
        }
        return re;
    }

    private static Size getSizeWIDTH(List<Size> sizes, int w, int h) {
        int wantWidth = w;
        Size re = null;
        int minGap = w;
        for (Size size : sizes) {
            if (getWidthGap(size, wantWidth) < minGap) {
                minGap = getWidthGap(size, wantWidth);
                re = size;
            }
        }
        return re;
    }

    private static Size getSizeLB(List<Size> sizes, int w, int h) {
        Size re = null;
        int wantSize = w * h;
        int minGap = wantSize;
        for (Size size : sizes) {
            if (getSizeGapValue(size, wantSize) > 0
                    && getSizeGapValue(size, wantSize) < minGap) {
                re = size;
                minGap = getSizeGap(size, wantSize);
            }
        }
        return re;
    }

    private static int getSizeGap(Size size, int wantSize) {
        return Math.abs(size.width * size.height - wantSize);
    }

    private static double getRatioGap(Size size, double wantRatio) {
        double sizeRatio = (double) size.height / (double) size.width;
        return Math.abs(sizeRatio - wantRatio);
    }

    private static int getWidthGap(Size size, int wantWidth) {
        return Math.abs(size.height - wantWidth);
    }

    private static int getSizeGapValue(Size size, int wantSize) {
        return size.width * size.height - wantSize;
    }
}

Related

  1. getOptimalPreviewSize(List supportedSizes, int w, int h)
  2. getOptimalPreviewSize(int displayOrientation, int width, int height, Camera.Parameters parameters)
  3. getBiggestPictureSize( Camera.Parameters parameters)
  4. toString(Iterable areas)
  5. getSmallestPictureSize( Camera.Parameters parameters)