Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

How to create watermarks with Java

In this article we will discuss about how to add a watermarking over an image using Java to do this.

What is Watermarking

Before diving into coding first let us understand what is watermarking. As per the wikepidia definition:

“A watermark is a recognizable image or pattern in paper that appears as various shades of lightness/darkness when viewed by transmitted light (or when viewed by reflected light, atop a dark background), caused by thickness or density variations in the paper.”

Digital Watermarking

There is another kind of watermarking, called Digital watermarking. The definition as per wikipedia is:

“A digital watermark is a kind of marker covertly embedded in a noise-tolerant signal such as audio or image data. It is typically used to identify ownership of the copyright of such signal. "Watermarking" is the process of hiding digital information in a carrier signal; the hidden information should, but does not need to contain a relation to the carrier signal.”

Purpose of watermarking

The purpose of a watermark is to identify the work and discourage its unauthorized use. Though a visible watermark can't prevent unauthorized use, it makes it more difficult for those who may want to claim someone else's photo or art work as their own.

Watermarking Techniques

There are tools available like Photoshop, GIMP and others to watermark your images. Watermarking can also be achieved using the Java coding techniques. For this you need, Java Graphics API. Let us first list down the steps we need to follow to watermark our image. In our example we will create textual watermark and image watermark.

Steps

  • Read the source image file into a BufferedImage object using the ImageIO.read() method.
  • Obtain graphics context of the BufferedImage object.
  • Use the Graphics2D object to paint the watermark which can be a String, an image or whatever can be drawn with the Graphics2D’s API. But basically, the watermark is usually translucent so an alpha channel is needed.
  • Write the output image using the ImageIO.write() method.

Let us put the above steps in practice. The following code create two images one textually watermarked and a second image watermarked.

Listing 1: Watermarking Example

package com.mrbool;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class ImageWatermarkExample {
	/**
	 * Embeds a textual watermark over a source image to produce a watermarked
	 * one.
	 * 
	 * @param text
	 *            The text to be embedded as watermark.
	 * @param sourceImageFile
	 *            The source image file.
	 * @param destImageFile
	 *            The output image file.
	 */
	static void addTextWatermark(String text, File sourceImageFile,
			File destImageFile) {
		try {
			BufferedImage sourceImage = ImageIO.read(sourceImageFile);
			Graphics2D g2d = (Graphics2D) sourceImage.getGraphics();

			// initializes necessary graphic properties
			AlphaComposite alphaChannel = AlphaComposite.getInstance(
					AlphaComposite.SRC_OVER, 0.3f);
			g2d.setComposite(alphaChannel);
			g2d.setColor(Color.RED);
			g2d.setFont(new Font("Arial", Font.BOLD, 64));
			FontMetrics fontMetrics = g2d.getFontMetrics();
			Rectangle2D rect = fontMetrics.getStringBounds(text, g2d);

			// calculates the coordinate where the String is painted
			int centerX = (sourceImage.getWidth() - (int) rect.getWidth()) / 2;
			int centerY = sourceImage.getHeight() / 2;

			// paints the textual watermark
			g2d.drawString(text, centerX, centerY);

			ImageIO.write(sourceImage, "png", destImageFile);
			g2d.dispose();

			System.out.println("The tex watermark is added to the image.");

		} catch (IOException ex) {
			System.err.println(ex);
		}
	}

	/**
	 * Embeds an image watermark over a source image to produce a watermarked
	 * one.
	 * 
	 * @param watermarkImageFile
	 *            The image file used as the watermark.
	 * @param sourceImageFile
	 *            The source image file.
	 * @param destImageFile
	 *            The output image file.
	 */
	static void addImageWatermark(File watermarkImageFile,
			File sourceImageFile, File destImageFile) {
		try {
			BufferedImage sourceImage = ImageIO.read(sourceImageFile);
			BufferedImage watermarkImage = ImageIO.read(watermarkImageFile);

			// initializes necessary graphic properties
			Graphics2D g2d = (Graphics2D) sourceImage.getGraphics();
			AlphaComposite alphaChannel = AlphaComposite.getInstance(
					AlphaComposite.SRC_OVER, 0.3f);
			g2d.setComposite(alphaChannel);

			// calculates the coordinate where the image is painted
			int topLeftX = (sourceImage.getWidth() - watermarkImage.getWidth()) / 2;
			int topLeftY = (sourceImage.getHeight() - watermarkImage
					.getHeight()) / 2;

			// paints the image watermark
			g2d.drawImage(watermarkImage, topLeftX, topLeftY, null);

			ImageIO.write(sourceImage, "png", destImageFile);
			g2d.dispose();

			System.out.println("The image watermark is added to the image.");

		} catch (IOException ex) {
			System.err.println(ex);
		}
	}

	public static void main(String[] args) {
		File sourceImageFile = new File("/home/manisha/originalimage.jpg");
		File destImageFile = new File("/home/manisha/text_watermarked.png");

		addTextWatermark("Mrbool", sourceImageFile, destImageFile);

		destImageFile = new File("/home/manisha/image_watermarked.png");

		File watermarkImageFile = new File("/home/manisha/logo.jpg");
		addImageWatermark(watermarkImageFile, sourceImageFile, destImageFile);
	}
}

