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.text.TextUtils;

import java.io.IOException;
import java.lang.ref.WeakReference;

public class Main {

    public static WeakReference<Bitmap> autoRotateBitmap(String path, WeakReference<Bitmap> bmp) {
        if (TextUtils.isEmpty(path) || bmp == null || bmp.get() == null)
            return bmp;
        int degree = getDegress(path);
        if (degree != 0) {
            bmp = rotateBitmap(bmp, degree);
        }
        return bmp;
    }

    public static int getDegress(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 WeakReference<Bitmap> rotateBitmap(WeakReference<Bitmap> bitmap, int degress) {
        if (bitmap == null || bitmap.get() == null)
            return null;
        Matrix m = new Matrix();
        m.postRotate(degress);
        return new WeakReference<Bitmap>(Bitmap.createBitmap(bitmap.get(), 0, 0, bitmap.get().getWidth(),
                bitmap.get().getHeight(), m, true));
    }
}