android 拍照图片旋转问题

[java]  view plain copy
  1.   
前阵子写了一个拍照的程序,拍完照片图片怎么看都是歪的,找了好久借鉴了很多博客找到了解决的办法,不说了 看代码把
[java]  view plain copy
  1. mOrientationListener = new OrientationEventListener(this){  
  2.             @Override  
  3.             public void onOrientationChanged(int orientation) {  
  4.                 orientations =orientation;  
  5.                 Log.v("time""现在是横屏"+orientation);  
  6.                   
  7.             }  
  8.         };  

在oncreate方法中添加OrientationEventListener,OrientationEventListener(方向事件监听器)是一个当方向发生变化时, 从 SensorManager(传感器管理程序)接收通知的辅助类,我就是通过这个来判断手机屏幕的旋转角度,从而将获取后的图片做相应的旋转。

接下来我在onResume中启动事件的监听器

[html]  view plain copy
  1. if(mOrientationListener!=null){//先判断下防止出现空指针异常  
  2.             mOrientationListener.enable();  
  3.         }  

然后就可以在onPictureTaken中将图片旋转相应的角度:

[java]  view plain copy
  1. BitmapFactory.Options opts = new BitmapFactory.Options();  
  2.                 opts.inPreferredConfig = Config.RGB_565;  
  3.                 opts.inSampleSize =4;   
  4.                 Bitmap bmp = BitmapFactory.decodeByteArray(images, 0, images.length,opts);  
  5.                 Matrix matrixs = new Matrix();  
  6.                     if(orientations > 325 || orientations <= 45){  
  7.                         Log.v("time""Surface.ROTATION_0;"+orientations);  
  8.                         matrixs.setRotate(90);  
  9.                     }else if(orientations > 45 && orientations <= 135){  
  10.                         Log.v("time"" Surface.ROTATION_270"+orientations);  
  11.                     matrixs.setRotate(180);  
  12.                     }else if(orientations > 135 && orientations < 225){  
  13.                         Log.v("time""Surface.ROTATION_180;"+orientations);  
  14.                     matrixs.setRotate(270);  
  15.                     }else {  
  16.                         Log.v("time""Surface.ROTATION_90"+orientations);  
  17.                     matrixs.setRotate(0);  
  18.                     }  
  19.                 bmp = Bitmap.createBitmap(bmp, 00, bmp.getWidth(), bmp.getHeight(), matrixs, true);  

这样图片就旋转过来了,最后别忘了在onPause中将事件监听器关掉:

[java]  view plain copy
  1. if(mOrientationListener!=null){  
  2.             mOrientationListener.disable();  
  3.         }  

ok!这样就完成了。这算是一点小小的收获吧!

GZ应届大学生IT   it菜鸟营
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值