How might I add a watermark effect to an image in Android?
Asked Answered
G

8

22

I have an image with frames and I need to add a watermark effect. How might I do this?

Glyptic answered 21/5, 2012 at 4:15 Comment(0)
D
41

I found great tutorial on Android Image Processing here.

public static Bitmap mark(Bitmap src, String watermark, Point location, Color color, int alpha, int size, boolean underline) {
    int w = src.getWidth();
    int h = src.getHeight();
    Bitmap result = Bitmap.createBitmap(w, h, src.getConfig());

    Canvas canvas = new Canvas(result);
    canvas.drawBitmap(src, 0, 0, null);

    Paint paint = new Paint();
    paint.setColor(color);
    paint.setAlpha(alpha);
    paint.setTextSize(size);
    paint.setAntiAlias(true);
    paint.setUnderlineText(underline);
    canvas.drawText(watermark, location.x, location.y, paint);

    return result;
}

Thanks to Pete Houston who shares such useful tutorial on basic image processing.

Depersonalization answered 21/5, 2012 at 11:20 Comment(5)
Can i call this method inside another function where image is specified?Glyptic
You can call it in another function.For example - Bitmap result = mark(src, watermark, location, color, alpha, size, underline);Depersonalization
The param Color color should be int color, the method paint.setColor() is waiting for argument int. If you have a better idea, please share it with us.Forked
Hi, it is not working. I using it on an image view inside a fragmentLorianne
After i captured image I'm saving it on storage by converting it into bitmap. So, When i used the above code to write water mark on that bitmap image before saving. It is not working. Can you give more detailsPaymaster
N
20

For others reference, if you want to add the logo of your application (which is in your drawable folder(s)) on top of image use following method:

private Bitmap addWaterMark(Bitmap src) {
        int w = src.getWidth();
        int h = src.getHeight();
        Bitmap result = Bitmap.createBitmap(w, h, src.getConfig());
        Canvas canvas = new Canvas(result);
        canvas.drawBitmap(src, 0, 0, null);

        Bitmap waterMark = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.logo);
        canvas.drawBitmap(waterMark, 0, 0, null);

        return result;
    }
Nonunionism answered 28/3, 2014 at 7:28 Comment(3)
i´m getting null from the waterMark Bitmap, why would that be?Milium
I'm not getting anything on image. It is just getting saved without watermark. Any help !Paymaster
Comments aren't answered, why is that? Are you guy OK? I am also not getting anything above the image.Election
P
14

If someone is still searching for this, I found a good solution here

It adds a watermark to the bottom right portion and scales it according to the source image which was exactly what I was looking for.

/**
 * Embeds an image watermark over a source image to produce
 * a watermarked one.
 * @param source The source image where watermark should be placed
 * @param watermark Watermark image to place
 * @param ratio A float value < 1 to give the ratio of watermark's height to image's height,
 *             try changing this from 0.20 to 0.60 to obtain right results
 */
public static Bitmap addWatermark(Bitmap source, Bitmap watermark, float ratio) {
    Canvas canvas;
    Paint paint;
    Bitmap bmp;
    Matrix matrix;
    RectF r;

    int width, height;
    float scale;

    width = source.getWidth();
    height = source.getHeight();

    // Create the new bitmap
    bmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    paint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG | Paint.FILTER_BITMAP_FLAG);

    // Copy the original bitmap into the new one
    canvas = new Canvas(bmp);
    canvas.drawBitmap(source, 0, 0, paint);

    // Scale the watermark to be approximately to the ratio given of the source image height
    scale = (float) (((float) height * ratio) / (float) watermark.getHeight());

    // Create the matrix
    matrix = new Matrix();
    matrix.postScale(scale, scale);

    // Determine the post-scaled size of the watermark
    r = new RectF(0, 0, watermark.getWidth(), watermark.getHeight());
    matrix.mapRect(r);

    // Move the watermark to the bottom right corner
    matrix.postTranslate(width - r.width(), height - r.height());

    // Draw the watermark
    canvas.drawBitmap(watermark, matrix, paint);

    return bmp;
}

And it is well commented which is what is a huge plus!

Pageantry answered 15/3, 2018 at 12:47 Comment(2)
Good Job @IrshadThumb
Works perfectly for me.Revolt
A
4

It seems you are looking for a waterrippleeffect as this one. Checkout the complete source code. Also check the screenshot how does the effect look like.

