Android: padding left a bitmap with white color
Asked Answered
S

5

16

How to set all white the 10 rows on the left side of a Bitmap? I'v got a Bitmap that has to be padded on the left side. I thought i can create a new image iterate on the old one getpixel for each position and setpixel on the new one (white or colored) than return the new bitmap...is this wrong? Any suggestion? thanks a lot!

Subtenant answered 5/8, 2011 at 13:26 Comment(0)
K
32

You can instead create a new Bitmap with the extra padding number of pixels. Set this as the canvas bitmap and Color the entire image with the required color and then copy your bitmap.

public Bitmap pad(Bitmap Src, int padding_x, int padding_y) {
    Bitmap outputimage = Bitmap.createBitmap(Src.getWidth() + padding_x,Src.getHeight() + padding_y, Bitmap.Config.ARGB_8888);
    Canvas can = new Canvas(outputimage);
    can.drawARGB(FF,FF,FF,FF); //This represents White color
    can.drawBitmap(Src, padding_x, padding_y, null);
    return outputimage;
}
Kessia answered 5/8, 2011 at 13:46 Comment(4)
Perfect! thanks..just one more question..for x-offset you mean the first pixel left after padding?Subtenant
Lets say i have a bitmap with dimentions (w256, h104), wil this be able to make this image (w256, h128) by adding white spaces on top and bottom evenly without stretching the image?Mckenzie
Nice but it only add pixcels to top and left side of bitmap. nothing found for all 4 sides(top,left,right,bottom).Principle
@RahulSharma I just did this to give padding from all side. Hope it will help someone. Bitmap outputimage = Bitmap.createBitmap(Src.getWidth() + padding_x,Src.getHeight() + padding_y, Bitmap.Config.ARGB_8888); Canvas can = new Canvas(outputimage); can.drawBitmap(Src, padding_x, padding_y, null); Bitmap output = Bitmap.createBitmap(outputimage.getWidth()+padding_x, outputimage.getHeight() + padding_y, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(output); canvas.drawBitmap(outputimage, 0, 0, null); return output;Canella
M
9
public Bitmap addPaddingTopForBitmap(Bitmap bitmap, int paddingTop) {
    Bitmap outputBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight() + paddingTop, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(outputBitmap);
    canvas.drawColor(Color.RED);
    canvas.drawBitmap(bitmap, 0, paddingTop, null);
    return outputBitmap;
}

public Bitmap addPaddingBottomForBitmap(Bitmap bitmap, int paddingBottom) {
    Bitmap outputBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight() + paddingBottom, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(outputBitmap);
    canvas.drawColor(Color.RED);
    canvas.drawBitmap(bitmap, 0, 0, null);
    return outputBitmap;
}


public Bitmap addPaddingRightForBitmap(Bitmap bitmap, int paddingRight) {
    Bitmap outputBitmap = Bitmap.createBitmap(bitmap.getWidth() + paddingRight, bitmap.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(outputBitmap);
    canvas.drawColor(Color.RED);
    canvas.drawBitmap(bitmap, 0, 0, null);
    return outputBitmap;
}

public Bitmap addPaddingLeftForBitmap(Bitmap bitmap, int paddingLeft) {
    Bitmap outputBitmap = Bitmap.createBitmap(bitmap.getWidth() + paddingLeft, bitmap.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(outputBitmap);
    canvas.drawColor(Color.RED);
    canvas.drawBitmap(bitmap, paddingLeft, 0, null);
    return outputBitmap;
}
Mellisamellisent answered 19/5, 2017 at 2:55 Comment(1)
Thanks so much man. Your functions are easy to use. +1.Ur
C
0

Here's a kotlin extension function with RxJava to get it done. I haven't tested completely but based combined the previous answers to get something

fun Bitmap.pad(top: Float = 0F, bottom: Float = 0F, left: Float = 0F, right: Float = 0F): Single<Bitmap> {
    return Single.create<Bitmap> { emitter ->
        val output = Bitmap.createBitmap(
            (width + left + right).toInt(),
            (height + top + bottom).toInt(),
            Bitmap.Config.ARGB_8888
        )
        val canvas = Canvas(output)

        canvas.drawBitmap(this, left, top, null)

        emitter.onSuccess(output)
    }.subscribeOn(Schedulers.computation())
}

I think the coroutine version would simply be

suspend fun Bitmap.pad(top: Float = 0F, bottom: Float = 0F, left: Float = 0F, right: Float = 0F): Bitmap {
    val output = Bitmap.createBitmap(
        (width + left + right).toInt(),
        (height + top + bottom).toInt(),
        Bitmap.Config.ARGB_8888
    )
    val canvas = Canvas(output)

    canvas.drawBitmap(this, left, top, null)

    return output
}
Catullus answered 16/1, 2020 at 14:15 Comment(0)
C
0

I just did this to give padding from all side. Hope it will help someone. Combination of https://mcmap.net/q/717373/-android-padding-left-a-bitmap-with-white-color & https://mcmap.net/q/717373/-android-padding-left-a-bitmap-with-white-color these answer.

Bitmap outputimage = Bitmap.createBitmap(Src.getWidth() + padding_x,Src.getHeight() + padding_y, Bitmap.Config.ARGB_8888);
        Canvas can = new Canvas(outputimage);
        can.drawBitmap(Src, padding_x, padding_y, null);

        Bitmap output = Bitmap.createBitmap(outputimage.getWidth()+padding_x, outputimage.getHeight() + padding_y, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(output);
        canvas.drawBitmap(outputimage, 0, 0, null);    
  return output;
Canella answered 9/5, 2020 at 10:14 Comment(0)
G
-1

You might want to look here:

http://download.oracle.com/javase/1.4.2/docs/api/java/awt/image/BufferedImage.html

methods you might want to use are: getHeight() then you know how many pixels to set and iterate over 10 columns

and setRGB (int x, int y, int RGB) to set the pixel

Ghana answered 5/8, 2011 at 13:46 Comment(1)
does this works on android? i'v got a Bitmap (android class) image..ho to switch to BufferedImage? thanks!Subtenant

© 2022 - 2024 — McMap. All rights reserved.