Android Bitmap Scale generateScaledBitmap(byte[] bitMapData, int width, int height)

Here you can find the source of generateScaledBitmap(byte[] bitMapData, int width, int height)

Description

Generates a bitmap scaled to provided width and height

License

Apache License

Parameter

Parameter Description
bitMapData a byte array representation of an image 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(byte[] bitMapData, 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 {
    /**/* w ww. ja  va  2  s  .  c  o m*/
     * 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. getScaleBitmap(Bitmap origialBitmap, int dstWidth, int dstHeight)
  2. getScaleBitmap(Context context, int resId, int dstWidth, int dstHeight)
  3. getScaleImage(final Bitmap bitmap, final float boundBoxInDp)
  4. generateScaledBitmap(Bitmap bitmap, int width, int height)
  5. generateScaledBitmap(Drawable drawable, int width, int height)
  6. crossStretchImageX(Bitmap image, int xsize)
  7. scaleBitmap(Bitmap bitmap, int width, int height)
  8. scaleBitmap(String path, int newWidth, int newHeight)
  9. scaleClipBitmapByCircle(Bitmap src, int nRadius, float fStrokeWidth)