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.Bitmap.Config;
import android.graphics.BitmapFactory;

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

public class Main {
    @SuppressWarnings("unused")
    private static Bitmap readBitMap(String filePath) throws FileNotFoundException {
        BitmapFactory.Options opt = new BitmapFactory.Options();
        opt.inPreferredConfig = Config.RGB_565;
        opt.inPurgeable = true;
        opt.inInputShareable = true;
        //      return BitmapFactory.decodeStream(new FileInputStream(file), null, opt);
        return BitmapFactory.decodeFile(filePath, opt);
    }

    /**
     * @param file
     * @param image_width
     * @param image_height
     * @return
     * @hide
     */
    @SuppressWarnings("unused")
    private static Bitmap decodeFile(File file, int image_width, int image_height) {
        try {
            BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;
            BitmapFactory.decodeStream(new FileInputStream(file), null, o);

            int REQUIRED_HEIGHT = image_height;
            int REQUIRED_WIDTH = image_width;

            int width_tmp = o.outWidth, height_tmp = o.outHeight;

            //         Log.w("===", (width_tmp + "  " + height_tmp));

            int scale = 1;
            while (true) {
                if (width_tmp / 2 < REQUIRED_WIDTH && height_tmp / 2 < REQUIRED_HEIGHT)
                    break;
                width_tmp /= 2;
                height_tmp /= 2;
                scale++;

                //            Log.w("===", scale + "''" + width_tmp + "  " + height_tmp);
            }

            BitmapFactory.Options o2 = new BitmapFactory.Options();
            o2.inSampleSize = scale;
            return BitmapFactory.decodeStream(new FileInputStream(file), null, o2);
        } catch (FileNotFoundException e) {
        }
        return null;
    }
}