Android解決三星手機圖片旋轉問題

此文章只是記錄自己在項目中遇到的問題,寫下來,給自己提醒,相信接觸過圖片選擇的必定會遇到三星手機從相冊或者是拍照之後拿到圖片路徑,獲取Bitmap對象,圖片大了還得對bitmap進行壓縮,最後顯示在ImageView上,就會發現照片會旋轉90°(我遇見的,不知道其他人旋轉了多少度),解決辦法也是比較方便快捷:


1.通過圖片路徑得到圖片的旋轉角度

public static int readPictureDegree(String path) {
        int degree = 0;
        try {
            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.根據圖片的選裝角度將圖片旋轉回去

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;
    }


通過以上兩部基本上就解決了,剛遇到的時候可能會讓人頭疼,不過解決了會那麼容易,也許這就是小小的一點成長,加油吧


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章