Android - 拍照結果、相簿取得依正常方向取得Bitmap

摘要:Android - 拍照結果、相簿取得依正常方向取得Bitmap

因為有些手機,拍完照之後,是以橫式存放,導致設定到ImageView時,方向錯亂,直式拍的,設定後變成了橫式。

圖片會顯示資訊他是以什麼方向存放,因此根據預設資訊,替他做轉向另存新檔。

 

分兩個東西改,

一是拍照

解法:

http://stackoverflow.com/questions/10530165/android-camera-orientation-issue

我包了一個Helper如下:

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.media.ExifInterface;
import android.util.Log;

import java.io.File;
import java.io.IOException;

public class CameraOrientationHelper {
    public static int resolveBitmapOrientation(File bitmapFile) throws IOException {
        ExifInterface exif = null;
        exif = new ExifInterface(bitmapFile.getAbsolutePath());

        return exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
    }
    
    
    
    public static Bitmap applyOrientation(Bitmap bitmap, int orientation) {
        int rotate = 0;
        switch (orientation) {
            case ExifInterface.ORIENTATION_ROTATE_270:
                rotate = 270;
                break;
            case ExifInterface.ORIENTATION_ROTATE_180:
                rotate = 180;
                break;
            case ExifInterface.ORIENTATION_ROTATE_90:
                rotate = 90;
                break;
            default:
                return bitmap;
        }

        int w = bitmap.getWidth();
        int h = bitmap.getHeight();
        Matrix mtx = new Matrix();
        mtx.postRotate(rotate);
        return Bitmap.createBitmap(bitmap, 0, 0, w, h, mtx, true);
    }
    
    public static Bitmap getOrientationBitmap(String filePath,BitmapFactory.Options options) {
        Bitmap srcBmp01=BitmapFactory.decodeFile(filePath,options);
        try {
            int orientation = CameraOrientationHelper.resolveBitmapOrientation(new File(filePath));
            srcBmp01 = CameraOrientationHelper.applyOrientation(srcBmp01, orientation);
        } catch (Exception ex) {
            ;
        }       
        
        return srcBmp01;
    }
    
    public static Bitmap getOrientationBitmap(String filePath) {
        Bitmap srcBmp01=BitmapFactory.decodeFile(filePath);
        
        if(srcBmp01!=null) {            
            try {
                int orientation = CameraOrientationHelper.resolveBitmapOrientation(new File(filePath));
                srcBmp01 = CameraOrientationHelper.applyOrientation(srcBmp01, orientation);
            } catch (Exception ex) {
                ;
            }   
        }
        
        return srcBmp01;
    }
}
    

 

 

二是相簿

相簿的解法。

http://stackoverflow.com/questions/13511356/android-image-selected-from-gallery-orientation-is-always-0-exif-tag

import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.media.ExifInterface;
import android.net.Uri;
import android.provider.MediaStore;
import android.util.Log;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

public class GalleryOrientationHelper {
    public static Bitmap getOrientationBitmap(Context context, Uri photoUri,BitmapFactory.Options options) throws IOException {
        InputStream is = context.getContentResolver().openInputStream(photoUri);
        BitmapFactory.Options dbo = new BitmapFactory.Options();
        dbo.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(is, null, dbo);
        is.close();

        int rotatedWidth, rotatedHeight;
        int orientation = getOrientation(context, photoUri);

        if (orientation == 90 || orientation == 270) {
            rotatedWidth = dbo.outHeight;
            rotatedHeight = dbo.outWidth;
        } else {
            rotatedWidth = dbo.outWidth;
            rotatedHeight = dbo.outHeight;
        }

        Bitmap srcBitmap;
        is = context.getContentResolver().openInputStream(photoUri);
        srcBitmap = BitmapFactory.decodeStream(is, null, options);
        is.close();

        /*
         * if the orientation is not 0 (or -1, which means we don't know), we
         * have to do a rotation.
         */
        if (orientation > 0) {
            Matrix matrix = new Matrix();
            matrix.postRotate(orientation);

            srcBitmap = Bitmap.createBitmap(srcBitmap, 0, 0, srcBitmap.getWidth(),
                    srcBitmap.getHeight(), matrix, true);
        }

        return srcBitmap;
    }

    public static int getOrientation(Context context, Uri photoUri) {
        /* it's on the external media. */
        Cursor cursor = context.getContentResolver().query(photoUri,
                new String[] { MediaStore.Images.ImageColumns.ORIENTATION }, null, null, null);

        if (cursor.getCount() != 1) {
            return -1;
        }

        cursor.moveToFirst();
        return cursor.getInt(0);
    }
}
    

當相簿,在Android 4.4.4 KitKat 時, 將會出現問題。以下面這個連結可看出例外

http://stackoverflow.com/questions/13511356/android-image-selected-from-gallery-orientation-is-always-0-exif-tag

 

在Android 4.4.4 KitKat 的時候,

可能需要處理的方式如下:

http://stackoverflow.com/questions/20067508/get-real-path-from-uri-android-kitkat-new-storage-access-framework/20559175#20559175