Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.media.ExifInterface;
import android.util.Log;
import java.io.File;

import java.io.IOException;

public class Main {
    private static final String TAG = "BitmapUtils";

    public static Bitmap decodeSampledBitmap(String fileName, int reqWidth, int reqHeight) {

        final int rotation = getRotation(fileName);

        // First decode with inJustDecodeBounds=true to check dimensions
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(fileName, options);

        // Calculate inSampleSize
        options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
        Bitmap thumb = null;
        try {
            thumb = BitmapFactory.decodeFile(fileName, options);
            return rotate(thumb, rotation);
        } finally {
            if (rotation != 0 && thumb != null) {
                thumb.recycle();
            }
        }
    }

    private static int getRotation(String fileName) {
        try {
            File imageCaptureFile = new File(fileName);
            ExifInterface exif = new ExifInterface(imageCaptureFile.getAbsolutePath());
            int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
            if (orientation == ExifInterface.ORIENTATION_ROTATE_90)
                return 90;
            if (orientation == ExifInterface.ORIENTATION_ROTATE_180)
                return 180;
            if (orientation == ExifInterface.ORIENTATION_ROTATE_270)
                return 270;
        } catch (IOException e) {
            Log.e(TAG, "Failed to read rotation from file: " + fileName, e);
        }
        return 0;
    }

    public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
        // Raw height and width of image
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;

        if (height > reqHeight || width > reqWidth) {

            final int halfHeight = height / 2;
            final int halfWidth = width / 2;

            // Calculate the largest inSampleSize value that is a power of 2 and keeps both
            // height and width larger than the requested height and width.
            while ((halfHeight / inSampleSize) > reqHeight && (halfWidth / inSampleSize) > reqWidth) {
                inSampleSize *= 2;
            }
        }

        return inSampleSize;
    }

    private static Bitmap rotate(Bitmap bmp, int rotation) {
        if (rotation == 0)
            return bmp;

        try {
            Matrix matrix = new Matrix();
            matrix.preRotate(rotation);
            return Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true);
        } finally {
            bmp.recycle();
        }
    }
}