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.Color;
import android.graphics.Paint;

import android.graphics.Rect;

import android.util.Log;

public class Main {
    /** Used to smoothly resize images */
    private static Paint antiAliasPaint = null;
    /** Default canvas used to interact with bitmaps */
    private static Canvas canvas = new Canvas();

    /**
     * Load the image at {@code imagePath} as a {@link Bitmap}, scaling it to
     * the specified size and preserving the aspect ratio.
     * @param imagePath Path of the image to load.
     * @param width Required width of the resulting {@link Bitmap}.
     * @param height Required height of the resulting {@link Bitmap}.
     * @return {@link Bitmap} representing the image at {@code imagePath}.
     */
    public static Bitmap getIconFromImage(String imagePath, int width, int height) {
        return loadResizedBitmap(imagePath, width, height, true, false);
    }

    /**
     * Load the image at {@code imagePath} as a {@link Bitmap}, scaling it to
     * the specified size and preserving the aspect ratio.
     * @param imagePath Path of the image to load.
     * @param width Required width of the resulting {@link Bitmap}.
     * @param height Required height of the resulting {@link Bitmap}.
     * @param fill {@code true} to fill the empty space with transparent color.
     * @param crop {@code true} to crop the image, {@code false} to resize without cutting the image.
     * @return {@link Bitmap} representing the image at {@code imagePath}.
     */
    public static Bitmap loadResizedBitmap(String imagePath, int width, int height, boolean fill, boolean crop) {
        Bitmap retVal;

        BitmapFactory.Options opts = new BitmapFactory.Options();
        opts.inSampleSize = getScale(imagePath, width, height);
        opts.inJustDecodeBounds = false;

        Bitmap image = BitmapFactory.decodeFile(imagePath, opts);

        if (image == null) {
            if (imagePath != null) {
                Log.w("Helper", "Cannot decode " + imagePath);
            } else {
                Log.w("Helper", "Path is null: Cannot decode");
            }
            return null;
        }

        if (image.getWidth() != width || image.getHeight() != height) {
            //Image need to be resized.
            int scaledWidth = (image.getWidth() * height) / image.getHeight();
            int scaledHeight;
            if ((crop && scaledWidth > width) || (!crop && scaledWidth < width)) {
                scaledHeight = height;
            } else {
                scaledWidth = width;
                scaledHeight = (image.getHeight() * width) / image.getWidth();
            }

            Rect src = new Rect(0, 0, image.getWidth(), image.getHeight());
            Rect dst = new Rect(0, 0, scaledWidth, scaledHeight);

            if (fill) {
                retVal = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
                dst.offset((width - scaledWidth) / 2, (height - scaledHeight) / 2);
            } else {
                retVal = Bitmap.createBitmap(scaledWidth, scaledHeight, Bitmap.Config.ARGB_8888);
            }
            retVal.eraseColor(Color.TRANSPARENT);

            synchronized (canvas) {
                if (antiAliasPaint == null) {
                    antiAliasPaint = new Paint();
                    antiAliasPaint.setAntiAlias(true);
                    antiAliasPaint.setFilterBitmap(true);
                    antiAliasPaint.setDither(true);
                }
                canvas.setBitmap(retVal);
                canvas.drawBitmap(image, src, dst, antiAliasPaint);
            }

            image.recycle();
        } else {
            //No need to scale.
            retVal = image;
        }

        return retVal;
    }

    private static int getScale(String imagePath, int desiredWidth, int desiredHeight) {
        //Get image size
        BitmapFactory.Options imageInfo = new BitmapFactory.Options();
        imageInfo.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(imagePath, imageInfo);

        //Compute the scaling factor
        int scale = 1;
        int width = imageInfo.outWidth;
        int height = imageInfo.outHeight;
        //if (size < imageInfo.outHeight) size = imageInfo.outHeight;
        //while (size/(scale*2) >= ICON_SIZE) {
        while (width / (scale * 2) >= desiredWidth && height / (scale * 2) >= desiredHeight) {
            scale = scale * 2;
        }

        return scale;
    }
}