Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.io.File;
import java.io.FileInputStream;

import java.io.IOException;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

public class Main {
    /**
     * default size bitmap decoding
     * 
     * @param f
     * @return
     */
    public static Bitmap decodeFile(File f) {
        return decodeFile(f, 500);
    }

    /**
     * decode the image bitmap according to specified size
     * 
     * @param f
     * @param maxSize
     * @return
     */
    public static Bitmap decodeFile(File f, int maxSize) {
        Bitmap b = null;
        try {
            // Decode image size
            BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;

            FileInputStream fis = new FileInputStream(f);
            BitmapFactory.decodeStream(fis, null, o);
            fis.close();

            int scale = 1;
            if (o.outHeight > maxSize || o.outWidth > maxSize) {
                scale = (int) Math.pow(2, (int) Math
                        .round(Math.log(maxSize / (double) Math.max(o.outHeight, o.outWidth)) / Math.log(0.5)));
            }

            // Decode with inSampleSize
            BitmapFactory.Options o2 = new BitmapFactory.Options();
            o2.inSampleSize = scale;
            fis = new FileInputStream(f);
            b = BitmapFactory.decodeStream(fis, null, o2);
            fis.close();
        } catch (IOException e) {
        }
        return b;
    }
}