Android圖片處理:識別映像方向並顯示

來源:互聯網
上載者:User

標籤:des   android   style   blog   http   io   color   ar   os   

在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.


到此這兩個問題理論上都解決了,開始實際操作一下吧,參照以下代碼。

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圖片處理:識別映像方向並顯示

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.