Android Bitmap Resize getResizedBitmap(Bitmap bm, int maxX, int maxY, double maxRatio)

Here you can find the source of getResizedBitmap(Bitmap bm, int maxX, int maxY, double maxRatio)

Description

Downsize the bitmap to at most the indicated ratio of the max specified size

License

Open Source License

Parameter

Parameter Description
bm Bitmap to resize
maxX pixels of max width
maxY pixels of max height
maxRatio The max ratio wrt the display size

Return

the new bitmap, OR input bitmap if no change needed

Declaration

public static Bitmap getResizedBitmap(Bitmap bm, int maxX, int maxY,
        double maxRatio) 

Method Source Code

//package com.java2s;

import android.graphics.Bitmap;

import android.graphics.Matrix;

public class Main {
    /***/* www.  jav a  2  s .  c o m*/
     * 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;
        }
    }
}

Related

  1. zoomBitmap(Bitmap bgimage, int newWidth, int newHeight)
  2. zoomBitmap(Bitmap bitmap, int width, int height)
  3. zoomBitmapAdjustScreen(Activity activity, String imagePath)
  4. zoomImage(Bitmap bgimage, int newWidth, int newHeight)
  5. getResizedBitmap(Bitmap bm, int newHeight, int newWidth)
  6. getSafeResizingBitmap(String strImagePath, int nMaxResizedWidth, int nMaxResizedHeight, boolean checkOrientation)