This is my second article about getting started

preface

Sometimes we need to watermark images uploaded by users, like the “Nuggets Community” in the bottom right corner of all uploads.

This article describes how to add text and image watermarks to images in Java

1. Analysis of ideas

To add a watermark to an image is actually to superimpose the watermark content on the original image, so how to achieve this in Java?

There are two ways to do this:

  1. Use JDK built-inGraphics2D: To read the original image from the streamBufferedImageAnd then useBufferedImagecreateGraphics2D drawing objectAnd then use the API in the drawing object to perform the corresponding operation
  2. Use third-party Jar packagesThumbnailator: Using third-party Jar packages is relatively simple, inThumbnailatorThe corresponding API is already available, just read the official documentation can be implemented

Thumbnailator’s official Github site allows you to view the Thumbnailator version and official documentation

Second, implementation method

Text watermarking

For text watermarking, it’s convenient to use the Graphics2D that comes with the JDK.

Graphics2D implementation

The code is as follows:

    public void addTextWaterMark(BufferedImage targetImg, Color textColor, int fontSize, String text, String outPath) {
        try {
            int width = targetImg.getWidth(); / / picture width
            int height = targetImg.getHeight(); / / picture
            BufferedImage bufferedImage = new BufferedImage(width,height,BufferedImage.TYPE_INT_BGR);
            Graphics2D g = bufferedImage.createGraphics();
            g.drawImage(targetImg, 0.0, width, height, null);
            g.setColor(textColor); // Watermark color
            g.setFont(new Font(Microsoft Yahei, Font.ITALIC, fontSize));
            // The watermark content is placed in the lower right corner
            int x = width - (text.length() + 1) * fontSize;
            int y = height- fontSize * 2;
            g.drawString(text, x, y);
            FileOutputStream outImgStream = new FileOutputStream(outPath);
            ImageIO.write(bufferedImage, "jpg", outImgStream);
            outImgStream.flush();
            outImgStream.close();
            g.dispose();
        } catch(Exception e) { e.printStackTrace(); }}@Test
    public void addTextTest(a) throws IOException {
        File file = new File("D:\\12.jpg");
        BufferedImage image = ImageIO.read(file);
        addTextWaterMark(image, Color.RED, 80."Test text watermarking"."D:\\13.jpg");
    }
Copy the code

The results of

The original picture is as follows:

Image after adding watermark (lower right corner) :

Image watermarking

As for image watermarks, it’s a little easier to use Thumbnailator, but here you have both.

Graphics2D implementation

    public void addImageWaterMark(BufferedImage targetImg, BufferedImage waterImg, String outPath) {
        try {
            int width = targetImg.getWidth(); / / picture width
            int height = targetImg.getHeight(); / / picture
            BufferedImage bufferedImage = new BufferedImage(width,height,BufferedImage.TYPE_INT_BGR);
            Graphics2D g = bufferedImage.createGraphics();
            g.drawImage(targetImg, 0.0, width, height, null);
            // The watermark content is placed in the lower right corner
            int x = width - waterImg.getWidth();
            int y = height- waterImg.getHeight();
            g.drawImage(waterImg, x, y, waterImg.getWidth(), waterImg.getHeight(), null);
            FileOutputStream outImgStream = new FileOutputStream(outPath);
            ImageIO.write(bufferedImage, "jpg", outImgStream);
            outImgStream.flush();
            outImgStream.close();
            g.dispose();
        } catch(Exception e) { e.printStackTrace(); }}@Test
    public void addImg1Test(a) throws IOException {
        BufferedImage image = ImageIO.read(new File("D:\\12.jpg"));
        BufferedImage waterImage = ImageIO.read(new File("D:\\mark.png"));
        addImageWaterMark(image, waterImage, "D:\\14.jpg");
    }
Copy the code

Thumbnailator implementation

    public void addImgWaterMark(BufferedImage targetImg, BufferedImage waterImg, String outPath) {
        try {
            Thumbnails.of(targetImg)
                    .size(targetImg.getWidth(), targetImg.getHeight()) / / size
                    .watermark(Positions.BOTTOM_RIGHT, waterImg, 1f)  // 0.5f indicates transparency. The maximum value is 1
                    .outputQuality(1)   // Image quality, Max is 1
                    .toFile(new File(outPath));
        } catch(IOException e) { e.printStackTrace(); }}@Test
    public void addImg2Test(a) throws IOException {
        BufferedImage image = ImageIO.read(new File("D:\\12.jpg"));
        BufferedImage waterImage = ImageIO.read(new File("D:\\mark.png"));
        addImgWaterMark(image, waterImage, "D:\\15.jpg");
    }
Copy the code

The results of

The original image and image watermark are as follows:

After adding image watermarking:

Third, summary

As you can see from the above, the Thumbnailator utility class is easy to use. The so-called predecessors plant trees and descendants enjoy the shade!

This is my second article, if there is any help to you, we will be honored! 😊