Aeriell answered 21/5, 2012 at 4:25 Comment(0)
I
1

In Kotlin:

Note: Its just modified code of above answers

private fun mark(src: Bitmap, watermark: String): Bitmap {
        val w = src.width
        val h = src.height
        val result = Bitmap.createBitmap(w, h, src.config)
        val canvas = Canvas(result)
        canvas.drawBitmap(src, 0f, 0f, null)
        val paint = Paint()
        paint.color = Color.RED
        paint.textSize = 10f
        paint.isAntiAlias = true
        paint.isUnderlineText = true
        canvas.drawText(watermark, 20f, 25f, paint)
        return result
    }

val imageBitmap = mark(yourBitmap, "Your Text")
binding.meetProofImageView.setImageBitmap(imageBitmap)
Intermingle answered 8/7, 2022 at 6:42 Comment(0)
C
0

You can use androidWM to add a watermark into your image, even with invisible watermarks:

add dependence:

dependencies {
  ...
  implementation 'com.huangyz0918:androidwm:0.2.3'
  ...
}

and java code:

 WatermarkText watermarkText = new WatermarkText(“Hello World”)
                    .setPositionX(0.5) 
                    .setPositionY(0.5) 
                    .setTextAlpha(100) 
                    .setTextColor(Color.WHITE) 
                    .setTextFont(R.font.champagne) 
                    .setTextShadow(0.1f, 5, 5, Color.BLUE); 

 WatermarkBuilder.create(this, backgroundBitmap) 
                    .loadWatermarkText(watermarkText) 
                    .getWatermark() 
                    .setToImageView(backgroundView); 

You can easily add an image type watermark or a text watermark like this, and the library size is smaller than 30Kb.

Calycine answered 23/3, 2019 at 16:56 Comment(1)
don't post a link of a tool as an answer, specially if you wrote it, you need to disclose affiliation, else it's spamSitton
O
0

I tried a few libraries mentioned in other posts, like this, but unfortunately it is missing, and not downloadable now. So I followed AndroidLearner 's answer above, but after tweaking the code a little bit, for those of you who are having trouble rotating the watermark, and what values are valid for the various methods of Paint class, so that the text shows rotated at an angle(like most of the company watermarks do), you can use the below code.
Note that, w and h are the screen width and height respectively, which you can calculate easily, there are tons of ways you can find on stackoverflow only.

public static Bitmap waterMarkBitmap(Bitmap src, String watermark) {
    int w = src.getWidth();
    int h = src.getHeight();
    Bitmap mutableBitmap = Utils.getMutableBitmap(src);
    Bitmap result = Bitmap.createBitmap(w, h, mutableBitmap.getConfig());

    Canvas canvas = new Canvas(result);
    canvas.drawBitmap(src, 0f, 0f, null);

    Paint paint = new Paint();
    paint.setColor(Color.RED);
    paint.setTextSize(92f);
    paint.setAntiAlias(true);
    paint.setAlpha(70);  // accepts value between 0 to 255, 0 means 100% transparent, 255 means 100% opaque.
    paint.setUnderlineText(false);
    canvas.rotate(45, w / 10f, h / 4f);
    canvas.drawText(watermark, w / 10f, h / 4f, paint);
    canvas.rotate(-45, w / 10f, h / 4f);

    return result;
}

It rotates the text watermark by 45 degrees, and places it at the centre of the bitmap.

Also note that, in case you are not able to get watermark, it might be the case that the bitmap you are using as source is immutable. For this worst case scenario, you can use below method to create a mutable bitmap from an immutable one.

public static Bitmap getMutableBitmap(Bitmap immutableBitmap) {
    if (immutableBitmap.isMutable()) {
        return immutableBitmap;
    }

    Bitmap workingBitmap = Bitmap.createBitmap(immutableBitmap);
    return workingBitmap.copy(Bitmap.Config.ARGB_8888, true);
}

I found above method inside here. I have tested using both the methods in my application, and it works perfectly after I added above tweaks. Try it and let me know if it works or not.

Osswald answered 26/12, 2022 at 8:55 Comment(0)
P
-5

use framelayout. put two imageviews inside the framelayout and specify the position of the watermark imageview.

Particular answered 21/5, 2012 at 4:22 Comment(1)
Watermark means that second image will be a part from the first image, then can be used like sharing the first image with water mark. Framelayout just just present two pictures, but doesn't change themChrisman

© 2022 - 2025 — McMap. All rights reserved.