How to Add a Professional Watermark to Images Using Java

How to Add a Professional Watermark to Images Using Java (2026)

How to Add a Professional Watermark to Images Using Java

Watermarking images is a common requirement for photographers, bloggers, and content creators who want to protect their work. In this tutorial, you will learn how to add a professional text watermark to JPG images using Java, using the built-in BufferedImage and Graphics2D classes — no third-party library required.

We will add a two-line watermark (name + description) at the bottom of an image, with a semi-transparent black background box and white anti-aliased text.

What You Need

  • JDK 8 or later (Java 11+ recommended)
  • An input image (JPG, PNG, or GIF)
  • A text editor or IDE (IntelliJ IDEA, Eclipse, VS Code)

No external libraries are required — everything we use is part of the standard java.awt, java.awt.image, and javax.imageio packages.

Complete Java Watermark Code

package com.deepsingh44;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;

public class WatermarkExample {

    public static void main(String[] args) throws Exception {

        // Input and output file paths
        String inputFile  = "C:\\Users\\Deeps\\Downloads\\swan.JPG";
        String outputFile = "D:\\swan.JPG";

        // Load the source image
        BufferedImage image = ImageIO.read(new File(inputFile));

        // Watermark text
        String name        = "Deep Singh";
        String description = "Professional Photographer";

        // Create Graphics2D object for drawing
        Graphics2D g2d = image.createGraphics();

        // Enable anti-aliasing for smooth text
        g2d.setRenderingHint(
                RenderingHints.KEY_TEXT_ANTIALIASING,
                RenderingHints.VALUE_TEXT_ANTIALIAS_ON
        );

        // Padding and position
        int padding = 15;
        int x = 20;
        int bottom = image.getHeight() - 20;

        // Fonts
        Font nameFont = new Font("Arial", Font.BOLD, 28);
        Font descFont = new Font("Arial", Font.PLAIN, 20);

        FontMetrics nameMetrics = g2d.getFontMetrics(nameFont);
        FontMetrics descMetrics = g2d.getFontMetrics(descFont);

        // Calculate box size
        int textWidth = Math.max(
                nameMetrics.stringWidth(name),
                descMetrics.stringWidth(description)
        );
        int textHeight = nameMetrics.getHeight() + descMetrics.getHeight();

        // Draw semi-transparent background box
        g2d.setComposite(
                AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f)
        );
        g2d.setColor(Color.BLACK);
        g2d.fillRoundRect(
                x - padding,
                bottom - textHeight - padding,
                textWidth + padding * 2,
                textHeight + padding * 2,
                10, 10
        );

        // Reset transparency for the text
        g2d.setComposite(
                AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.0f)
        );

        // Draw white text
        g2d.setColor(Color.WHITE);

        int nameY = bottom - descMetrics.getHeight();

        g2d.setFont(nameFont);
        g2d.drawString(name, x, nameY);

        g2d.setFont(descFont);
        g2d.drawString(description, x, bottom);

        // Release resources
        g2d.dispose();

        // Save the watermarked image
        ImageIO.write(image, "JPG", new File(outputFile));
    }
}

Step-by-Step Explanation

1. Load the Image

ImageIO.read(new File(inputFile)) loads the source image into a BufferedImage. This is the base image we will modify.

2. Create a Graphics2D Object

image.createGraphics() returns a Graphics2D instance, which provides methods to draw text, shapes, and images on top of the source image.

3. Enable Anti-Aliasing

Anti-aliasing smooths the edges of the watermark text, making it look clean and professional instead of pixelated.

4. Define Fonts and Measure Text

We use two fonts: a bold font for the name and a plain font for the description. FontMetrics helps calculate how wide and tall the text will be, so the background box fits perfectly.

5. Draw the Semi-Transparent Background

We use AlphaComposite with an alpha value of 0.5f to draw a black rounded rectangle behind the text. This keeps the text readable without hiding the underlying image completely.

6. Draw the White Watermark Text

We reset the alpha composite to 1.0f (fully opaque) so the text appears sharp, then draw the name and description using drawString().

7. Dispose and Save

g2d.dispose() releases the graphics resources. Finally, ImageIO.write() saves the modified image to the output path.

Result: Watermarked Image

After running the code above, the output image looks like this:

Customization Ideas

You can easily extend this example:

  • Change position: Move the watermark to the top-right, center, or tiled across the image.
  • Change opacity: Use 0.3f for a lighter watermark or 0.8f for a stronger one.
  • Use a logo: Instead of text, draw a PNG logo using g2d.drawImage().
  • Rotate the text: Use g2d.rotate(Math.toRadians(-30)) for a diagonal watermark.
  • Batch processing: Loop through all images in a folder to apply the watermark automatically.
  • PNG transparency: Save as PNG to preserve transparency in the watermark.

Frequently Asked Questions

How do I add a watermark to an image in Java?

Use ImageIO.read() to load the image into a BufferedImage, then call image.createGraphics() to get a Graphics2D object. Set the font, color, and AlphaComposite for transparency, then use drawString() to draw the watermark text. Finally, dispose the graphics and save the image using ImageIO.write().

What is the best way to make a watermark transparent in Java?

Use AlphaComposite.getInstance(AlphaComposite.SRC_OVER, opacity) where opacity is between 0.0f (fully transparent) and 1.0f (fully opaque). A value like 0.5f gives a semi-transparent watermark that does not hide the image.

Which image formats support watermarking in Java?

Java's ImageIO supports JPG, PNG, GIF, BMP, and WBMP out of the box. For PNG or GIF, you can use transparency; for JPG, the watermark will be drawn on top of the image.

Why is my watermark text blurry in Java?

Enable text anti-aliasing using g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON). This smooths the edges of the text and produces a professional look.

Can I add an image watermark instead of text in Java?

Yes. Load the watermark image with ImageIO.read(), set AlphaComposite for transparency, and use g2d.drawImage(watermark, x, y, null) to draw it on top of the base image.

Conclusion

Adding a watermark to images in Java is simple once you understand BufferedImage, Graphics2D, and AlphaComposite. With just a few lines of code, you can protect your images with a professional-looking watermark — no third-party library needed.

Try customizing the position, font, opacity, and text to match your brand, and consider batch-processing a folder of images for efficiency.

Related topics: Java ImageIO, Graphics2D Tutorial, Java Image Processing, BufferedImage Examples, Java File Handling

No comments: