Android examples for android.hardware:Camera Preview
get Camera Params, such as preview size, picture size, focus mode, etc
import java.util.List; import android.annotation.TargetApi; import android.graphics.Point; import android.hardware.Camera; import android.os.Build; import android.util.Size; public class Main { /**/* w w w . ja v a 2s . c o m*/ * Set camera parameters such as preview size, picture size, focus mode, etc. */ public static Camera.Parameters getCameraParams(Camera.Parameters params, int width, int height, boolean isSideways) { if (params == null) return null; // Preview size Camera.Size mBestPreviewSize = getBiggestSize(params.getSupportedPreviewSizes(), width, height, isSideways); params.setPreviewSize(mBestPreviewSize.width, mBestPreviewSize.height); // Picture size Camera.Size mBestPicSize = getBiggestSize(params.getSupportedPictureSizes()); params.setPictureSize(mBestPicSize.width, mBestPicSize.height); // Focus mode String focusMode = getBestFocusMode(params); if (focusMode != null) { params.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO); } return params; } /** * Given a range of sizes, find the one that is largest while fitting in the * given width and height */ public static Camera.Size getBiggestSize(List<Camera.Size> sizes, int width, int height, boolean isSideways) { if (sizes == null || width <= 0 || height <= 0) { return null; } Point[] points = new Point[sizes.size()]; for (int i = 0; i < points.length; i++) { points[i] = new Point(sizes.get(i).width, sizes.get(i).height); } return sizes.get(getBiggestSizeIdx(points, width, height, isSideways)); } @TargetApi(21) public static Size getBiggestSize(Size[] sizes) { return getBiggestSize(sizes, Integer.MAX_VALUE, Integer.MAX_VALUE, false); } @TargetApi(21) /** * With camera2, find the biggest size that fits within the bounds */ public static Size getBiggestSize(Size[] sizes, int width, int height, boolean isSideways) { if (sizes == null || width <= 0 || height <= 0) { return null; } Point[] points = new Point[sizes.length]; for (int i = 0; i < points.length; i++) { points[i] = new Point(sizes[i].getWidth(), sizes[i].getHeight()); } return sizes[getBiggestSizeIdx(points, width, height, isSideways)]; } public static String getBestFocusMode(Camera.Parameters params) { if (params == null) return null; String focusMode = null; List<String> focusModes = params.getSupportedFocusModes(); if (Build.VERSION.SDK_INT >= 14 && focusModes.contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE)) { params.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE); } else if (Build.VERSION.SDK_INT >= 9 && focusModes.contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO)) { params.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO); } else if (focusModes.contains(Camera.Parameters.FOCUS_MODE_AUTO)) { params.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO); } return focusMode; } public static Camera.Size getBiggestSize(List<Camera.Size> sizes) { return getBiggestSize(sizes, Integer.MAX_VALUE, Integer.MAX_VALUE, false); } public static int getBiggestSizeIdx(Point[] sizes, int width, int height, boolean isSideways) { if (sizes == null || width <= 0 || height <= 0) { return 0; } /* * Scumbag exceptional cases */ String model = Build.MODEL; if (model != null && model.equals("Nexus 4")) { log("Nexus 4; using max size for preview and photo"); width = height = Integer.MAX_VALUE; } if (isSideways) { int temp = width; width = height; height = temp; } log("Finding biggest size within " + width + ", " + height); int biggestScore = 0; int biggestIdx = 0; for (int i = 0; i < sizes.length; i++) { log("Found size " + sizes[i].x + ", " + sizes[i].y); int score = sizes[i].x * sizes[i].y; if (sizes[i].x <= width && sizes[i].y <= height && score > biggestScore) { biggestScore = score; biggestIdx = i; } } log("Best size is " + sizes[biggestIdx].x + ", " + sizes[biggestIdx].y); return biggestIdx; } public static void log(String msg) { } }