Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.io.IOException;

import android.graphics.Bitmap;

import android.graphics.Matrix;
import android.media.ExifInterface;

public class Main {
    /**
     * Checks if the orientation of the image is correct, if it's not it will rotate the image
     *
     * @param pBitmap
     *        The bitmap to check the orientation for
     * @param pPath
     *        The path to the image, used to load the exif interface
     * @return Bitmap in the correct orientation
     */
    public static Bitmap imageOreintationValidator(Bitmap pBitmap, String pPath) {
        ExifInterface ei;
        try {
            ei = new ExifInterface(pPath);
            int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
            switch (orientation) {
            case ExifInterface.ORIENTATION_ROTATE_90:
                pBitmap = rotateImage(pBitmap, 90);
                break;
            case ExifInterface.ORIENTATION_ROTATE_180:
                pBitmap = rotateImage(pBitmap, 180);
                break;
            case ExifInterface.ORIENTATION_ROTATE_270:
                pBitmap = rotateImage(pBitmap, 270);
                break;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return pBitmap;
    }

    /**
     * Rotates a bitmap by a certain angle
     *
     * @param pSourceBitmap
     *        The bitmap that needs to be rotated
     * @param pAngle
     *        The angle the bitmap should be rotated to
     * @return A rotated bitmap
     */
    private static Bitmap rotateImage(Bitmap pSourceBitmap, float pAngle) {
        Bitmap bitmap = null;
        Matrix matrix = new Matrix();
        matrix.postRotate(pAngle);
        try {
            bitmap = Bitmap.createBitmap(pSourceBitmap, 0, 0, pSourceBitmap.getWidth(), pSourceBitmap.getHeight(),
                    matrix, true);
        } catch (OutOfMemoryError err) {
            err.printStackTrace();
        }
        return bitmap;
    }
}