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.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;

import android.util.Log;

public class Main {
    final static String TAG = "BitmapUtil";

    public static Bitmap makeBitmap(String filePath, int maxNumOfPixels) {
        try {
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            BitmapFactory.decodeFile(filePath, options);
            if (options.mCancel || options.outWidth == -1 || options.outHeight == -1) {
                return null;
            }
            options.inSampleSize = computeSampleSize(options, -1, maxNumOfPixels);
            options.inJustDecodeBounds = false;
            options.inDither = false;
            options.inPreferredConfig = Bitmap.Config.ARGB_8888;
            Bitmap b = BitmapFactory.decodeFile(filePath, options);
            b = formatBitmap(b, Math.min(options.outHeight, options.outWidth));
            return b;
        } catch (OutOfMemoryError ex) {
            Log.e(TAG, "Got oom exception ", ex);
            return null;
        }
    }

    public static int computeSampleSize(BitmapFactory.Options options, int minSideLength, int maxNumOfPixels) {
        int initialSize = computeInitialSampleSize(options, minSideLength, maxNumOfPixels);

        int roundedSize;
        if (initialSize <= 8) {
            roundedSize = 1;
            while (roundedSize < initialSize) {
                roundedSize <<= 1;
            }
        } else {
            roundedSize = (initialSize + 7) / 8 * 8;
        }

        return roundedSize;
    }

    public static Bitmap formatBitmap(Bitmap bitmap, int width, int height) {

        int wh = Math.max(bitmap.getWidth(), bitmap.getHeight());
        Bitmap mBitmap = Bitmap.createBitmap(wh, wh, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(mBitmap);
        canvas.drawBitmap(bitmap, -(bitmap.getWidth() - wh) / 2, -(bitmap.getHeight() - wh) / 2, null);
        bitmap = Bitmap.createScaledBitmap(mBitmap, width, height, true);
        return bitmap;
    }

    public static Bitmap formatBitmap(Bitmap bitmap, int size) {

        float width = bitmap.getWidth();
        float height = bitmap.getHeight();
        float rota = size / height;

        int widthnew = (int) ((size * width) / height);
        // int wh = Math.max(bitmap.getWidth(), bitmap.getHeight());
        Bitmap mBitmap = Bitmap.createBitmap(widthnew, size, Bitmap.Config.ARGB_8888);
        Matrix matrix = new Matrix();
        matrix.postScale(rota, rota);
        Canvas canvas = new Canvas(mBitmap);
        canvas.drawBitmap(bitmap, matrix, null);

        // bitmap = Bitmap.createScaledBitmap(mBitmap, width, height, true);
        return mBitmap;
    }

    public static int computeInitialSampleSize(BitmapFactory.Options options, int minSideLength,
            int maxNumOfPixels) {
        double w = options.outWidth;
        double h = options.outHeight;

        int lowerBound = (maxNumOfPixels == -1) ? 1 : (int) Math.ceil(Math.sqrt(w * h / maxNumOfPixels));
        int upperBound = (minSideLength == -1) ? 128
                : (int) Math.min(Math.floor(w / minSideLength), Math.floor(h / minSideLength));

        if (upperBound < lowerBound) {
            // return the larger one when there is no overlapping zone.
            return lowerBound;
        }

        if ((maxNumOfPixels == -1) && (minSideLength == -1)) {
            return 1;
        } else if (minSideLength == -1) {
            return lowerBound;
        } else {
            return upperBound;
        }
    }
}