Share on Google+Share on Google+

Java Image Watermarking

In this section, you will learn how to watermark an image using java swing.

Java Image Watermarking

A watermarking is a technique that allows an individual to add copyright notices or other verification messages to digital audio, video, or image signals and documents. It  may be visible, hidden, or a combination of both. In this section, you will learn how to watermark an image using java swing.

Here is the code:

import java.awt.*;
import java.awt.image.*;
import javax.imageio.*;
import javax.swing.ImageIcon;
import java.awt.geom.Rectangle2D;

public class ImageWatermarking {
	public static void main(String[] args) {

		try {
			File file = new File("C:/rose.jpg");
			if (!file.exists()) {
				System.out.println("File not Found");
			ImageIcon icon = new ImageIcon(file.getPath());
			BufferedImage bufferedImage = new BufferedImage(
					icon.getIconWidth(), icon.getIconHeight(),
			Graphics2D g2d = (Graphics2D) bufferedImage.getGraphics();
			g2d.drawImage(icon.getImage(), 0, 0, null);
			AlphaComposite alpha = AlphaComposite.getInstance(
					AlphaComposite.SRC_OVER, 0.5f);
			g2d.setFont(new Font("Arial", Font.BOLD, 30));
			String watermark = "Hello World";
			FontMetrics fontMetrics = g2d.getFontMetrics();
			Rectangle2D rect = fontMetrics.getStringBounds(watermark, g2d);
			g2d.drawString(watermark, (icon.getIconWidth() - (int) rect
					.getWidth()) / 2, (icon.getIconHeight() - (int) rect
					.getHeight()) / 2);
			File fileout = new File("C:/watermarkedImage.jpg");
			ImageIO.write(bufferedImage, "jpg", fileout);
		} catch (IOException ioe) {



Posted on: July 24, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.