1. 程式人生 > >Android 效率開發之圖片旋轉處理

Android 效率開發之圖片旋轉處理

之前寫過一篇 Android 三星手機拍照圖片旋轉處理,其實不止三星拍照的圖片會旋轉,小米的也會。這裡把旋轉的部分重新記錄下:

1,先獲取圖片旋轉角度

  /**
     * 獲取旋轉角度
     * @param path
     * @return
     */
    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,旋轉圖片

                    Matrix matrix = new Matrix();
                    matrix.postRotate(readPictureDegree(imagePath));
                    bmp = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true);
                    FileOutputStream fOut;
                    try {
                        fOut = new FileOutputStream(imagePath);
                        //不壓縮
                        bmp.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
                        fOut.flush();
                        fOut.close();

                    } catch (FileNotFoundException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

當然只有當圖片沒有旋轉的時候,才需要強制去旋轉。