Android Bitmap Scale generateScaledBitmap(Bitmap bitmap, int width, int height)

Here you can find the source of generateScaledBitmap(Bitmap bitmap, int width, int height)

Description

Generates a bitmap scaled to provided width and height

License

Apache License

Parameter

Parameter Description
bitmap a Bitmap to be scaled
width the new Bitmap's desired width
height the new Bitmap's desired height

Return

A Bitmap scaled to the provided height and width

Declaration

public static Bitmap generateScaledBitmap(Bitmap bitmap, int width,
        int height) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;

public class Main {
    /**/*from  w  ww.java  2 s. com*/
     * Generates a bitmap scaled to provided width and height
     *
     * @param drawable a valid BitmapDrawable to be scaled
     * @param width the new Bitmap's desired width
     * @param height the new Bitmap's desired height
     * @return A Bitmap scaled to the provided height and width
     */
    public static Bitmap generateScaledBitmap(Drawable drawable, int width,
            int height) {
        if (drawable != null && drawable instanceof BitmapDrawable) {
            Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
            return generateScaledBitmap(bitmap, width, height);
        } else {
            return null;
        }
    }

    /**
     * Generates a bitmap scaled to provided width and height
     *
     * @param bitmap a Bitmap to be scaled
     * @param width the new Bitmap's desired width
     * @param height the new Bitmap's desired height
     * @return A Bitmap scaled to the provided height and width
     */
    public static Bitmap generateScaledBitmap(Bitmap bitmap, int width,
            int height) {
        if (bitmap != null) {
            try {
                return Bitmap.createScaledBitmap(bitmap, width, height,
                        true);
            } catch (IllegalArgumentException illegalArgs) {
                return null;
            }
        } else {
            return null;
        }
    }

    /**
     * Generates a bitmap scaled to provided width and height
     *
     * @param bitMapData a byte array representation of an image to be scaled
     * @param width the new Bitmap's desired width
     * @param height the new Bitmap's desired height
     * @return A Bitmap scaled to the provided height and width
     */
    public static Bitmap generateScaledBitmap(byte[] bitMapData, int width,
            int height) {
        if (bitMapData != null) {
            try {
                Bitmap bitmap = BitmapFactory.decodeByteArray(bitMapData,
                        0, bitMapData.length);
                return generateScaledBitmap(bitmap, width, height);
            } catch (ArrayIndexOutOfBoundsException indexOutOfBounds) {
                return null;
            } catch (IllegalArgumentException illegalArgs) {
                return null;
            }
        } else {
            return null;
        }
    }
}

Related

  1. scaleImg(Bitmap bitmap, int newWidth, int newHeight)
  2. scaleImg(File file, int newWidth, int newHeight)
  3. getScaleBitmap(Bitmap origialBitmap, int dstWidth, int dstHeight)
  4. getScaleBitmap(Context context, int resId, int dstWidth, int dstHeight)
  5. getScaleImage(final Bitmap bitmap, final float boundBoxInDp)
  6. generateScaledBitmap(Drawable drawable, int width, int height)
  7. generateScaledBitmap(byte[] bitMapData, int width, int height)
  8. crossStretchImageX(Bitmap image, int xsize)
  9. scaleBitmap(Bitmap bitmap, int width, int height)