android应用里使用相机图片时必须要考虑的一个问题就是图片朝向,只有判断对朝向才能调整图片从而更好的展现。本文将介绍一种通过ExifInterface判断图片朝向的方法!
上代码:
代码如下 | 复制代码 |
/** * * 利用给定路径下的图片设置ImageView * * @param imgPath 手机图片文件路径 * @param imgView 需要设置的ImageView */ publicvoidsetImg(String imgPath, ImageView imgView) { File file =newFile(imgPath); if(file.exists() && file.canRead()) { // -------1.图片缩放--------
// 手机屏幕信息 DisplayMetrics metric =newDisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(metric); intdw = metric.widthPixels;// 屏幕宽 intdh = metric.heightPixels;// 屏幕高
// 加载图像,只是为了获取尺寸 BitmapFactory.Options options =newBitmapFactory.Options(); options.inJustDecodeBounds =true;// 设置之后可以获取尺寸信息 Bitmap bitmap = BitmapFactory.decodeFile(imgPath, options); // 计算水平和垂直缩放系数 intheightRatio = (int) Math.ceil(options.outHeight / (float) dh); intwidthRatio = (int) Math.ceil(options.outWidth / (float) dw); // 判断哪个大 if(heightRatio >1&& widthRatio >1) { if(heightRatio > widthRatio) { options.inSampleSize = heightRatio; }else{ options.inSampleSize = widthRatio; } } // 图片缩放 options.inJustDecodeBounds =false; bitmap = BitmapFactory.decodeFile(imgPath, options);
// -------2.判断图片朝向-------- try{ ExifInterface exif =newExifInterface(imgPath); intdegree =0;// 图片旋转角度 if(exif !=null) { intorientation = exif.getAttributeInt( ExifInterface.TAG_ORIENTATION, -1); if(orientation != -1) { switch(orientation) { caseExifInterface.ORIENTATION_ROTATE_90: degree =90; break;
caseExifInterface.ORIENTATION_ROTATE_180: degree =180; break;
caseExifInterface.ORIENTATION_ROTATE_270: degree =270; break; default: break; } } }
if(degree !=0) {// 图片需要旋转 intwidth = bitmap.getWidth(); intheight = bitmap.getHeight(); Matrix matrix =newMatrix(); matrix.preRotate(degree); Bitmap mRotateBitmap = Bitmap.createBitmap(bitmap,0,0, width, height, matrix,true);
imgView.setImageBitmap(mRotateBitmap); }else{ imgView.setImageBitmap(bitmap); } }catch(IOException e) { } } } |
本代码包含两大功能:
1. 图片缩放:原始图片一般比较大,经过缩小才能使用;
2. 图片旋转:由于用户拍照时手机角度不同,所得照片可能需要旋转。
凡尔赛法环免广告版 最新版v1.69.1
下载天煞异种 安卓版v1.0.21
下载十三月 (Undecember)最新官方中文版v3.26.0200
下载我不是废柴无限99999钻999999金币游戏 不减反增版v1.2.16
下载我的女友是声优免广告 安卓版v1
我的女友是声优免广告是一款非常好玩的模拟经营类手游,玩家在游
石器帝国去广告版 v8
石器帝国免广告版是游戏的破解版本,在该版本中为玩家去除了广告
校园恋爱模拟器真人版去广告版 v0.0.7
校园恋爱模拟器真人版免广告版是游戏的破解版本,在该版本中为玩
教练我要开球馆内置菜单修改器 安卓版v1.5.4
教练我要开球馆内购版是一款模拟经营类游戏,玩家们将在游戏中创
我的游轮无限砖石 安卓版v1.7.0
我的游轮无限金币钻石版是一款模拟经营类游戏,你将成为一艘游轮