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.BitmapFactory;

import android.graphics.Matrix;

import android.media.ExifInterface;

public class Main {
    /****************************************************************************************************************
     * Get the image bitmap that resizing to maximum size of limit.
     * Parameter :
     *  - context : Context 
     *  - uri : Image URI
     *  - bContentStreamImage : Gallery contents stream file(true)/file path(false)
     *  - nMaxResizedWidth : The maximum allowable width of resizing image.
     *  - nMaxResizedHeight : The maximum allowable height of resizing image.
     * Return :
     *  - Resizing bitmap
     */
    public static Bitmap getSafeResizingBitmap(String strImagePath, int nMaxResizedWidth, int nMaxResizedHeight,
            boolean checkOrientation) {
        //==========================================
        // Bitmap Option
        //==========================================
        BitmapFactory.Options options = getBitmapSize(strImagePath);
        if (options == null)
            return null;

        //==========================================
        // Bitmap Scaling
        //==========================================
        int nSampleSize;
        int degree = 0;
        if (checkOrientation) {
            degree = getExifDegree(strImagePath);
        }

        if (degree == 90 || degree == 270) {
            nSampleSize = getSafeResizingSampleSize(options.outHeight, options.outWidth, nMaxResizedWidth,
                    nMaxResizedHeight);
        } else {
            nSampleSize = getSafeResizingSampleSize(options.outWidth, options.outHeight, nMaxResizedWidth,
                    nMaxResizedHeight);
        }

        //==========================================
        // Load the bitmap including actually data.
        //==========================================
        options.inJustDecodeBounds = false;
        options.inSampleSize = nSampleSize;
        options.inDither = false;
        options.inPreferredConfig = Bitmap.Config.ARGB_8888;
        options.inPurgeable = true;

        Bitmap photo = BitmapFactory.decodeFile(strImagePath, options);
        if (checkOrientation && (degree == 90 || degree == 270)) {
            return getRotatedBitmap(photo, degree);
        }
        return photo;
    }

    public static BitmapFactory.Options getBitmapSize(String strImagePath) {
        //==========================================
        // Loaded the temporary bitmap for getting size.
        //==========================================
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        //Bitmap photo = BitmapFactory.decodeFile(strPath, options);
        BitmapFactory.decodeFile(strImagePath, options);

        return options;
    }

    public static BitmapFactory.Options getBitmapSize(String strImagePath, boolean checkOrientation) {
        //==========================================
        // Loaded the temporary bitmap for getting size.
        //==========================================
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        //Bitmap photo = BitmapFactory.decodeFile(strPath, options);
        BitmapFactory.decodeFile(strImagePath, options);

        if (checkOrientation) {
            int degree = getExifDegree(strImagePath);
            if (degree == 90 || degree == 270) {
                int temp = options.outWidth;
                options.outWidth = options.outHeight;
                options.outHeight = temp;
            }
        }

        return options;
    }

    public static int getExifDegree(String filepath) {
        int degree = 0;
        ExifInterface exif;
        try {
            exif = new ExifInterface(filepath);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return 0;
        }

        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 = 270;
                    break;
                }
            }
        }
        return degree;
    }

    /****************************************************************************************************************
     * Get the sampling size for load the bitmap. (If you load the bitmap file of big size, This application is terminated.)
     * Parameter :
     *  - nOrgWidth   : The width of the original image (Value of outWidth of BitmapFactory.Options)
     *  - nOrgHeight : The height of the original image  (Value of outHeight of BitmapFactory.Options)
     *  - nMaxWidth : The width of the image of maximum size.  (width under 3M. ex.3000)
     *  - nMaxHeight : The height of the image of maximum size.   (height under 3M. ex.1000)    *  
     * Return :
     *  - Sampling size (If no need to resize, return 1). Throttled much larger.
     *  - If more than x.5 times , divide x+1 times.
     */
    public static int getSafeResizingSampleSize(int nOrgWidth, int nOrgHeight, int nMaxWidth, int nMaxHeight) {
        int size = 1;
        float fsize;
        float fWidthScale = 0;
        float fHeightScale = 0;

        if (nOrgWidth > nMaxWidth || nOrgHeight > nMaxHeight) {
            if (nOrgWidth > nMaxWidth)
                fWidthScale = (float) nOrgWidth / (float) nMaxWidth;
            if (nOrgHeight > nMaxHeight)
                fHeightScale = (float) nOrgHeight / (float) nMaxHeight;

            if (fWidthScale >= fHeightScale)
                fsize = fWidthScale;
            else
                fsize = fHeightScale;

            size = (int) fsize;
        }

        return size;
    }

    public static Bitmap getRotatedBitmap(Bitmap bitmap, int degrees) {
        if (degrees != 0 && bitmap != null) {
            Matrix m = new Matrix();
            m.setRotate(degrees, (float) bitmap.getWidth() / 2, (float) bitmap.getHeight() / 2);
            try {
                Bitmap b2 = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), m, true);
                if (bitmap.equals(b2)) {
                    bitmap.recycle();
                    bitmap = b2;
                }
            } catch (OutOfMemoryError ex) {
                // TODO Auto-generated catch block
                ex.printStackTrace();
            }
        }
        return bitmap;
    }
}