首页 > 系统 > Android > 正文

Android编程中调用Camera时预览画面有旋转问题的解决方法

2019-10-24 20:34:11
字体:
来源:转载
供稿:网友

这篇文章主要介绍了Android编程中调用Camera时预览画面有旋转问题的解决方法,涉及Android针对Camera调用摄像头源码部分的相关修改技巧,需要的朋友可以参考下

本文实例讲述了Android编程中调用Camera时预览画面有旋转问题的解决方法。分享给大家供大家参考,具体如下:

在调用Camera写应用的时候,前后摄像头的情况有时候是不一样的。有时候,明明后摄像头没有问题,而调用到前摄像头时,却倒转了180°,或者其他角度,百思不得其解。在查看了Android源码之后,发现它的解决办法很是好,接下来贴个源码,以备日后查看。

 

 
  1. public static int getDisplayRotation(Activity activity) { 
  2. int rotation = activity.getWindowManager().getDefaultDisplay() 
  3. .getRotation(); 
  4. switch (rotation) { 
  5. case Surface.ROTATION_0: return 0; 
  6. case Surface.ROTATION_90: return 90; 
  7. case Surface.ROTATION_180: return 180; 
  8. case Surface.ROTATION_270: return 270; 
  9. return 0; 
  10. public static void setCameraDisplayOrientation(Activity activity, 
  11. int cameraId, Camera camera) { 
  12. // See android.hardware.Camera.setCameraDisplayOrientation for 
  13. // documentation. 
  14. Camera.CameraInfo info = new Camera.CameraInfo(); 
  15. Camera.getCameraInfo(cameraId, info); 
  16. int degrees = getDisplayRotation(activity); 
  17. int result; 
  18. if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) { 
  19. result = (info.orientation + degrees) % 360; 
  20. result = (360 - result) % 360; // compensate the mirror 
  21. else { // back-facing 
  22. result = (info.orientation - degrees + 360) % 360; 
  23. camera.setDisplayOrientation(result); 

在调用Camera的时候只要调用setCameraDisplayOrientation这个方法就可以了。

希望本文所述对大家Android程序设计有所帮助。


注:相关教程知识阅读请移步到Android开发频道。
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表