Example usage for android.graphics Bitmap createScaledBitmap

List of usage examples for android.graphics Bitmap createScaledBitmap

Introduction

In this page you can find the example usage for android.graphics Bitmap createScaledBitmap.

Prototype

public static Bitmap createScaledBitmap(@NonNull Bitmap src, int dstWidth, int dstHeight, boolean filter) 

Source Link

Document

Creates a new bitmap, scaled from an existing bitmap, when possible.

Usage

From source file:Main.java

public static Drawable resizeDrawable(Context context, Drawable drawable, int width, int height) {
    Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
    Drawable newDrawable = new BitmapDrawable(context.getResources(),
            Bitmap.createScaledBitmap(bitmap, width, height, true));

    return newDrawable;
}

From source file:Main.java

public static Bitmap createScaledToCenterInsideBitmap(Bitmap bitmap, int reqWidth, int reqHeight) {
    final int height = bitmap.getHeight();
    final int width = bitmap.getWidth();

    float widthScaleF = (float) width / (float) reqWidth;
    float heightScaleF = (float) height / (float) reqHeight;
    if (widthScaleF > heightScaleF) {
        return Bitmap.createScaledBitmap(bitmap, reqWidth, (int) (height / widthScaleF), true);
    } else {/*from  w w  w . j a  v a  2 s  .  c o m*/
        return Bitmap.createScaledBitmap(bitmap, (int) (width / heightScaleF), reqHeight, true);
    }
}

From source file:Main.java

public static Bitmap getHistogram(Bitmap bmpOriginal) {

    //Scale bmpOriginal to improve performance 
    final int dstWidth = 200;
    int dstHeight = dstWidth * bmpOriginal.getHeight() / bmpOriginal.getWidth();
    Bitmap bmpScaled = Bitmap.createScaledBitmap(bmpOriginal, dstWidth, dstHeight, false);
    int[] histogramValues = new int[256];

    int[] pixels = new int[dstWidth];
    int pxBrightness;
    for (int row = 0; row < dstHeight; row++) {
        bmpScaled.getPixels(pixels, 0, dstWidth, 0, row, dstWidth, 1);
        for (int col = 0; col < dstWidth; col++) {
            pxBrightness = rgbToGray(pixels[col]);
            histogramValues[pxBrightness]++;
        }// w w w . j  av  a 2s.c  o m
    }
    bmpScaled.recycle();

    int histogramMax = max(histogramValues);
    Bitmap bmpHistogram = Bitmap.createBitmap(256, histogramMax, Config.ARGB_8888);
    Canvas canvas = new Canvas(bmpHistogram);
    Paint paint = new Paint();
    paint.setColor(Color.CYAN);

    for (int i = 0; i < 256; i++)
        canvas.drawLine(i, histogramMax - histogramValues[i], i, histogramMax, paint);

    return bmpHistogram;
}

From source file:Main.java

public static Bitmap getCroppedBitmap(Bitmap bmp, int radius) {

    Bitmap sbmp;/*from   ww w  . j a  va  2 s. c  o  m*/
    if (bmp.getWidth() != radius || bmp.getHeight() != radius)
        sbmp = Bitmap.createScaledBitmap(bmp, radius, radius, false);
    else
        sbmp = bmp;
    Bitmap output = Bitmap.createBitmap(sbmp.getWidth(), sbmp.getHeight(), Config.ARGB_8888);
    Canvas canvas = new Canvas(output);
    //final int color = 0xffa19774;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, sbmp.getWidth(), sbmp.getHeight());
    paint.setAntiAlias(true);
    paint.setFilterBitmap(true);
    paint.setDither(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(Color.parseColor("#BAB399"));
    canvas.drawCircle(sbmp.getWidth() / 2 + 0.7f, sbmp.getHeight() / 2 + 0.7f, sbmp.getWidth() / 2 + 0.1f,
            paint);
    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    canvas.drawBitmap(sbmp, rect, rect, paint);
    return output;
}

From source file:Main.java

public static Bitmap extractBitmap(Bitmap source, float scale) {
    if (null == source) {
        return null;
    }//from  ww w.j a v  a2 s. c o m
    Bitmap dest = Bitmap.createScaledBitmap(source, (int) (source.getWidth() * scale),
            (int) (source.getHeight() * scale), false);
    recycle(source);
    return dest;
}

From source file:Main.java

/**
 * Converts the supplied byte[] to a Bitmap at 75% compression
 *
 * @param bytes the bitmap's bytes/*ww w .  j a va  2s  .co  m*/
 * @return the Bitmap
 */
public static synchronized Bitmap asBitmap(byte[] bytes) {
    Bitmap bmp = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
    return Bitmap.createScaledBitmap(bmp, bmp.getWidth() / 4, bmp.getHeight() / 4, true);
}

From source file:Main.java

public static Bitmap scaleToStretchBitmap(Bitmap dst, byte[] byImage) {
    Bitmap src = BitmapFactory.decodeByteArray(byImage, 0, byImage.length);

    return Bitmap.createScaledBitmap(src, dst.getWidth(), dst.getHeight(), true);
}

From source file:Main.java

public static Bitmap resizeBitmap(Bitmap image, int maxSize) {
    int width = image.getWidth();
    int height = image.getHeight();
    float aspectRatio = width / (float) height;

    if (aspectRatio > 1) {
        width = maxSize;//  ww  w.  ja  v a2  s  . c o m
        height = Math.round(width / aspectRatio);
    } else {
        height = maxSize;
        width = Math.round(height * aspectRatio);
    }

    return Bitmap.createScaledBitmap(image, width, height, true);
}

From source file:Main.java

/**
 * Create rounded corner bitmap.//ww  w.  ja  va2  s  . co m
 *
 * @param bitmap bitmap.
 * @return rounded bitmap.
 */
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap) {
    Bitmap source = Bitmap.createScaledBitmap(bitmap, 480, 480, false);
    Bitmap output = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    final int color = 0xff424242;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, source.getWidth(), source.getHeight());
    final RectF rectF = new RectF(rect);
    final float roundPx = source.getWidth();

    paint.setAntiAlias(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));

    canvas.drawBitmap(source, rect, rect, paint);

    return output;
}

From source file:Main.java

public static void changeImageButton(Context context, ImageButton button, int resId, int width, int height) {
    Drawable icon;//from   w  w  w .  j av  a2  s  .  c om
    icon = context.getResources().getDrawable(resId);
    icon.setBounds(0, 0, width, height);
    Bitmap iconBitmap = ((BitmapDrawable) icon).getBitmap();
    Bitmap bitmapResized = Bitmap.createScaledBitmap(iconBitmap, width, height, false);
    button.setImageBitmap(bitmapResized);

}