Android图片处理:识别图像方向并显示实例教程
更新时间:2013年06月16日 16:38:58 作者:
在Android中使用ImageView显示图片的时候发现图片显示不正,方向偏了或者倒过来了,下面与大家分享下具体的解决方法,感性的朋友可以参考下
在Android中使用ImageView显示图片的时候发现图片显示不正,方向偏了或者倒过来了。
解决这个问题很自然想到的分两步走:
1、自动识别图像方向,计算旋转角度;
2、对图像进行旋转并显示。
一、识别图像方向
首先在这里提一个概念EXIF(Exchangeable Image File Format,可交换图像文件),具体解释参见Wiki。
简而言之,Exif是一个标准,用于电子照相机(也包括手机、扫描器等)上,用来规范图片、声音、视屏以及它们的一些辅助标记格式。
Exif支持的格式如下:
图像
压缩图像文件:JPEG、DCT
非压缩图像文件:TIFF
不支持:JPEG 2000、PNG、GIF
音频
RIFF、WAV
Android提供了对JPEG格式图像Exif接口支持,可以读取JPEG文件metadata信息,参见ExifInterface.
这些Metadata信息总的来说大致分为三类:日期时间、空间信息(经纬度、高度)、Camera信息(孔径、焦距、旋转角、曝光量等等)。
二、图像旋转
Android中提供了对Bitmap进行矩阵旋转的操作,参见Bitmap提供的静态createBitmap方法.
public static Bitmap createBitmap (Bitmap source, int x, int y, int width, int height, Matrix m, boolean filter)
IllegalArgumentException if the x, y, width, height values are outside of the dimensions of the source bitmap.
到此这两个问题理论上都解决了,开始实际操作一下吧,参照以下代码。
public class IOHelper {
......
/** 从给定路径加载图片*/
public static Bitmap loadBitmap(String imgpath) {
return BitmapFactory.decodeFile(imgpath);
}
/** 从给定的路径加载图片,并指定是否自动旋转方向*/
public static Bitmap loadBitmap(String imgpath, boolean adjustOritation) {
if (!adjustOritation) {
return loadBitmap(imgpath);
} else {
Bitmap bm = loadBitmap(imgpath);
int digree = 0;
ExifInterface exif = null;
try {
exif = new ExifInterface(imgpath);
} catch (IOException e) {
e.printStackTrace();
exif = null;
}
if (exif != null) {
// 读取图片中相机方向信息
int ori = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_UNDEFINED);
// 计算旋转角度
switch (ori) {
case ExifInterface.ORIENTATION_ROTATE_90:
digree = 90;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
digree = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_270:
digree = 270;
break;
default:
digree = 0;
break;
}
}
if (digree != 0) {
// 旋转图片
Matrix m = new Matrix();
m.postRotate(digree);
bm = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(),
bm.getHeight(), m, true);
}
return bm;
}
}
......
}
解决这个问题很自然想到的分两步走:
1、自动识别图像方向,计算旋转角度;
2、对图像进行旋转并显示。
一、识别图像方向
首先在这里提一个概念EXIF(Exchangeable Image File Format,可交换图像文件),具体解释参见Wiki。
简而言之,Exif是一个标准,用于电子照相机(也包括手机、扫描器等)上,用来规范图片、声音、视屏以及它们的一些辅助标记格式。
Exif支持的格式如下:
图像
压缩图像文件:JPEG、DCT
非压缩图像文件:TIFF
不支持:JPEG 2000、PNG、GIF
音频
RIFF、WAV
Android提供了对JPEG格式图像Exif接口支持,可以读取JPEG文件metadata信息,参见ExifInterface.
这些Metadata信息总的来说大致分为三类:日期时间、空间信息(经纬度、高度)、Camera信息(孔径、焦距、旋转角、曝光量等等)。
二、图像旋转
Android中提供了对Bitmap进行矩阵旋转的操作,参见Bitmap提供的静态createBitmap方法.
public static Bitmap createBitmap (Bitmap source, int x, int y, int width, int height, Matrix m, boolean filter)
IllegalArgumentException if the x, y, width, height values are outside of the dimensions of the source bitmap.
到此这两个问题理论上都解决了,开始实际操作一下吧,参照以下代码。
复制代码 代码如下:
public class IOHelper {
......
/** 从给定路径加载图片*/
public static Bitmap loadBitmap(String imgpath) {
return BitmapFactory.decodeFile(imgpath);
}
/** 从给定的路径加载图片,并指定是否自动旋转方向*/
public static Bitmap loadBitmap(String imgpath, boolean adjustOritation) {
if (!adjustOritation) {
return loadBitmap(imgpath);
} else {
Bitmap bm = loadBitmap(imgpath);
int digree = 0;
ExifInterface exif = null;
try {
exif = new ExifInterface(imgpath);
} catch (IOException e) {
e.printStackTrace();
exif = null;
}
if (exif != null) {
// 读取图片中相机方向信息
int ori = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_UNDEFINED);
// 计算旋转角度
switch (ori) {
case ExifInterface.ORIENTATION_ROTATE_90:
digree = 90;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
digree = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_270:
digree = 270;
break;
default:
digree = 0;
break;
}
}
if (digree != 0) {
// 旋转图片
Matrix m = new Matrix();
m.postRotate(digree);
bm = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(),
bm.getHeight(), m, true);
}
return bm;
}
}
......
}
相关文章
Android 使用AlarmManager和NotificationManager来实现闹钟和通知栏
这篇文章主要介绍了Android 使用AlarmManager和NotificationManager来实现闹钟和通知栏,需要的朋友可以参考下2017-02-02解决Android Studio 格式化快捷键和QQ 锁键盘快捷键冲突问题
每次打开qq使用android studio格式化的快捷键Ctrl + Alt +L时,总是出现qq锁键盘提示,怎么回事呢?下面小编给大家带来了android studio格式化的快捷键和qq快捷键之间的冲突的处理方法,需要的朋友参考下吧2017-12-12Android UI设计系列之自定义Dialog实现各种风格的对话框效果(7)
这篇文章主要介绍了Android UI设计系列之自定义Dialog实现各种风格的对话框效果,具有一定的实用性和参考价值,感兴趣的小伙伴们可以参考一下2016-06-06Android开发案例手册Application跳出dialog
这篇文章主要为大家介绍了Android开发案例手册Application跳出dialog,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪2022-06-06
最新评论