How to Add Text or Image Watermarks Using Java Graphics2D

This guide walks through reading local or network images in Java, creating a Graphics2D canvas, and applying either a red semi‑transparent text watermark or a scaled image watermark at pixel‑specified coordinates before saving the result as a PNG file.

The Dominant Programmer
The Dominant Programmer
The Dominant Programmer
How to Add Text or Image Watermarks Using Java Graphics2D

The article explains how to add text or image watermarks to pictures in Java using the Graphics2D API.

Preparation

First import the required classes:

import org.apache.commons.lang3.StringUtils;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.URL;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

Utility class

WatermarkUtils provides three static methods: public static Image readLocalPicture(String path) reads a local file into an Image, throwing RuntimeException if the path is null or reading fails. public static Image readNetworkPicture(String path) creates a URL, reads the image via ImageIO.read(url.openStream()), and validates the result.

public static String manageWatermark(Image image, Integer type, String watermark, int x, int y, String tarImgPath)

creates a BufferedImage with the same dimensions as the source, draws the original image, then depending on type adds either a text watermark (red semi‑transparent, font “微软雅黑” Bold 45pt, drawn at (x,y)) or an image watermark (scaled to 50×50, alpha 0.9). After drawing, the method writes the result as a PNG to tarImgPath and returns a success message.

Testing

Example for a text watermark:

Image image = readLocalPicture("D://test//test.png");
manageWatermark(image, 1, "霸道的程序猿", 150, 50,
    "D://test//testResult" + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss")) + ".png");

The coordinates are pixel values that can be determined with an image editor.

Example for an image watermark:

Image image = readLocalPicture("D://test//test.png");
manageWatermark(image, 2, "D://test//mark.jpg", 150, 50,
    "D://test//testResult" + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss")) + ".png");

Resulting screenshots show the watermark placed at the specified location.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaWatermarkImageProcessingGraphics2DBufferedImage
The Dominant Programmer
Written by

The Dominant Programmer

Resources and tutorials for programmers' advanced learning journey. Advanced tracks in Java, Python, and C#. Blog: https://blog.csdn.net/badao_liumang_qizhi

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.