SoFunction
Updated on 2025-04-13

Java adds watermarks to images and saves implementation methods (with source code)

Project background

In the field of image processing, watermarking is a common method to protect copyright and identify the ownership of images. A watermark can be a text, a watermark image, or a transparent layer, which is usually located in the center or corner of the image. In many applications, such as image sharing, photo album management, etc., we may need to add watermarks to the image.

Java provides powerful image processing capabilities that can be usedGraphics2DClass draws watermarks on the image. The goal of this project is to implement a Java program that can add text or image watermarks to images and save the modified image as a file.

Related knowledge

BufferedImage

BufferedImageIt is a class used in Java to represent images, which allows pixel-level operations on images. The image can be passedGraphics2DDraw, scale, add watermarks, etc.

Graphics2D

Graphics2DyesGraphicsA subclass of graphic drawing. passGraphics2D, We can draw various elements on the image, such as text, pictures, shapes, etc., and control the drawing's transparency, color, font and other attributes.

ImageIO

ImageIOClass is used to read images from files, or save images as files of a specified format (such as PNG, JPEG, etc.).

Implementation ideas

  • Loading the image:use()Load the image from the file.
  • Add watermark
    • Text watermark:passGraphics2DofdrawString()Method to draw text on an image.
    • Picture watermark:passGraphics2DofdrawImage()Method draw another image as a watermark onto the target image.
  • Save the image:use()Save the modified image as a file.

Implement code

The following is a Java program that implements adding text watermarks and image watermarks to images:

1. Java code: add watermark and save image

import ;
import .*;
import ;
import ;
import ;

public class ImageWatermark {

    public static void main(String[] args) {
        try {
            // 1. Load the image            File inputFile = new File("path/to/your/");  // Modify to your image path            BufferedImage image = (inputFile);

            // 2. Add text watermark            String textWatermark = "Sample Watermark";
            BufferedImage textWatermarkedImage = addTextWatermark(image, textWatermark);

            // 3. Add image watermark            File watermarkImageFile = new File("path/to/your/watermark_image.png");  // Modify to watermark image path            BufferedImage watermarkImage = (watermarkImageFile);
            BufferedImage finalImage = addImageWatermark(textWatermarkedImage, watermarkImage);

            // 4. Save the image after adding the watermark            File outputFile = new File("path/to/save/watermarked_image.png");  // Output file path            (finalImage, "PNG", outputFile);  // Save as PNG format
            ("Watermark added and image saved successfully!");

        } catch (IOException e) {
            ();
        }
    }

    // Add text watermark    private static BufferedImage addTextWatermark(BufferedImage originalImage, String watermarkText) {
        Graphics2D g2d = (Graphics2D) ();

        // Set font and transparency
        Font font = new Font("Arial", , 50);
        (font);
        (new Color(255, 255, 255, 128));  // White and translucent
        // Get the watermark's position (center the watermark on the image)
        FontMetrics fontMetrics = ();
        int x = (() - (watermarkText)) / 2;
        int y = () / 2;

        // Draw watermark text
        (watermarkText, x, y);

        // Dispose the graphics context
        ();

        return originalImage;
    }

    // Add image watermark    private static BufferedImage addImageWatermark(BufferedImage originalImage, BufferedImage watermarkImage) {
        Graphics2D g2d = (Graphics2D) ();

        // Get the watermark image's position (bottom-right corner)
        int x = () - () - 10;  // 10px margin from the right edge
        int y = () - () - 10;  // 10px margin from the bottom edge

        // Draw watermark image with transparency
        ((AlphaComposite.SRC_OVER, 0.5f));  // Set transparency
        (watermarkImage, x, y, null);

        // Dispose the graphics context
        ();

        return originalImage;
    }
}

Code interpretation

  • Loading the image

    • use(inputFile)Read the image file of the specified path and store it inBufferedImagein object.
  • Add text watermark

    • useGraphics2DThe object'sdrawString()Method to draw text watermarks on the image. We set the font, color (white with transparency) and position (center).
    • (new Color(255, 255, 255, 128))Set the text to white color with a transparency of 128.
  • Add image watermark

    • useGraphics2DThe object'sdrawImage()Method draws another image as a watermark to the lower right corner of the target image.
    • pass(AlphaComposite.SRC_OVER, 0.5f)Set transparency so that the watermark image has 50% transparency.
  • Save the image

    • use()Method saves the image after adding watermarks to PNG format.
  • Resource Management

    • ()releaseGraphics2DThe resource occupied by the object to prevent memory leakage.

Common watermark operations

  • Text watermark

    • You can adjust the font, color, size, transparency and other attributes to control the appearance of the watermark. Fonts can be usedFontClass settings, color usageColorClass settings.
    • The position of the text watermark can be determined by calculating the width of the image and the width of the text, so that it is centered or located at a specified position.
  • Picture watermark

    • usedrawImage()Method to draw another image as a watermark. The position of the watermark can be adjusted、size、Transparency, etc.。
    • Transparency of watermark pictures passAlphaCompositeClass settings.
  • Transparency control

    • For image watermarks, transparency control is very important. By settingAlphaCompositeThe value of the watermark image can make the watermark image more vague, thus not covering the details of the original image.
  • Watermark location

    • You can choose to center or set the position of the text watermark. Common locations for image watermarks include the lower right corner, the lower left corner, the upper right corner, etc. Flexible positioning can be achieved through coordinate calculation.

Project Summary

Through this project, we implement a simple Java program that can add text watermarks and image watermarks to images. useGraphics2DClass, we can flexibly control the appearance of the watermark, including location, transparency, color, etc., and through()Save the modified image.

Optimization direction

  • Custom watermark style: It can provide more customization options for text watermarks, such as font, size, color, transparency, etc.
  • Add batch watermarks: Can extend the program, support batch processing of multiple image files and add unified watermarks.
  • Graphic interface: You can add a graphical interface to the program, allowing users to select parameters such as watermark type (text or picture), location, transparency, etc. through the interface.

Through this project, you can master how to use Java to process image watermarking, which is suitable for copyright protection, brand promotion, batch image processing and other scenarios.

This is the article about adding watermarks to images and saving implementation methods for Java. For more related Java add watermarks to images and saving content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!