android 图片拍照图片旋转的处理方式

第一种:String str=path;

 

            

/**
* 读取图片属性:旋转的角度
*
* @param path
* 图片绝对路径
* @return degree旋转的角度
*/
private void readPictureDegree(String filePath) {
int angle = 0;
try {
ExifInterface exifInterface = new ExifInterface(filePath);
int orientation = exifInterface.getAttributeInt(
ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL);

switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
angle = 90;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
angle = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_270:
angle = 270;
break;
}
} catch (IOException e) {
e.printStackTrace();
}

if (filePath != null) {
Bitmap newBitmap = BitmapFactory.decodeFile(filePath);// 根据Path读取资源图片
Bitmap bitmap = ImageTools
.zoomBitmap(newBitmap, newBitmap.getWidth() / SCALE,
newBitmap.getHeight() / SCALE);

if (angle != 0) {
// 下面的方法主要作用是把图片转一个角度,也可以放大缩小等
Matrix m = new Matrix();

int width = bitmap.getWidth();
int height = bitmap.getHeight();

m.setRotate(angle); // 旋转angle度

bitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, m,
true);// 从新生成图片

}
if (bitmap != null) {
mListBitmap.add(bitmap);
creatImage(mListBitmap);
}
// 由于Bitmap内存占用较大,这里需要回收内存,否则会报out of memory异常
// bitmap.recycle();
}
}

第二种:Uri uri=null;

  

private void setImage(Uri mImageCaptureUri) {
// 不管是拍照还是选择图片每张图片都有在数据中存储也存储有对应旋转角度orientation值
// 所以我们在取出图片是把角度值取出以便能正确的显示图片,没有旋转时的效果观看

ContentResolver cr = this.getContentResolver();
Cursor cursor = cr.query(mImageCaptureUri, null, null, null, null);// 根据Uri从数据库中找
if (cursor != null) {
cursor.moveToFirst();// 把游标移动到首位,因为这里的Uri是包含ID的所以是唯一的不需要循环找指向第一个就是了
String filePath = cursor.getString(cursor.getColumnIndex("_data"));// 获取图片路
String orientation = cursor.getString(cursor
.getColumnIndex("orientation"));// 获取旋转的角度
cursor.close();
if (filePath != null) {
Bitmap newBitmap = BitmapFactory.decodeFile(filePath);// 根据Path读取资源图片
Bitmap bitmap = ImageTools.zoomBitmap(newBitmap,
newBitmap.getWidth() / SCALE, newBitmap.getHeight()
/ SCALE);

int angle = 0;
if (orientation != null && !"".equals(orientation)) {
angle = Integer.parseInt(orientation);
}
if (angle != 0) {
// 下面的方法主要作用是把图片转一个角度,也可以放大缩小等
Matrix m = new Matrix();

int width = bitmap.getWidth();
int height = bitmap.getHeight();

m.setRotate(angle); // 旋转angle度

bitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height,
m, true);// 从新生成图片

}
if (bitmap != null) {
mListBitmap.add(bitmap);
creatImage(mListBitmap);
}
// 由于Bitmap内存占用较大,这里需要回收内存,否则会报out of memory异常
// bitmap.recycle();
}
}
}

 

posted on 2013-11-25 10:11  yujian_bcq  阅读(846)  评论(0编辑  收藏  举报

导航