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 int getAverageColor(@NonNull Bitmap bitmap) {
    Bitmap onePixelBitmap = Bitmap.createScaledBitmap(bitmap, 1, 1, true);
    int color = onePixelBitmap.getPixel(0, 0);
    onePixelBitmap.recycle();/* w  w  w . j av  a 2s. c  o m*/
    return color;
}

From source file:Main.java

private static byte[] resizeImage(byte[] input, int length) {
    Bitmap original = BitmapFactory.decodeByteArray(input, 0, input.length);
    Bitmap resized = Bitmap.createScaledBitmap(original, length, length, true);

    ByteArrayOutputStream blob = new ByteArrayOutputStream();
    resized.compress(Bitmap.CompressFormat.JPEG, 100, blob);

    return blob.toByteArray();
}

From source file:Main.java

public static Bitmap getScaled(Bitmap bm, int sidePx, boolean max) {
    float w = bm.getWidth();
    float h = bm.getHeight();
    float wRatio = sidePx / w;
    float hRatio = sidePx / h;
    float ratio = max ? Math.min(wRatio, hRatio) : Math.max(wRatio, hRatio);
    w = ratio * w;//from w  w w.  ja  v a  2  s  .  co m
    h = ratio * h;
    return Bitmap.createScaledBitmap(bm, (int) w, (int) h, true);
}

From source file:Main.java

public static ImageButton makeImageButton(Context context, int id, int resId, int width, int height,
        OnClickListener onClickListener, OnTouchListener onTouchListener) {
    Drawable icon;/*ww  w.  jav a2  s .  c  om*/
    ImageButton button = new ImageButton(context);
    button.setId(id);
    if (onClickListener != null)
        button.setOnClickListener(onClickListener);
    button.setBackgroundColor(Color.TRANSPARENT);
    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);
    button.setVisibility(View.VISIBLE);
    if (onTouchListener != null)
        button.setOnTouchListener(onTouchListener);
    return button;
}

From source file:Main.java

public static Bitmap scaleBitmap(Bitmap src, int maxWidth, int maxHeight) {
    if (src == null)
        return null;
    double scaleFactor = Math.min(((double) maxWidth) / src.getWidth(), ((double) maxHeight) / src.getHeight());
    return Bitmap.createScaledBitmap(src, (int) (src.getWidth() * scaleFactor),
            (int) (src.getHeight() * scaleFactor), false);
}

From source file:Main.java

public static Bitmap resizeBitmap(Bitmap src, int ratio) {
    if (src == null)
        return null;

    int width = src.getWidth();
    int height = src.getHeight();

    return Bitmap.createScaledBitmap(src, width * ratio, height * ratio, false);
}

From source file:Main.java

public static Bitmap formatUserAvatar(Bitmap photo) {
    int maskColor = 0xff424242;
    Paint cornerPaint = new Paint();
    cornerPaint.setAntiAlias(true);//from  w ww . j a  va  2s.c  o m
    cornerPaint.setColor(maskColor);
    Rect roundedCornerRect = new Rect(0, 0, 256, 256);
    RectF roundedCornerRectF = new RectF(roundedCornerRect);
    Bitmap roundedCornerBitmap = Bitmap.createBitmap(256, 256, Bitmap.Config.ARGB_8888);
    Canvas roundedCornerCanvas = new Canvas(roundedCornerBitmap);
    roundedCornerCanvas.drawARGB(0, 0, 0, 0);
    roundedCornerCanvas.drawRoundRect(roundedCornerRectF, 128, 128, cornerPaint);
    cornerPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    Bitmap scaledBitmap = Bitmap.createScaledBitmap(photo, 256, 256, true);
    roundedCornerCanvas.drawBitmap(scaledBitmap, roundedCornerRect, roundedCornerRect, cornerPaint);
    return roundedCornerBitmap;
}

From source file:Main.java

public static Bitmap decodeBitmapFromFileToSize(String filePath, int reqWidth, int reqHeight,
        boolean maintainAspectRatio) {
    Bitmap original = BitmapFactory.decodeFile(filePath);
    if (original == null) {
        return null;
    }/*from  w w w  . jav  a  2s . c o m*/
    if (original.getWidth() == reqWidth || original.getHeight() == reqHeight) {
        return original;
    }

    if (maintainAspectRatio) {
        int oriWidth = original.getWidth(), oriHeight = original.getHeight();
        if (oriWidth >= oriHeight) {
            float aRatio = (float) oriHeight / (float) oriWidth;
            reqHeight = (int) (reqWidth * aRatio);
        } else if (oriHeight > oriWidth) {
            float aRatio = (float) oriWidth / (float) oriHeight;
            reqWidth = (int) (reqHeight * aRatio);
        }
    }

    Bitmap scaled = Bitmap.createScaledBitmap(original, reqWidth, reqHeight, false);
    if (scaled == null) {
        return original;
    }
    if (scaled != original) {
        original.recycle();
        original = null;
    }
    return scaled;
}

From source file:Main.java

/**
 * This method gets the contact image from the phone book.
 * //from  w  w  w.  j  a v a  2 s .  c  o  m
 * @param context
 * @param imageUri
 * @return Contact's image or null
 */
public static Bitmap getByteContactImage(Context context, String imageUri, int pixel) {
    if (imageUri == null) {
        return null;
    }
    if (imageUri != null) {
        try {
            Uri image_Uri = Uri.parse(imageUri);
            Bitmap bitmap = MediaStore.Images.Media.getBitmap(context.getContentResolver(), image_Uri);
            bitmap = Bitmap.createScaledBitmap(bitmap, pixel, pixel, false);
            return getRoundedCornerBitmap(bitmap, pixel);
        } catch (IOException e) {
            Log.e("EPollUtil", e.getLocalizedMessage());
        }
    }

    return null;
}

From source file:Main.java

public static Bitmap forceEvenBitmapSize(Bitmap original) {
    int width = original.getWidth();
    int height = original.getHeight();

    if (width % 2 == 1) {
        width++;/*from   w  ww.  j  a v a2 s  .c  om*/
    }
    if (height % 2 == 1) {
        height++;
    }

    Bitmap fixedBitmap = original;
    if (width != original.getWidth() || height != original.getHeight()) {
        fixedBitmap = Bitmap.createScaledBitmap(original, width, height, false);
    }

    if (fixedBitmap != original) {
        original.recycle();
    }

    return fixedBitmap;
}