Android图片处理:识别图像方向并显示实例教程

时间:2023-02-11 08:01:07 

在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) 
Android图片处理:识别图像方向并显示实例教程
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;
}
}
......
}
标签:图片处理,图像方向
0
投稿

猜你喜欢

  • Java环境下高德地图Api的使用方式

    2022-06-13 06:43:59
  • SpringMvc MultipartFile实现图片文件上传示例

    2022-07-30 16:40:45
  • Spring中Bean扫描原理详情

    2022-05-26 04:33:14
  • Java concurrency之公平锁(二)_动力节点Java学院整理

    2023-11-24 21:14:54
  • SpringMVC使用hibernate-validator进行参数校验最佳实践记录

    2022-11-12 23:44:04
  • android实现widget时钟示例分享

    2021-09-09 09:24:55
  • 在@Value注解内使用SPEL自定义函数方式

    2022-04-26 20:59:41
  • 拉钩网java笔试题分享

    2022-02-13 08:48:25
  • Android UI效果之绘图篇(三)

    2022-01-03 03:01:08
  • 举例解析Java的设计模式编程中里氏替换原则的意义

    2021-06-30 18:37:17
  • Android WebView支持input file启用相机/选取照片功能

    2023-09-15 07:56:48
  • java的三种随机数生成方式

    2022-03-06 13:43:57
  • Java Collection集合的三种遍历方式详解

    2023-04-29 08:11:40
  • 关于MyBatis中Mapper XML热加载优化

    2023-05-20 01:49:34
  • Java实现简单的日历界面

    2021-10-08 03:13:01
  • 第三方开源Android TickPlusDrawable状态可以通过动画切换的按钮

    2022-11-20 01:21:18
  • Java调用接口如何获取json数据解析后保存到数据库

    2023-11-16 15:01:36
  • Android实现图片加载进度提示

    2022-09-11 17:54:44
  • Android编程实现下载图片及在手机中展示的方法

    2023-10-26 12:30:59
  • SpringBoot整合Redis将对象写入redis的实现

    2023-07-30 14:39:07
  • asp之家 软件编程 m.aspxhome.com