Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import android.graphics.Bitmap;

import android.graphics.Matrix;

import android.media.ExifInterface;

import android.util.Log;

import java.io.IOException;

public class Main {
    public static Bitmap orientationBitMap(String filepath, Bitmap bit) {
        int orientation = getExifOrientation(filepath);
        if (orientation != 0) {
            Matrix matrix = new Matrix();
            matrix.setRotate(orientation);
            return Bitmap.createBitmap(bit, 0, 0, bit.getWidth(), bit.getHeight(), matrix, true);
        }
        return bit;
    }

    public static int getExifOrientation(String filepath) {
        int degree = 0;
        try {
            ExifInterface exif = new ExifInterface(filepath);
            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 = 90;
                        break;
                    }
                }
            }
            return degree;
        } catch (IOException ex) {
            Log.e("ExifOrientation", "cannot read exif", ex);
        }
        return degree;
    }
}