Jed84
Jed84

Reputation: 171

Android: padding left a bitmap with white color

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!

Upvotes: 16

Views: 15490

Answers (5)

Addy
Addy

Reputation: 91

I just did this to give padding from all side. Hope it will help someone. Combination of https://stackoverflow.com/a/44060669/6480433 & https://stackoverflow.com/a/6957333/6480433 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;

Upvotes: 0

William Reed
William Reed

Reputation: 1844

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
}

Upvotes: 0

Linh
Linh

Reputation: 60973

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;
}

Upvotes: 9

Deepak
Deepak

Reputation: 1141

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;
}

Upvotes: 32

SnowyTracks
SnowyTracks

Reputation: 1985

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

Upvotes: -1

Related Questions