九鼎创展论坛中文版English
登录 | 立即注册 设为首页收藏本站 切换到宽版
查看: 4134|回复: 0

Android解决当调用Camera时预览画面有旋转的问题

[复制链接]
发表于 2014-7-11 19:10:30 | 显示全部楼层 |阅读模式

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

[java] view plaincopy


  • public static int getDisplayRotation(Activity activity) {  
  •     int rotation = activity.getWindowManager().getDefaultDisplay()  
  •        .getRotation();  
  •     switch (rotation) {  
  •         case Surface.ROTATION_0: return 0;  
  •         case Surface.ROTATION_90: return 90;  
  •         case Surface.ROTATION_180: return 180;  
  •         case Surface.ROTATION_270: return 270;  
  •     }  
  •     return 0;  
  • }  
  •   
  • public static void setCameraDisplayOrientation(Activity activity,  
  •         int cameraId, Camera camera) {  
  •     // See android.hardware.Camera.setCameraDisplayOrientation for  
  •     // documentation.  
  •     Camera.CameraInfo info = new Camera.CameraInfo();  
  •     Camera.getCameraInfo(cameraId, info);  
  •     int degrees = getDisplayRotation(activity);  
  •     int result;  
  •     if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {  
  •         result = (info.orientation + degrees) % 360;  
  •         result = (360 - result) % 360;  // compensate the mirror  
  •     } else {  // back-facing  
  •         result = (info.orientation - degrees + 360) % 360;  
  •     }  
  •     camera.setDisplayOrientation(result);  
  • }  


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

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|小黑屋|深圳市九鼎创展科技官方论坛 ( 粤ICP备11028681号-2  

GMT+8, 2024-3-28 20:55 , Processed in 0.024790 second(s), 22 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表