关于后置摄像头拍照后照片方向的总结
来源:互联网 发布:java的计算器源代码 编辑:程序博客网 时间:2024/04/28 15:23
开发android Camera的时候,发现一个问题,就是拍照后图片的方向是错的。如何避免他?可以读取照片的exif信息,exif信息存储了包括时间,照片方向等照片的全部信息。具体步骤如下。
ExifInterface exif = null;
String TAG_ORIENTATION=null;
try {
exif = new ExifInterface(getPhotoPath());
TAG_ORIENTATION=exif.getAttribute(ExifInterface.TAG_ORIENTATION);
Log.d(TAG,"exif方向: "+TAG_ORIENTATION);
} catch (IOException e) {
e.printStackTrace();
}
getPhotoPath() 方法是获得图片的路径,比如/sdcard/1.jpg
我用四个方向进行拍照。经过打印发现,打印的结果分别是6,8,3,1
经过源码查询发现,这几个值是系统定义的方向分别对应应ExifInterface.ORIENTATION_ROTATE_90,ExifInterface.ORIENTATION_ROTATE_270,ExifInterface.ORIENTATION_ROTATE_180和0度
竖屏模式下,逆时针旋转90度,exif打印结果是1,就是0度,就是摄像头的正方向,由此可见android手机的摄像头是横屏方向安装上的
因此我们可以根据exif信息旋转bitmap。
try { ExifInterface exifInterface = new ExifInterface(file.getPath()); int result = exifInterface.getAttributeInt( ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED); int rotate = 0; switch(result) { case ExifInterface.ORIENTATION_ROTATE_90: rotate = 90; break; case ExifInterface.ORIENTATION_ROTATE_180: rotate = 180; break; case ExifInterface.ORIENTATION_ROTATE_270: rotate = 270; break; default: break; } catch (IOException e) { e.printStackTrace(); }下面是旋转bitmap的方法
public static Bitmap rotate(Bitmap b, int degrees,String filePath, int reqWidth, int reqHeight) { if (degrees != 0 && b != null) { Matrix m = new Matrix(); m.setRotate(degrees, (float) b.getWidth() / 2, (float) b.getHeight() / 2); try { Bitmap b2 = Bitmap.createBitmap( b, 0, 0, b.getWidth(), b.getHeight(), m, true); if (b != b2) { b.recycle(); b = b2; } } catch (OutOfMemoryError ex) { // 建议大家如何出现了内存不足异常,最好return 原始的bitmap对象。. return getSampledBitmap(filePath, reqWidth, reqHeight); } } return b; }
public static Bitmap getSampledBitmap(String filePath, int reqWidth, int reqHeight) { BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeFile(filePath, options); // Raw height and width of image final int height = options.outHeight; final int width = options.outWidth; int inSampleSize = 1; if (height > reqHeight || width > reqWidth) { if (width > height) { inSampleSize = (int) FloatMath.floor(((float) height / reqHeight) + 0.5f); //Math.round((float)height / (float)reqHeight); } else { inSampleSize = (int) FloatMath.floor(((float) width / reqWidth) + 0.5f); //Math.round((float)width / (float)reqWidth); } } options.inSampleSize = inSampleSize; options.inJustDecodeBounds = false; return BitmapFactory.decodeFile(filePath, options); }
- 关于后置摄像头拍照后照片方向的总结
- 关于前置摄像头拍照后照片方向的总结(续前篇)
- 调用手机摄像头拍照和从图库中选取照片以及得到照片后的备份
- 关于后副摄像头模组的方向问题
- android 拍照的照片方向问题,读取图片EXIF信息
- 关于swift中自定义相机拍照后,截取照片代码
- Android拍照后显示照片
- Android 拍照后显示照片
- Android自定义照相机 预览拍照 切换前后置摄像头
- Android自定义照相机 预览拍照 切换前后置摄像头
- Android自定义照相机 预览拍照 切换前后置摄像头
- Android自定义照相机 预览拍照 切换前后置摄像头
- Android自定义照相机 预览拍照 切换前后置摄像头
- Android自定义照相机 预览拍照 切换前后置摄像头
- Android自定义照相机 预览拍照 切换前后置摄像头
- Android自定义照相机 预览拍照 切换前后置摄像头
- Android自定义照相机 预览拍照 切换前后置摄像头
- 如何获得android手机拍照后照片的存放目录
- ssh定时bath的配置
- 【※用QQ撤回离线文件测试好友是否隐身※偏方】系统收藏
- MySQL索引背后的数据结构及算法原理
- OSDI, SOSP与美国著名计算机系的调查
- COCOS2d 安装
- 关于后置摄像头拍照后照片方向的总结
- Android中Bitmap和Drawable
- 关于thread.getConextClassLoader()
- BDE错误列表
- 不在沉默中爆发,就在沉默中灭亡
- 如何管理你的客户?别用会员卡积分打折了,把客户变成粉丝
- Some thing about android:visibility
- 面试困惑问与答(2)——感觉挺好,为啥被拒了?
- poj 3608 Bridge Across Islands(旋转卡壳)