Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Open Source License 

import java.io.IOException;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.media.ExifInterface;

public class Main {
    public static Bitmap rotateBitmapFromExif(String filePath, Bitmap bitmap) {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(filePath, options);

        String orientString;
        try {
            ExifInterface exif = new ExifInterface(filePath);
            orientString = exif.getAttribute(ExifInterface.TAG_ORIENTATION);
        } catch (IOException e) {
            e.printStackTrace();
            return bitmap;
        }

        int orientation = (orientString != null) ? Integer.parseInt(orientString)
                : ExifInterface.ORIENTATION_NORMAL;
        int rotationAngle = 0, outHeight = 0, outWidth = 0;
        switch (orientation) {
        case ExifInterface.ORIENTATION_ROTATE_90:
            rotationAngle = 90;
            outHeight = bitmap.getWidth();
            outWidth = bitmap.getHeight();
            break;
        case ExifInterface.ORIENTATION_ROTATE_180:
            rotationAngle = 180;
            outHeight = bitmap.getHeight();
            outWidth = bitmap.getWidth();
            break;
        case ExifInterface.ORIENTATION_ROTATE_270:
            rotationAngle = 270;
            outHeight = bitmap.getWidth();
            outWidth = bitmap.getHeight();
            break;
        }

        if (rotationAngle == 0) {
            return bitmap;
        }

        Matrix matrix = new Matrix();
        matrix.setRotate(rotationAngle, (float) bitmap.getWidth() / 2, (float) bitmap.getHeight() / 2);
        Bitmap rotateBitmap = Bitmap.createBitmap(bitmap, 0, 0, outWidth, outHeight, matrix, true);
        if (bitmap != rotateBitmap) {
            bitmap.recycle();
        }

        return rotateBitmap;
    }
}