亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android圖片處理:識(shí)別圖像方向并顯示實(shí)例教程

 更新時(shí)間:2013年06月16日 16:38:58   作者:  
在Android中使用ImageView顯示圖片的時(shí)候發(fā)現(xiàn)圖片顯示不正,方向偏了或者倒過(guò)來(lái)了,下面與大家分享下具體的解決方法,感性的朋友可以參考下
在Android中使用ImageView顯示圖片的時(shí)候發(fā)現(xiàn)圖片顯示不正,方向偏了或者倒過(guò)來(lái)了。
解決這個(gè)問(wèn)題很自然想到的分兩步走:
1、自動(dòng)識(shí)別圖像方向,計(jì)算旋轉(zhuǎn)角度;
2、對(duì)圖像進(jìn)行旋轉(zhuǎn)并顯示。

一、識(shí)別圖像方向
首先在這里提一個(gè)概念EXIF(Exchangeable Image File Format,可交換圖像文件),具體解釋參見(jiàn)Wiki。
簡(jiǎn)而言之,Exif是一個(gè)標(biāo)準(zhǔn),用于電子照相機(jī)(也包括手機(jī)、掃描器等)上,用來(lái)規(guī)范圖片、聲音、視屏以及它們的一些輔助標(biāo)記格式。
Exif支持的格式如下:
圖像
壓縮圖像文件:JPEG、DCT
非壓縮圖像文件:TIFF
不支持:JPEG 2000、PNG、GIF
音頻
RIFF、WAV
Android提供了對(duì)JPEG格式圖像Exif接口支持,可以讀取JPEG文件metadata信息,參見(jiàn)ExifInterface.
這些Metadata信息總的來(lái)說(shuō)大致分為三類:日期時(shí)間、空間信息(經(jīng)緯度、高度)、Camera信息(孔徑、焦距、旋轉(zhuǎn)角、曝光量等等)。

二、圖像旋轉(zhuǎn)
Android中提供了對(duì)Bitmap進(jìn)行矩陣旋轉(zhuǎn)的操作,參見(jiàn)Bitmap提供的靜態(tài)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. 
到此這兩個(gè)問(wèn)題理論上都解決了,開(kāi)始實(shí)際操作一下吧,參照以下代碼。
復(fù)制代碼 代碼如下:

public class IOHelper {
......
/** 從給定路徑加載圖片*/
public static Bitmap loadBitmap(String imgpath) {
return BitmapFactory.decodeFile(imgpath);
}
/** 從給定的路徑加載圖片,并指定是否自動(dòng)旋轉(zhuǎn)方向*/
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) {
// 讀取圖片中相機(jī)方向信息
int ori = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_UNDEFINED);
// 計(jì)算旋轉(zhuǎn)角度
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) {
// 旋轉(zhuǎn)圖片
Matrix m = new Matrix();
m.postRotate(digree);
bm = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(),
bm.getHeight(), m, true);
}
return bm;
}
}
......
}

相關(guān)文章

最新評(píng)論