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.Matrix;

public class Main {
    /***
     * Downsize the bitmap to at most the indicated ratio of the max specified size
     * @param bm Bitmap to resize
     * @param maxX pixels of max width
     * @param maxY pixels of max height
     * @param maxRatio The max ratio wrt the display size
     * @return the new bitmap, OR input bitmap if no change needed
     */
    public static Bitmap getResizedBitmap(Bitmap bm, int maxX, int maxY, double maxRatio) {

        // we have an starting bitmap object to work from
        if (null == bm) {
            return bm;
        }

        // Get current size and h:w ratio
        int height = bm.getHeight();
        int width = bm.getWidth();

        // ensure bitmap size is valid
        if (0 == height || 0 == width) {
            return bm;
        }

        // What is the height to width ratio - will always be > 0 at this point
        double ratio = height / width;

        // Figure out new max size
        int newHeight = (int) (Math.min(maxX, maxY) * maxRatio);
        int newWidth = (int) (newHeight / ratio);

        // If we don't need to downsize, then return with the original
        if (newHeight >= height && newWidth >= width) {
            return bm;
        }

        // Calculate the scaling factors in both the x and y direction
        float scaleWidth = ((float) newWidth) / width;
        float scaleHeight = ((float) newHeight) / height;

        // create a matrix for the manipulation
        Matrix matrix = new Matrix();

        // resize the bit map
        matrix.postScale(scaleWidth, scaleHeight);

        // recreate the new Bitmap, allowing for failure
        try {
            Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);
            return resizedBitmap;
        } catch (Exception e) {
            return bm;
        }
    }
}