当前位置:   article > 正文

Camera基本属性了解及设置_setpreviewsize

setpreviewsize

  将camera肯定是要有预览界面来配合着才有激情,不然无图无真相。那就讲的没有太大的激情。

  我们对camera的使用一般是在app里面有用到设置获取图片的时候提供一个拍照功能,一般是跳转到系统的拍照界面使用camera来获取图片;有的app有需求就跳转到自己的拍照界面。我们知道系统的应用其实也是个app,无论是跳转到系统拍照界面还是自己的拍照界面其实都是一个preview,然后再“截屏”生成一个图片。我下面讲一些关于camera的基础和preview的结合使用。

  首先要获取camera的属性:Camera.Parameters parameters = camera.getParameters();

 camera的基本属性设置:

  (1)设置预览大小 :  

    parameters.setPreviewSize(“宽”, “高”);

   一般设置的预览大小就是手机屏幕的宽高,那么就要获取屏幕的宽高,然后再经过稍微的处理:

  1. WindowManager manager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
  2. Display display = manager.getDefaultDisplay();
  3. 宽:display.getWidth() 高:display.getHeight()
 由于下面的介绍都会用到一个系统的实体Point,所以我这里把宽高设置到这个实体里面,然后方便使用,然后就得到下面的代码:

  1. WindowManager manager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
  2. Display display = manager.getDefaultDisplay();
  3. //获取屏幕的宽高(像素值)设置到一个实体(point)里面
  4. Point screenResolution = new Point(display.getWidth(), display.getHeight());
然后再根据屏幕的宽高做优化处理:

  1. /*根据相机的参数和手机屏幕参数来重新设置相机的*/
  2. public Point getCameraResolution(Camera.Parameters parameters, Point screenResolution) {
  3. String previewSizeValueString = parameters.get("preview-size-values");
  4. if (previewSizeValueString == null) {
  5. previewSizeValueString = parameters.get("preview-size-value");
  6. }
  7. Point cameraResolution = null;
  8. if (previewSizeValueString != null) {
  9. cameraResolution = findBestPreviewSizeValue(previewSizeValueString, screenResolution);
  10. }
  11. if (cameraResolution == null) {
  12. //没有找到最优的预览效果,那么至少也要确保相机分辨率是8的倍数(我也不知道为什么),下面的操作就是防备屏幕不是8的倍数,如果
  13. //是8的倍数,就会在上面的寻找最优size的时候会找到的
  14. cameraResolution = new Point(
  15. (screenResolution.x >> 3) << 3,
  16. (screenResolution.y >> 3) << 3);
  17. }
  18. return cameraResolution;
  19. }
  20. /*根据预览大小和手机屏幕大小来设一个比较完美的预览效果*/
  21. public Point findBestPreviewSizeValue(CharSequence previewSizeValueString, Point screenResolution) {
  22. int bestX = 0;
  23. int bestY = 0;
  24. int diff = Integer.MAX_VALUE;//差值
  25. for (String previewSize : COMMA_PATTERN.split(previewSizeValueString)) {
  26. previewSize = previewSize.trim();
  27. int dimPosition = previewSize.indexOf('x');
  28. if (dimPosition < 0) {
  29. //这是一个很差的预览大小
  30. continue;
  31. }
  32. int newX;
  33. int newY;
  34. try {
  35. newX = Integer.parseInt(previewSize.substring(0, dimPosition));
  36. newY = Integer.parseInt(previewSize.substring(dimPosition + 1));
  37. } catch (NumberFormatException nfe) {
  38. //字符串转换异常,这是自己写的tryCatch,是为了安全起见
  39. continue;
  40. }
  41. int newDiff = Math.abs(newX - screenResolution.x) + Math.abs(newY - screenResolution.y);
  42. if (newDiff == 0) {
  43. bestX = newX;
  44. bestY = newY;
  45. break;
  46. } else if (newDiff < diff) {
  47. bestX = newX;
  48. bestY = newY;
  49. diff = newDiff;
  50. }
  51. }
  52. if (bestY > 0 && bestX > 0) {
  53. return new Point(bestX, bestY);
  54. }
  55. return null;
  56. }

最后把优化处理之后的point的数据设置个camera的parameters:

  1. Camera.Parameters parameters = camera.getParameters();
  2. parameters.setPreviewSize(cameraResolution.x, cameraResolution.y);//设置相机的预览size


