Android examples for Camera:Camera Preview
Attempts to find the camera preview size as close as possible to the given width and height.
//package com.java2s; 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 . j av a 2 s.c o m int bestDiff = 0; 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; } } }