Add Watermark to Image in android

Hope everyone is doing well, So today's code snippet is for the programmers who wanted to add watermark to their image dynamically. This will help you when you want to put watermark to your every image in the list or simply just want to add watermark once in an application.

So Not wasting in your time here is the snippet for adding watermark to your image.

Prerequisites: 1) Your source image must be in Bitmap.

                         2) Watermark Text.

                         3) Size and color for the Text.

Here is the code:-

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
public Bitmap addWaterMarkText(Bitmap sourceBitmap, String waterMarkText, Color watermarkColor, int waterMarkSize){
    int sourceWidth = sourceBitmap.getWidth();
    int sourceHeight = sourceBitmap.getHeight();
	
    Bitmap waterMarkedResult = Bitmap.createBitmap(sourceWidth, sourceHeight, sourceBitmap.getConfig());
    Canvas canvas = new Canvas(waterMarkedResult);
    canvas.drawBitmap(sourceBitmap, 0, 0, null);
    Paint paint = new Paint();
    paint.setColor(watermarkColor);
    paint.setTextSize(waterMarkSize);
    paint.setAntiAlias(true);
    
	canvas.drawText(waterMarkText, xPosition, yPosition, paint);

    return waterMarkedResult;
}

You can call this method anywhere in your application's activity, and can see the result.

Here is a sample output:-

Add watermark to image in android


Thank You.

Happy Coding!

Comments