(2)设置闪光灯:

  

  1. /*设置闪光灯,本来是不用设置的,应该是直接获取,但是根据三星建议说防止黑客攻击,最好设置一下*/
  2. private void setFlash(Camera.Parameters parameters) {
  3. if (Build.MODEL.contains("Behold II") && Integer.parseInt(Build.VERSION.SDK) == 3) {
  4. parameters.set("flash-value", 1);
  5. } else {
  6. parameters.set("flash-value", 2);
  7. }
  8. parameters.set("flash-mode", "off");
  9. }

(3)设置缩放:

   一般使用camera进行拍照和录制视频的时候都要支持手势识别进行缩放,原理就是把手势缩放的xy值然后进行算法转换然后动态的设置到camera的parameter上。

  1. /*设置缩放*/
  2. private void setZoom(Camera.Parameters parameters) {
  3. String zoomSupportString = parameters.get("zoom-supported");
  4. if (zoomSupportString != null && !Boolean.parseBoolean(zoomSupportString)) {
  5. return;
  6. }
  7. int tenDesiredZoom = TEN_DESIRED_ZOOM;//TEN_DESIRED_ZOOM = 27
  8. String maxZoomString = parameters.get("max-zoom");
  9. if (maxZoomString != null) {
  10. try {
  11. int tenMaxZoom = (int) (10.0 * Double.parseDouble(maxZoomString));
  12. if (tenDesiredZoom > tenMaxZoom) {
  13. tenDesiredZoom = tenMaxZoom;
  14. }
  15. } catch (NumberFormatException e) {
  16. Log.i(TAG, "获取原本的缩放最大级别转换失败");
  17. }
  18. String takingPictureZoomMaxString = parameters.get("taking-picture-zoom-max");
  19. if (takingPictureZoomMaxString != null) {
  20. try {
  21. int takePicMax = Integer.parseInt(takingPictureZoomMaxString);
  22. if (tenDesiredZoom > takePicMax) {
  23. tenDesiredZoom = takePicMax;
  24. }
  25. Log.i(TAG, "拍照缩放级别被最原始级别小");
  26. } catch (NumberFormatException nfe) {
  27. Log.i(TAG, "拍照缩放级别转换失败");
  28. }
  29. }
  30. String motZoomMaxString = parameters.get("mot-zoom-values");
  31. if (motZoomMaxString != null) {
  32. tenDesiredZoom = findBestMotZoomValue(motZoomMaxString, tenDesiredZoom);
  33. }
  34. String motZoomStepString = parameters.get("mot-zoom-step");
  35. if (motZoomStepString != null) {
  36. try {
  37. double motZoomStep = Double.parseDouble(motZoomStepString.trim());
  38. int tenZoomStep = (int) (10.0 * motZoomStep);
  39. if (tenZoomStep > 1) {
  40. tenDesiredZoom -= tenDesiredZoom % tenZoomStep;
  41. }
  42. } catch (NumberFormatException nfe) {
  43. }
  44. }
  45. if (maxZoomString != null || motZoomMaxString != null) {
  46. parameters.set("zoom", String.valueOf(tenDesiredZoom / 10.0));
  47. }
  48. if (takingPictureZoomMaxString != null) {
  49. parameters.set("taking-picture-zoom", tenDesiredZoom);
  50. }
  51. }
  52. }
  53. private static int findBestMotZoomValue(CharSequence stringValues, int tenDesireZoom) {
  54. int tenBestValue = 0;
  55. for (String stringValue : COMMA_PATTERN.split(stringValues)) {
  56. stringValue = stringValue.trim();
  57. double value;
  58. try {
  59. value = Double.parseDouble(stringValue);
  60. } catch (NumberFormatException nfe) {
  61. return tenDesireZoom;
  62. }
  63. int tenValue = (int) (10.0 * value);
  64. if (Math.abs(tenDesireZoom - value) < Math.abs(tenDesireZoom - tenBestValue)) {
  65. tenBestValue = tenValue;
  66. }
  67. }
  68. return tenBestValue;
  69. }
