How can we add watermarks to images in Java?

You can add a watermark to an image in Java by following these steps:

  1. Import the relevant libraries:
import java.awt.AlphaComposite;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
  1. Load the original image and the watermark image.
BufferedImage originalImage = ImageIO.read(new File("original.jpg"));
BufferedImage watermarkImage = ImageIO.read(new File("watermark.png"));
  1. Create a new image object with the same size as the original image.
BufferedImage combinedImage = new BufferedImage(originalImage.getWidth(), originalImage.getHeight(), BufferedImage.TYPE_INT_RGB);
  1. Create a graphic object and set the anti-aliasing and rendering mode.
Graphics2D graphics = (Graphics2D) combinedImage.getGraphics();
graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
graphics.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
  1. Draw the original image onto a new image object.
graphics.drawImage(originalImage, 0, 0, null);
  1. Adjust the transparency and placement of the watermark image, and then draw it onto a new image object.
AlphaComposite alphaComposite = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f); // 设置透明度为0.5
graphics.setComposite(alphaComposite);

int x = combinedImage.getWidth() - watermarkImage.getWidth() - 10; // 水印图片的x坐标
int y = combinedImage.getHeight() - watermarkImage.getHeight() - 10; // 水印图片的y坐标
graphics.drawImage(watermarkImage, x, y, null);
  1. Insert watermark text:
graphics.setFont(new Font("Arial", Font.BOLD, 12));
graphics.drawString("watermark", 10, 20);
  1. Save the new image object as a file.
ImageIO.write(combinedImage, "JPEG", new File("result.jpg"));

Here is the complete sample code:

import java.awt.AlphaComposite;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;

public class ImageWatermarkExample {
    public static void main(String[] args) {
        try {
            BufferedImage originalImage = ImageIO.read(new File("original.jpg"));
            BufferedImage watermarkImage = ImageIO.read(new File("watermark.png"));

            BufferedImage combinedImage = new BufferedImage(originalImage.getWidth(), originalImage.getHeight(), BufferedImage.TYPE_INT_RGB);

            Graphics2D graphics = (Graphics2D) combinedImage.getGraphics();
            graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            graphics.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);

            graphics.drawImage(originalImage, 0, 0, null);

            AlphaComposite alphaComposite = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f);
            graphics.setComposite(alphaComposite);

            int x = combinedImage.getWidth() - watermarkImage.getWidth() - 10;
            int y = combinedImage.getHeight() - watermarkImage.getHeight() - 10;
            graphics.drawImage(watermarkImage, x, y, null);

            graphics.setFont(new Font("Arial", Font.BOLD, 12));
            graphics.drawString("watermark", 10, 20);

            ImageIO.write(combinedImage, "JPEG", new File("result.jpg"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

The code above combines the original image and the watermark image into a new image, saving it as result.jpg. Here, original.jpg is the original image, watermark.png is the watermark image, and “watermark” is the watermark text. You can adjust the file paths and watermark position, opacity, and other parameters as needed.

bannerAds