android 拍照图片旋转问题
2013-11-05 11:04
405 查看
[java] view
plaincopy
前阵子写了一个拍照的程序,拍完照片图片怎么看都是歪的,找了好久借鉴了很多博客找到了解决的办法,不说了 看代码把
[java] view
plaincopy
mOrientationListener = new OrientationEventListener(this){
@Override
public void onOrientationChanged(int orientation) {
orientations =orientation;
Log.v("time", "现在是横屏"+orientation);
}
};
在oncreate方法中添加OrientationEventListener,OrientationEventListener(方向事件监听器)是一个当方向发生变化时, 从 SensorManager(传感器管理程序)接收通知的辅助类,我就是通过这个来判断手机屏幕的旋转角度,从而将获取后的图片做相应的旋转。
接下来我在onResume中启动事件的监听器
[html] view
plaincopy
if(mOrientationListener!=null){//先判断下防止出现空指针异常
mOrientationListener.enable();
}
然后就可以在onPictureTaken中将图片旋转相应的角度:
[java] view
plaincopy
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inPreferredConfig = Config.RGB_565;
opts.inSampleSize =4;
Bitmap bmp = BitmapFactory.decodeByteArray(images, 0, images.length,opts);
Matrix matrixs = new Matrix();
if(orientations > 325 || orientations <= 45){
Log.v("time", "Surface.ROTATION_0;"+orientations);
matrixs.setRotate(90);
}else if(orientations > 45 && orientations <= 135){
Log.v("time", " Surface.ROTATION_270"+orientations);
matrixs.setRotate(180);
}else if(orientations > 135 && orientations < 225){
Log.v("time", "Surface.ROTATION_180;"+orientations);
matrixs.setRotate(270);
}else {
Log.v("time", "Surface.ROTATION_90"+orientations);
matrixs.setRotate(0);
}
bmp = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrixs, true);
这样图片就旋转过来了,最后别忘了在onPause中将事件监听器关掉:
[java] view
plaincopy
if(mOrientationListener!=null){
mOrientationListener.disable();
}
ok!这样就完成了。这算是一点小小的收获吧!
plaincopy
前阵子写了一个拍照的程序,拍完照片图片怎么看都是歪的,找了好久借鉴了很多博客找到了解决的办法,不说了 看代码把
[java] view
plaincopy
mOrientationListener = new OrientationEventListener(this){
@Override
public void onOrientationChanged(int orientation) {
orientations =orientation;
Log.v("time", "现在是横屏"+orientation);
}
};
在oncreate方法中添加OrientationEventListener,OrientationEventListener(方向事件监听器)是一个当方向发生变化时, 从 SensorManager(传感器管理程序)接收通知的辅助类,我就是通过这个来判断手机屏幕的旋转角度,从而将获取后的图片做相应的旋转。
接下来我在onResume中启动事件的监听器
[html] view
plaincopy
if(mOrientationListener!=null){//先判断下防止出现空指针异常
mOrientationListener.enable();
}
然后就可以在onPictureTaken中将图片旋转相应的角度:
[java] view
plaincopy
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inPreferredConfig = Config.RGB_565;
opts.inSampleSize =4;
Bitmap bmp = BitmapFactory.decodeByteArray(images, 0, images.length,opts);
Matrix matrixs = new Matrix();
if(orientations > 325 || orientations <= 45){
Log.v("time", "Surface.ROTATION_0;"+orientations);
matrixs.setRotate(90);
}else if(orientations > 45 && orientations <= 135){
Log.v("time", " Surface.ROTATION_270"+orientations);
matrixs.setRotate(180);
}else if(orientations > 135 && orientations < 225){
Log.v("time", "Surface.ROTATION_180;"+orientations);
matrixs.setRotate(270);
}else {
Log.v("time", "Surface.ROTATION_90"+orientations);
matrixs.setRotate(0);
}
bmp = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrixs, true);
这样图片就旋转过来了,最后别忘了在onPause中将事件监听器关掉:
[java] view
plaincopy
if(mOrientationListener!=null){
mOrientationListener.disable();
}
ok!这样就完成了。这算是一点小小的收获吧!
相关文章推荐
- 解决android有的手机拍照后上传图片被旋转的问题
- 解决android有的手机拍照后上传图片被旋转的问题
- Android拍照后获得的图片旋转90度问题
- android解决坚屏拍照和保存图片旋转90度的问题,并兼容4.0
- Android解决三星手机拍照图片调用时逆时针旋转90度问题
- 解决android有的手机拍照后上传图片被旋转的问题
- android 选择图片或拍照时旋转了90度问题
- Uri android 选择图片或拍照时旋转了90度问题
- 解决android有的手机拍照后上传图片被旋转的问题
- android有的手机拍照后上传图片被旋转的问题
- 解决android有的手机拍照后上传图片被旋转的问题
- MVP模式的Android 调用系统拍照,相册,剪裁,适配到7.0,修复拍照图片旋转问题
- 解决android有的手机拍照后上传图片被旋转的问题
- android拍照后图片路径的获取(解决不同手机存在拍照后旋转的问题)
- android手机拍照后上传图片被旋转的问题
- android 部分手机拍照后在imageView中显示预览图片被旋转的问题
- 解决android有的手机拍照后上传图片被旋转的问题
- 解决android有的手机拍照后上传图片被旋转的问题
- 解决android有的手机拍照后上传图片被旋转的问题
- 解决android有的手机拍照后上传图片被旋转的问题