使用的时候你可以直接进行设置,parameter.set("zoom","10");来设置缩放。

 最后贴一个完整测camera的配置类:

 

  1. import android.content.Context;
  2. import android.graphics.Point;
  3. import android.hardware.Camera;
  4. import android.os.Build;
  5. import android.util.Log;
  6. import android.view.Display;
  7. import android.view.WindowManager;
  8. import java.lang.reflect.Method;
  9. import java.util.regex.Pattern;
  10. public class CameraConfigurationManager {
  11. private static final String TAG = "Cy==CameraConfigManager";
  12. private Context mContext;
  13. private Point screenResolution;
  14. private static final Pattern COMMA_PATTERN = Pattern.compile(",");
  15. private Point cameraResolution;
  16. private static final int TEN_DESIRED_ZOOM = 27;//设置十级缩放级别
  17. private int previewFormat;
  18. private String previewFormatString;
  19. private CameraConfigurationManager() {
  20. }
  21. public Point getCameraResolution() {
  22. return cameraResolution;
  23. }
  24. public Point getScreenResolution() {
  25. return screenResolution;
  26. }
  27. public int getPreviewFormat() {
  28. return previewFormat;
  29. }
  30. public String getPreviewFormatString() {
  31. return previewFormatString;
  32. }
  33. private static final class Holder {
  34. private static final CameraConfigurationManager CAMERA_CONFIGURATION_MANAGER = new CameraConfigurationManager();
  35. }
  36. public static CameraConfigurationManager getInstance() {
  37. return Holder.CAMERA_CONFIGURATION_MANAGER;
  38. }
  39. public CameraConfigurationManager setContext(Context mContext) {
  40. this.mContext = mContext;
  41. return this;
  42. }
  43. /*获取手机的相机和手机屏幕的一些基本参数*/
  44. public void getCameraParams(Camera camera) {
  45. Camera.Parameters parameters = camera.getParameters();
  46. //获取相机预览界面格式
  47. previewFormat = parameters.getPreviewFormat();
  48. previewFormatString = parameters.get("preview-format");
  49. WindowManager manager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
  50. Display display = manager.getDefaultDisplay();
  51. //获取屏幕的宽高(像素值)设置到一个实体(point)里面
  52. screenResolution = new Point(display.getWidth(), display.getHeight());
  53. //尽量获取相机最好的point
  54. cameraResolution = getCameraResolution(parameters, screenResolution);
  55. }
  56. /*设置相机的预览参数(渴望设置成什么样就设置成什么样)*/
  57. public void setDseiredCameraParameters(Camera camera) {
  58. Camera.Parameters parameters = camera.getParameters();
  59. parameters.setPreviewSize(cameraResolution.x, cameraResolution.y);//设置相机的预览size
  60. setFlash(parameters);
  61. setZoom(parameters);
  62. setDisplayOrientation(camera, 90);
  63. camera.setParameters(parameters);
  64. }
  65. /*设置展示角度*/
  66. private void setDisplayOrientation(Camera camera, int orientation) {
  67. Class<? extends Camera> clazz = camera.getClass();
  68. try {
  69. Method displayOrientation = clazz.getMethod("setDisplayOrientation", new Class[]{int.class});
  70. if (displayOrientation != null) {
  71. displayOrientation.invoke(camera, new Object[]{orientation});
  72. }
  73. } catch (Exception e) {
  74. e.printStackTrace();
  75. }
  76. }
  77. /*设置缩放*/
  78. private void setZoom(Camera.Parameters parameters) {
  79. String zoomSupportString = parameters.get("zoom-supported");
  80. if (zoomSupportString != null && !Boolean.parseBoolean(zoomSupportString)) {
  81. return;
  82. }
  83. int tenDesiredZoom = TEN_DESIRED_ZOOM;//TEN_DESIRED_ZOOM = 27
  84. String maxZoomString = parameters.get("max-zoom");
  85. if (maxZoomString != null) {
  86. try {
  87. int tenMaxZoom = (int) (10.0 * Double.parseDouble(maxZoomString));
  88. if (tenDesiredZoom > tenMaxZoom) {
  89. tenDesiredZoom = tenMaxZoom;
  90. }
  91. } catch (NumberFormatException e) {
  92. Log.i(TAG, "获取原本的缩放最大级别转换失败");
  93. }
  94. String takingPictureZoomMaxString = parameters.get("taking-picture-zoom-max");
  95. if (takingPictureZoomMaxString != null) {
  96. try {
  97. int takePicMax = Integer.parseInt(takingPictureZoomMaxString);
  98. if (tenDesiredZoom > takePicMax) {
  99. tenDesiredZoom = takePicMax;
  100. }
  101. Log.i(TAG, "拍照缩放级别被最原始级别小");
  102. } catch (NumberFormatException nfe) {
  103. Log.i(TAG, "拍照缩放级别转换失败");
  104. }
  105. }
  106. String motZoomMaxString = parameters.get("mot-zoom-values");
  107. if (motZoomMaxString != null) {
  108. tenDesiredZoom = findBestMotZoomValue(motZoomMaxString, tenDesiredZoom);
  109. }
  110. String motZoomStepString = parameters.get("mot-zoom-step");
  111. if (motZoomStepString != null) {
  112. try {
  113. double motZoomStep = Double.parseDouble(motZoomStepString.trim());
  114. int tenZoomStep = (int) (10.0 * motZoomStep);
  115. if (tenZoomStep > 1) {
  116. tenDesiredZoom -= tenDesiredZoom % tenZoomStep;
  117. }
  118. } catch (NumberFormatException nfe) {
  119. }
  120. }
  121. if (maxZoomString != null || motZoomMaxString != null) {
  122. parameters.set("zoom", String.valueOf(tenDesiredZoom / 10.0));
  123. }
  124. if (takingPictureZoomMaxString != null) {
  125. parameters.set("taking-picture-zoom", tenDesiredZoom);
  126. }
  127. }
  128. }
  129. private static int findBestMotZoomValue(CharSequence stringValues, int tenDesireZoom) {
  130. int tenBestValue = 0;
  131. for (String stringValue : COMMA_PATTERN.split(stringValues)) {
  132. stringValue = stringValue.trim();
  133. double value;
  134. try {
  135. value = Double.parseDouble(stringValue);
  136. } catch (NumberFormatException nfe) {
  137. return tenDesireZoom;
  138. }
  139. int tenValue = (int) (10.0 * value);
  140. if (Math.abs(tenDesireZoom - value) < Math.abs(tenDesireZoom - tenBestValue)) {
  141. tenBestValue = tenValue;
  142. }
  143. }
  144. return tenBestValue;
  145. }
  146. /*设置闪光灯,本来是不用设置的,应该是直接获取,但是根据三星建议说防止黑客攻击,最好设置一下*/
  147. private void setFlash(Camera.Parameters parameters) {
  148. if (Build.MODEL.contains("Behold II") && CameraManager.SDK_INT == 3) {
  149. parameters.set("flash-value", 1);
  150. } else {
  151. parameters.set("flash-value", 2);
  152. }
  153. parameters.set("flash-mode", "off");
  154. }
  155. /*根据相机的参数和手机屏幕参数来重新设置相机的*/
  156. public Point getCameraResolution(Camera.Parameters parameters, Point screenResolution) {
  157. String previewSizeValueString = parameters.get("preview-size-values");
  158. if (previewSizeValueString == null) {
  159. previewSizeValueString = parameters.get("preview-size-value");
  160. }
  161. Point cameraResolution = null;
  162. if (previewSizeValueString != null) {
  163. cameraResolution = findBestPreviewSizeValue(previewSizeValueString, screenResolution);
  164. }
  165. if (cameraResolution == null) {
  166. //没有找到最优的预览效果,那么至少也要确保相机分辨率是8的倍数(我也不知道为什么),下面的操作就是防备屏幕不是8的倍数,如果
  167. //是8的倍数,就会在上面的寻找最优size的时候会找到的
  168. cameraResolution = new Point(
  169. (screenResolution.x >> 3) << 3,
  170. (screenResolution.y >> 3) << 3);
  171. }
  172. return cameraResolution;
  173. }
  174. /*根据预览大小和手机屏幕大小来设一个比较完美的预览效果*/
  175. public Point findBestPreviewSizeValue(CharSequence previewSizeValueString, Point screenResolution) {
  176. int bestX = 0;
  177. int bestY = 0;
  178. int diff = Integer.MAX_VALUE;//差值
  179. for (String previewSize : COMMA_PATTERN.split(previewSizeValueString)) {
  180. previewSize = previewSize.trim();
  181. int dimPosition = previewSize.indexOf('x');
  182. if (dimPosition < 0) {
  183. //这是一个很差的预览大小
  184. continue;
  185. }
  186. int newX;
  187. int newY;
  188. try {
  189. newX = Integer.parseInt(previewSize.substring(0, dimPosition));
  190. newY = Integer.parseInt(previewSize.substring(dimPosition + 1));
  191. } catch (NumberFormatException nfe) {
  192. //字符串转换异常,这是自己写的tryCatch,是为了安全起见
  193. continue;
  194. }
  195. int newDiff = Math.abs(newX - screenResolution.x) + Math.abs(newY - screenResolution.y);
  196. if (newDiff == 0) {
  197. bestX = newX;
  198. bestY = newY;
  199. break;
  200. } else if (newDiff < diff) {
  201. bestX = newX;
  202. bestY = newY;
  203. diff = newDiff;
  204. }
  205. }
  206. if (bestY > 0 && bestX > 0) {
  207. return new Point(bestX, bestY);
  208. }
  209. return null;
  210. }
  211. }



 

    

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/102813
推荐阅读
相关标签
  

闽ICP备14008679号