Chap 16 Image orientation

When I capture an image, the preview always shows in landscape even if the picture was taken in portrait. Landscape pix show okay (but sometimes upside down :wink: Is there some setting that I can use to fix this? Or a method to rotate images?

1 Like

Hey, did you ever work out a fix for this? I’m having the same issue

Sorry, nope. Just moved on for now. Good luck!

I changed my API version to the newer one: from 19 to 21. Now the orientation is okay.

这似乎是某些设备的原因,我获取了照片的角度信息,并将它纠正回来。

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.Point;
import android.support.media.ExifInterface;

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

public class PictureUtils {
    public static Bitmap getScaledBitmap(String path, int destWidth, int destHeight) {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(path, options);


        float srcWidth = options.outWidth;
        float srcHeight = options.outHeight;

        int inSampleSize = 1;
        if (srcHeight > destHeight || srcWidth > destWidth) {
            float heightScale = srcHeight / destHeight;
            float widthScale = srcWidth / destWidth;

            inSampleSize = Math.round(heightScale > widthScale ? heightScale : widthScale);
        }

        options = new BitmapFactory.Options();
        options.inSampleSize = inSampleSize;
        
        int angle = readPictureDegree(path);
        Bitmap bitmap = BitmapFactory.decodeFile(path, options);
        bitmap = rotatingImageView(angle, bitmap);
        return bitmap;
    }

    public static Bitmap getScaledBitmap(String path, Activity activity) {
        Point size = new Point();
        activity.getWindowManager().getDefaultDisplay().getSize(size);
        return getScaledBitmap(path, size.x, size.y);
    }

    public static boolean removePhoto(String path) {
        File file = new File(path);
        return file.delete();
    }

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

    public static Bitmap rotatingImageView(int angle, Bitmap bitmap) {
        Bitmap newBitmap = null;

        Matrix matrix = new Matrix();
        matrix.postRotate(angle);
        try {
            newBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
        } catch (OutOfMemoryError e) {
        }
        if (newBitmap == null) {
            newBitmap = bitmap;
        }
        if (bitmap != newBitmap) {
            bitmap.recycle();
        }
        return newBitmap;
    }
}