Result:

Execute the above code. You would get the following output on console:

The tex watermark is added to the image.
The image watermark is added to the image.

Details:

The piece of code to create textual watermark from the above java code is as below:

/**
	 * Embeds a textual watermark over a source image to produce a watermarked
	 * one.
	 * 
	 * @param text
	 *            The text to be embedded as watermark.
	 * @param sourceImageFile
	 *            The source image file.
	 * @param destImageFile
	 *            The output image file.
	 */
	static void addTextWatermark(String text, File sourceImageFile,
			File destImageFile) {
		try {
			BufferedImage sourceImage = ImageIO.read(sourceImageFile);
			Graphics2D g2d = (Graphics2D) sourceImage.getGraphics();

			// initializes necessary graphic properties
			AlphaComposite alphaChannel = AlphaComposite.getInstance(
					AlphaComposite.SRC_OVER, 0.3f);
			g2d.setComposite(alphaChannel);
			g2d.setColor(Color.RED);
			g2d.setFont(new Font("Arial", Font.BOLD, 64));
			FontMetrics fontMetrics = g2d.getFontMetrics();
			Rectangle2D rect = fontMetrics.getStringBounds(text, g2d);

			// calculates the coordinate where the String is painted
			int centerX = (sourceImage.getWidth() - (int) rect.getWidth()) / 2;
			int centerY = sourceImage.getHeight() / 2;

			// paints the textual watermark
			g2d.drawString(text, centerX, centerY);

			ImageIO.write(sourceImage, "png", destImageFile);
			g2d.dispose();

			System.out.println("The tex watermark is added to the image.");

		} catch (IOException ex) {
			System.err.println(ex);
		}
	}

Usage:

File sourceImageFile = new File("/home/manisha/originalimage.jpg");
File destImageFile = new File("/home/manisha/text_watermarked.png");

addTextWatermark("Mrbool", sourceImageFile, destImageFile);

Original Image: placed at the path /home/manisha/originalimage.jpg

originalimage.jpg

Figure 1: originalimage.jpg

Result images:

text_watermarked.png

Figure 2: text_watermarked.png

The code to create image watermark is:

/**
	 * Embeds an image watermark over a source image to produce a watermarked
	 * one.
	 * 
	 * @param watermarkImageFile
	 *            The image file used as the watermark.
	 * @param sourceImageFile
	 *            The source image file.
	 * @param destImageFile
	 *            The output image file.
	 */
	static void addImageWatermark(File watermarkImageFile,
			File sourceImageFile, File destImageFile) {
		try {
			BufferedImage sourceImage = ImageIO.read(sourceImageFile);
			BufferedImage watermarkImage = ImageIO.read(watermarkImageFile);

			// initializes necessary graphic properties
			Graphics2D g2d = (Graphics2D) sourceImage.getGraphics();
			AlphaComposite alphaChannel = AlphaComposite.getInstance(
					AlphaComposite.SRC_OVER, 0.3f);
			g2d.setComposite(alphaChannel);

			// calculates the coordinate where the image is painted
			int topLeftX = (sourceImage.getWidth() - watermarkImage.getWidth()) / 2;
			int topLeftY = (sourceImage.getHeight() - watermarkImage
					.getHeight()) / 2;

			// paints the image watermark
			g2d.drawImage(watermarkImage, topLeftX, topLeftY, null);

			ImageIO.write(sourceImage, "png", destImageFile);
			g2d.dispose();

			System.out.println("The image watermark is added to the image.");

		} catch (IOException ex) {
			System.err.println(ex);
		}
	}

Usage:

destImageFile = new File("/home/manisha/image_watermarked.png");
File watermarkImageFile = new File("/home/manisha/logo.jpg");
addImageWatermark(watermarkImageFile, sourceImageFile, destImageFile);

The image used to create watermark effect is:

logo.jpg

Figure 3: logo.jpg

 image_watermarked.png

Figure 4: image_watermarked.png

Conclusion:

In this article we saw how to achieve watermarking using Java code in simple and easy steps. Hope you liked the article.

See also



I''m a full stack developer with around 10+ yrs of experience. I enjoy writing technical articles on upcoming technical trends.

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login