Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;

import android.media.ExifInterface;

import java.io.IOException;

public class Main {
    public static Bitmap getRotatedImg(String path) {
        int angle = getBitmapRotation(path);
        Matrix matrix = new Matrix();
        matrix.postRotate(angle);
        Bitmap bitmap = BitmapFactory.decodeFile(path);
        try {
            bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return bitmap;
    }

    private static int getBitmapRotation(String filePath) {
        int rotation = 0;
        switch (getExifOrientation(filePath)) {
        case ExifInterface.ORIENTATION_ROTATE_180:
            rotation = 180;
            break;
        case ExifInterface.ORIENTATION_ROTATE_90:
            rotation = 90;
            break;
        case ExifInterface.ORIENTATION_ROTATE_270:
            rotation = 270;
            break;
        }
        return rotation;
    }

    private static int getExifOrientation(String filePath) {
        ExifInterface exif;
        int orientation = 0;
        try {
            exif = new ExifInterface(filePath);
            orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return orientation;
    }
}