Android examples for Camera:Camera Preview
find Closet Preview Size
import java.util.List; import android.graphics.Point; import android.hardware.Camera; import android.util.Log; public class Main { private static final String TAG = ""; public static Camera.Size findClosetPreviewSize(Camera camera, Point preferSize) {/*from w ww . j a v a 2s . c o m*/ int preferX = preferSize.x; int preferY = preferSize.y; Camera.Parameters parameters = camera.getParameters(); List<Camera.Size> allSupportSizes = parameters.getSupportedPreviewSizes(); int minDiff = Integer.MAX_VALUE; int index = 0; for (int i = 0; i < allSupportSizes.size(); i++) { Camera.Size size = allSupportSizes.get(i); int x = size.width; int y = size.height; int diff = Math.abs(x - preferX) + Math.abs(y - preferY); if (diff < minDiff) { minDiff = diff; index = i; } } Camera.Size size = allSupportSizes.get(index); return size; } private static String dumpPreviewSizeList(List<Camera.Size> sizes) { String result = ""; for (Camera.Size size : sizes) { result += "(" + size.width + "," + size.height + ") "; } return result; } }