Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux 程式設計 >> Linux 程式設計

Android 相機照片方向錯亂?用 ExifInterface 讀取並修正

在 App 裡使用相機拍攝的照片時,方向問題幾乎一定會遇到:同一張照片在不同裝置上可能一個橫一個豎。要正確呈現,就必須先讀出照片的朝向資訊,再決定要不要旋轉。以下示範以 ExifInterface 判斷並修正方向的完整做法。

/**
 * 依指定路徑載入圖片並設定到 ImageView
 *
 * @param imgPath 手機圖片檔案路徑
 * @param imgView 要設定的 ImageView
 */
public void setImg(String imgPath, ImageView imgView) {
    File file = new File(imgPath);
    if (file.exists() && file.canRead()) {

        // ------- 1. 圖片縮放 -------
        // 取得螢幕資訊
        DisplayMetrics metric = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metric);
        int dw = metric.widthPixels;   // 螢幕寬
        int dh = metric.heightPixels;  // 螢幕高

        // 先只讀尺寸,不真正解碼
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        Bitmap bitmap = BitmapFactory.decodeFile(imgPath, options);

        // 計算水平與垂直縮放係數
        int heightRatio = (int) Math.ceil(options.outHeight / (float) dh);
        int widthRatio = (int) Math.ceil(options.outWidth / (float) dw);

        // 取較大者作為 sampling 倍率
        if (heightRatio > 1 && widthRatio > 1) {
            if (heightRatio > widthRatio) {
                options.inSampleSize = heightRatio;
            } else {
                options.inSampleSize = widthRatio;
            }
        }

        // 正式解碼
        options.inJustDecodeBounds = false;
        bitmap = BitmapFactory.decodeFile(imgPath, options);

        // ------- 2. 判斷圖片朝向 -------
        try {
            ExifInterface exif = new ExifInterface(imgPath);
            int degree = 0;  // 需要旋轉的角度
            if (exif != null) {
                int orientation = exif.getAttributeInt(
                        ExifInterface.TAG_ORIENTATION, -1);
                if (orientation != -1) {
                    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;
                        default:
                            break;
                    }
                }
            }

            if (degree != 0) {  // 需要旋轉
                int width = bitmap.getWidth();
                int height = bitmap.getHeight();
                Matrix matrix = new Matrix();
                matrix.preRotate(degree);
                Bitmap mRotateBitmap = Bitmap.createBitmap(bitmap, 0, 0,
                        width, height, matrix, true);
                imgView.setImageBitmap(mRotateBitmap);
            } else {
                imgView.setImageBitmap(bitmap);
            }
        } catch (IOException e) {
            // 建議在此補上日誌,避免問題被靜默吞掉
        }
    }
}

幾個實務提醒

  • 縮放與旋轉的順序會影響記憶體峰值,大圖建議先縮放再旋轉,並在替換 Bitmap 後呼叫 recycle()。
  • ExifInterface 讀檔可能丟出 IOException,空 catch 會讓方向問題變成難以追蹤的 bug,務必記錄日誌。
  • Android 7.0 之後若使用 FileProvider 分享 content:// URI,需要改用支援串流建構子的 ExifInterface 版本。
  • 除了旋轉,Exif 還有 ORIENTATION_FLIP_HORIZONTAL 等鏡像情境,若需求涵蓋自拍鏡像,記得一併處理。

掌握這套流程,相機照片的方向就能在各裝置上保持一致,使用者不會再遇到「躺著的照片」。

Copyright © Linux教程網 All Rights Reserved