Android examples for Camera:Camera Preview
Set camera parameters such as preview size, picture size, focus mode, etc.
//package com.java2s; import android.annotation.TargetApi; import android.graphics.Point; import android.hardware.Camera; import android.os.Build; import android.support.annotation.NonNull; import android.util.Log; import android.util.Size; import java.util.List; public class Main { public static final String TAG = "CameraUtils"; 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);// ww w . j a v a 2s . c om 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; } public static void log(String msg) { Log.v(TAG, msg); } 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)); } public static Camera.Size getBiggestSize(List<Camera.Size> sizes) { return getBiggestSize(sizes, Integer.MAX_VALUE, Integer.MAX_VALUE, false); } @TargetApi(21) 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)]; } @TargetApi(21) public static Size getBiggestSize(Size[] sizes) { return getBiggestSize(sizes, Integer.MAX_VALUE, Integer.MAX_VALUE, false); } 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 int getBiggestSizeIdx(@NonNull Point[] sizes, int width, int height, boolean isSideways) { if (sizes == null || width <= 0 || height <= 0) { return 0; } String model = Build.MODEL; if (model != null && model.equals("Nexus 4")) { width = height = Integer.MAX_VALUE; } if (isSideways) { int temp = width; width = height; height = temp; } int biggestScore = 0; int biggestIdx = 0; for (int i = 0; i < sizes.length; i++) { int score = sizes[i].x * sizes[i].y; if (sizes[i].x <= width && sizes[i].y <= height && score > biggestScore) { biggestScore = score; biggestIdx = i; } } return biggestIdx; } }