get best Camera Size For Width And Height - Android android.hardware

Android examples for android.hardware:Camera Size

Description

get best Camera Size For Width And Height

Demo Code

import java.lang.reflect.Method;
import java.util.List;

import android.hardware.Camera;

public class Main{


    public static Camera.Size bestCameraSizeForWidthAndHeight(
            Camera.Parameters params, int width, int height) {
        List<Camera.Size> previewSizes = previewSizesForCameraParameters(params);
        if (previewSizes == null || previewSizes.size() == 0)
            return null;

        Camera.Size bestSize = null;/*from   w w  w. ja v  a2s .c  o m*/
        int bestDiff = 0;
        // find the preview size that minimizes the difference between width and height
        for (Camera.Size size : previewSizes) {
            int diff = Math.abs(size.width - width)
                    + Math.abs(size.height - height);
            if (bestSize == null || diff < bestDiff) {
                bestSize = size;
                bestDiff = diff;
            }
        }
        return bestSize;
    }

    public static List<Camera.Size> previewSizesForCameraParameters(
            Camera.Parameters params) {
        try {
            Method m = params.getClass().getMethod(
                    "getSupportedPreviewSizes");
            return (List<Camera.Size>) m.invoke(params);
        } catch (Exception ex) {
            return null;
        }
    }

}

Related Tutorials