/** * 获取最佳预览大小 * * @param parameters 相机参数 * @param screenResolution 屏幕宽高 * @return */ private Point getBestCameraResolution(Camera.Parameters parameters, Point screenResolution) { float tmp; float mindiff = 100f; float x_d_y = (float) screenResolution.x / (float) screenResolution.y; Size best = null; List<Size> supportedPreviewSizes = parameters.getSupportedPreviewSizes(); for (Size s : supportedPreviewSizes) { tmp = Math.abs(((float) s.height / (float) s.width) - x_d_y); if (tmp < mindiff) { mindiff = tmp; best = s; } } return new Point(best.width, best.height); } /** * 获取屏幕宽度和高度,单位为px * * @param context * @return */ public static Point getScreenMetrics(Context context) { DisplayMetrics dm = context.getResources().getDisplayMetrics(); return new Point(dm.widthPixels, dm.heightPixels); }