Android图片处理:识别图像方向并显示

在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)
Added in  API level 1

Returns an immutable bitmap from subset of the source bitmap, transformed by the optional matrix. The new bitmap may be the same object as source, or a copy may have been made. It is initialized with the same density as the original bitmap. If the source bitmap is immutable and the requested subset is the same as the source bitmap itself, then the source bitmap is returned and no new bitmap is created.

Parameters
source The bitmap we are subsetting
x The x coordinate of the first pixel in source
y The y coordinate of the first pixel in source
width The number of pixels in each row
height The number of rows
m Optional matrix to be applied to the pixels
filter true if the source should be filtered. Only applies if the matrix contains more than just translation.
Returns
  • A bitmap that represents the specified subset of source
Throws
IllegalArgumentException if the x, y, width, height values are outside of the dimensions of the source bitmap.


到此这两个问题理论上都解决了,开始实际操作一下吧,参照以下代码。

[java]  view plain  copy
 print ?
  1. public class IOHelper {  
  2.       
  3.     ......  
  4.       
  5.     /** 从给定路径加载图片*/  
  6.     public static Bitmap loadBitmap(String imgpath) {  
  7.         return BitmapFactory.decodeFile(imgpath);  
  8.     }  
  9.   
  10.       
  11.     /** 从给定的路径加载图片,并指定是否自动旋转方向*/  
  12.     public static Bitmap loadBitmap(String imgpath, boolean adjustOritation) {  
  13.         if (!adjustOritation) {  
  14.             return loadBitmap(imgpath);  
  15.         } else {  
  16.             Bitmap bm = loadBitmap(imgpath);  
  17.             int digree = 0;  
  18.             ExifInterface exif = null;  
  19.             try {  
  20.                 exif = new ExifInterface(imgpath);  
  21.             } catch (IOException e) {  
  22.                 e.printStackTrace();  
  23.                 exif = null;  
  24.             }  
  25.             if (exif != null) {  
  26.                 // 读取图片中相机方向信息  
  27.                 int ori = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,  
  28.                         ExifInterface.ORIENTATION_UNDEFINED);  
  29.                 // 计算旋转角度  
  30.                 switch (ori) {  
  31.                 case ExifInterface.ORIENTATION_ROTATE_90:  
  32.                     digree = 90;  
  33.                     break;  
  34.                 case ExifInterface.ORIENTATION_ROTATE_180:  
  35.                     digree = 180;  
  36.                     break;  
  37.                 case ExifInterface.ORIENTATION_ROTATE_270:  
  38.                     digree = 270;  
  39.                     break;  
  40.                 default:  
  41.                     digree = 0;  
  42.                     break;  
  43.                 }  
  44.             }  
  45.             if (digree != 0) {  
  46.                 // 旋转图片  
  47.                 Matrix m = new Matrix();  
  48.                 m.postRotate(digree);  
  49.                 bm = Bitmap.createBitmap(bm, 00, bm.getWidth(),  
  50.                         bm.getHeight(), m, true);  
  51.             }  
  52.             return bm;  
  53.         }  
  54.     }  
  55.           
  56.     ......  
  57. }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值