部分Android手機拍照後照片被旋轉的解決方案

來源:互聯網
上載者:User

標籤:android   style   io   os   使用   ar   java   sp   資料   

在部分Android手機(如MT788、Note2)上,使用Camera拍照以後,得到的照片會被自動旋轉(90°、180°、270°),這個情況很不符合預期。仔細分析了一下,因為照片屬性中是儲存了旋轉資訊的,所以要解決這個問題,可以在onActivityResult方法中,擷取到照片資料後,讀取它的旋轉資訊,如果不是0,說明這個照片已經被旋轉過了,那麼再使用android.graphics.Matrix將照片旋轉回去即可。

1、讀取圖片的旋轉屬性

/** * 讀取圖片的旋轉的角度 * * @param path *            圖片絕對路徑 * @return 圖片的旋轉角度 */private int getBitmapDegree(String path) {    int degree = 0;    try {        // 從指定路徑下讀取圖片,並擷取其EXIF資訊        ExifInterface exifInterface = new ExifInterface(path);        // 擷取圖片的旋轉資訊        int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION,                ExifInterface.ORIENTATION_NORMAL);        switch (orientation) {        case ExifInterface.ORIENTATION_ROTATE_90:            degree = 90;            break;        case ExifInterface.ORIENTATION_ROTATE_180:            degree = 180;            break;        case ExifInterface.ORIENTATION_ROTATE_270:            degree = 270;            break;        }    } catch (IOException e) {        e.printStackTrace();    }    return degree;}

2、將圖片按照某個角度進行旋轉

/** * 將圖片按照某個角度進行旋轉 * * @param bm *            需要旋轉的圖片 * @param degree *            旋轉角度 * @return 旋轉後的圖片 */public static Bitmap rotateBitmapByDegree(Bitmap bm, int degree) {    Bitmap returnBm = null;      // 根據旋轉角度,產生旋轉矩陣    Matrix matrix = new Matrix();    matrix.postRotate(degree);    try {        // 將原始圖片按照旋轉矩陣進行旋轉,並得到新的圖片        returnBm = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), matrix, true);    } catch (OutOfMemoryError e) {    }    if (returnBm == null) {        returnBm = bm;    }    if (bm != returnBm) {        bm.recycle();    }    return returnBm;}

OK,用上面兩個方法,就能搞定了!

部分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.