It’s interesting and fairly simple to add/embed a watermark over an image using Java graphics API. The steps are as follows:

  • Read the source image file into a BufferedImage object using the ImageIO.read() method.
  • Obtain graphics context of the BufferedImage object.
  • Using the Graphics2D object to pain 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.
Using this technique, you can develop more complex programs that provide watermark-related functionalities such as adding watermarks in batch, embedding watermarks conditionally, etc.

In this tutorial, we introduce code examples for two typical scenarios: textual watermark and image watermark.

 

1. Java code example to add textual watermark for an image

The following utility method adds a textual watermark over an image. The code comments are self-explanatory:

/**
 * 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.1f);
		g2d.setComposite(alphaChannel);
		g2d.setColor(Color.BLUE);
		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);
	}
}
Example usage:

File sourceImageFile = new File("e:/Test/Watermark/SwingEmailSender.png");
File destImageFile = new File("e:/Test/Watermark/text_watermarked.png");

addTextWatermark("CodeJava", sourceImageFile, destImageFile);
The sample source image:

SwingEmailSender

The output image:



text watermarked

 

 

2. Java code example to add image watermark for an image

And the following utility method adds an image watermark over an image:

/**
 * 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);
	}
}
Example usage:

File sourceImageFile = new File("e:/Test/Watermark/SwingEmailSender.png");
File watermarkImageFile = new File("e:/Test/Watermark/CodeJavaLogo.png");
File destImageFile = new File("e:/Test/Watermark/text_watermarked.png");

addImageWatermark(watermarkImageFile, sourceImageFile, destImageFile);
The sample source image:

SwingEmailSender

The watermark image:

CodeJavaLogo

The output image:

image watermarked

 

Other Java Graphics Tutorials:


About the Author:

is certified Java programmer (SCJP and SCWCD). He started programming with Java in the time of Java 1.4 and has been falling in love with Java since then. Make friend with him on Facebook and watch his Java videos you YouTube.



Attachments:
Download this file (ImageWatermarkExamples.java)ImageWatermarkExamples.java[ ]3 kB

Add comment

   


Comments 

#15Nam2021-04-26 08:44
I don't know, Bernard.
Maybe you need to use image recognition and machine learning.
Quote
#14Bernard2021-04-26 07:08
How about how to detect a watermark
Quote
#13sss2018-08-28 02:30
what about adding watermark imag in excel sheel in bground
Quote
#12bala2016-11-24 00:39
nice example,thanks namha
Quote
#11Samir2016-08-01 02:21
Excellent example
Quote