android旋轉照片/圖片

來源:互聯網
上載者:User

標籤:android   style   blog   http   color   io   ar   strong   for   

今天比較閑(是任務做完了,不是偷懶),就多更新幾篇,補一下之前做的東西。

養成好習慣,先推薦閱讀:

Android圖片處理:識別映像方向並顯示執行個體教程

android中調用系統相機得到的相片的方向判斷

 

做圖片旋轉前我考慮了一個問題,就是把android裝置反著拿拍照,開啟照片的時候是不是反著的。測試後發現,不管我是正著拍照還是倒著拍照,在任何(其實是大部分)裝置裡的圖片瀏覽器都能把照片正著讀出來,所以我就想這個照片裡肯定有什麼資訊,而且是標準資訊,來表示照片的正方向。

後來查了資料,發現有這麼個玩意:EXIF

有標準,就找介面用就是了。。

另外一個需要知道就就是映像資料旋轉怎麼搞。

用createBitmap(Bitmap source, int x, int y, int width, int height, Matrix m, boolean filter)就好。。

方法說明:

Bitmap android.graphics.Bitmap.createBitmap(Bitmap source, int x, int y, int width, int height, Matrix m, boolean filter)

 

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.

下面放代碼:

讀取exif資訊,找圖片正方向的旋轉角度

 1 private int readPictureDegree(String path) { 2         int degree = 0; 3         try { 4             ExifInterface exifInterface = new ExifInterface(path); 5             int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, 6                     ExifInterface.ORIENTATION_NORMAL); 7             switch (orientation) { 8             case ExifInterface.ORIENTATION_ROTATE_90: 9                 degree = 90;10                 break;11             case ExifInterface.ORIENTATION_ROTATE_180:12                 degree = 180;13                 break;14             case ExifInterface.ORIENTATION_ROTATE_270:15                 degree = 270;16                 break;17             }18         } catch (IOException e) {19             e.printStackTrace();20         }21         return degree;22     }

找出角度就旋轉吧,讓其轉回到正方向顯示

 1     private static Bitmap rotate(Bitmap b, int degrees) { 2         if (degrees == 0) { 3             return b; 4         } 5         if (degrees != 0 && b != null) { 6             Matrix m = new Matrix(); 7             m.setRotate(degrees, (float) b.getWidth(), (float) b.getHeight()); 8             try { 9                 Bitmap b2 = Bitmap.createBitmap(b, 0, 0, b.getWidth(), b.getHeight(), m, true);10                 if (b != b2) {11                     b.recycle();12                     b = b2;13                 }14             } catch (OutOfMemoryError ex) {15             }16         }17         return b;18     }

這個基本上沒問題。。也許有的板子會無法讀取到正方向吧。

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.