Java Image Watermarking for Brand Protection

Java’s powerful image watermarking technique protects images and ensures proper attribution. Watermarking can be seamlessly integrated into applications for content management systems, e-commerce platforms, Jasper reports, and social media apps.

Watermarking deters intellectual property theft and helps maintain brand integrity. By consistently applying watermarks to images, the developer reinforces its brand identity and prevents misrepresenting its visual assets. The developer can also identify which user generated the report.

Understanding the Java ImageIO library.

Java provides a powerful built-in library called ImageIO (Image I/O) that simplifies the process of reading, writing, and manipulating image data. This library is essential for implementing image watermarking functionality in Java applications.

The ImageIO library supports various image formats, including JPEG, PNG, GIF, BMP, and WBMP. It offers a consistent and flexible API for working with image data, making it easier to develop cross-platform and cross-format solutions.

One key advantage of using the ImageIO library is its ability to read and write metadata associated with an image. This metadata can be leveraged to embed invisible watermarks or store additional information about the image, such as copyright notices or ownership details.

Steps to add a watermark to an image using Java.

Adding a watermark to an image in Java involves several steps, and the process may vary slightly depending on the type of watermark the developer chooses to implement. Here’s a general outline of the steps involved:

Sample original image.

Image by svstudioart on Freepik

Create a watermark of dynamic text on an Image.

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

public static void watermarkImage() {
        try {
            // Load the original image
            File originalImageFile = new File("D:/workspaces/image/Set_of_programmers_02_02.jpg");
            BufferedImage image = ImageIO.read(originalImageFile);

            // Create a Graphics2D object
            Graphics2D g2d = (Graphics2D) image.getGraphics();

            // Set the watermark text
            String watermarkText = "Dynamic Text";

            // Set the font and color
            Font font = new Font("Arial", Font.BOLD, 400);
            g2d.setFont(font);
            g2d.setColor(new Color(0, 0, 0, 128)); // White color with 50% transparency

            // Get the text's position
            FontMetrics fontMetrics = g2d.getFontMetrics();
            int x = image.getWidth() - fontMetrics.stringWidth(watermarkText) - 10;
            int y = image.getHeight() - fontMetrics.getHeight() + fontMetrics.getAscent() - 10;

            // Draw the watermark text
            g2d.drawString(watermarkText, x, y);

            // Dispose of the Graphics2D object
            g2d.dispose();

            // Save the watermarked image to a new file
            String path = "D:/workspaces/image/watermarked_rotated_image_" + System.currentTimeMillis() + ".png";
            ImageIO.write(image, "jpg", new File(path));

            System.out.println("Watermark added successfully.");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

Customizing watermarks in Java.

While the basic steps outlined in the previous section provide a foundation for watermarking images, Java offers many customization options to tailor the watermarking process to the developer’s needs. Here are some ways the developer can customize watermarks in Java:

1. Watermark Positioning

The developer can control the position of the watermark on the image by adjusting the coordinates at which the watermark is drawn or overlaid. This allows the developer to place the watermark in a specific location, such as the center, corner, or along the edges of the image.

public static  void watermarkCenter(){
        try {
            // Load the original image
            File originalImageFile = new File("D:/workspaces/image/Set_of_programmers_02_02.jpg");
            BufferedImage image = ImageIO.read(originalImageFile);

            // Create a Graphics2D object
            Graphics2D g2d = (Graphics2D) image.getGraphics();

            // Set the watermark text
            String watermarkText = "Watermark";

            // Set the font and color
            Font font = new Font("Arial", Font.BOLD, 400);
            g2d.setFont(font);
            g2d.setColor(new Color(255, 255, 255, 128)); // White color with 50% transparency

            // Calculate the center position of the image
            FontMetrics fontMetrics = g2d.getFontMetrics();
            int textWidth = fontMetrics.stringWidth(watermarkText);
            int textHeight = fontMetrics.getHeight();
            int x = (image.getWidth() - textWidth) / 2;
            int y = (image.getHeight() - textHeight) / 2 + fontMetrics.getAscent();

            // Draw the watermark text at the center of the image
            g2d.drawString(watermarkText, x, y);

            // Dispose of the Graphics2D object
            g2d.dispose();

            // Save the watermarked image to a new file
            String path = "D:/workspaces/image/watermarked_rotated_image_" + System.currentTimeMillis() + ".png";
            ImageIO.write(image, "jpg", new File(path));

            System.out.println("Centered watermark added successfully.");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

2. Watermark Rotation

The developer can rotate the watermark image or text to create a unique and visually appealing effect. This can help prevent the watermark from appearing too repetitive or predictable across multiple photos.

public static void rotateTextWatermark(){
        try {
            // Load the original image
            File originalImageFile = new File("D:/workspaces/image/Set_of_programmers_02_02.jpg");
            BufferedImage image = ImageIO.read(originalImageFile);

            // Create a Graphics2D object
            Graphics2D g2d = (Graphics2D) image.getGraphics();

            // Set the watermark text
            String watermarkText = "Dynamic Text";

            // Set the font and color
            Font font = new Font("Arial", Font.BOLD, 200);
            g2d.setFont(font);
            g2d.setColor(new Color(255, 255, 255, 128)); // White color with 50% transparency

            // Calculate the position where the text will be placed
            FontMetrics fontMetrics = g2d.getFontMetrics();
            int x = image.getWidth() / 2;
            int y = image.getHeight() / 2;

            // Rotate the text by a specified angle (in radians)
            double angle = Math.toRadians(-45); // Rotate 45 degrees counterclockwise

            // Apply the rotation transformation to the Graphics2D context
            g2d.rotate(angle, x, y);

            // Draw the rotated watermark text
            g2d.drawString(watermarkText, x - fontMetrics.stringWidth(watermarkText) / 2, y);

            // Dispose of the Graphics2D object
            g2d.dispose();

            // Save the watermarked image to a new file
            String path = "D:/workspaces/image/watermarked_rotated_image_" + System.currentTimeMillis() + ".png";
            ImageIO.write(image, "jpg", new File(path));

            System.out.println("Rotated watermark added successfully.");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

3. Watermark Scaling

Depending on the size of the original image, the developer may need to scale the watermark up or down to maintain an appropriate size and visibility.

Font font = new Font("Arial", Font.BOLD, 40); //Font size

4. Watermark Tiling

Instead of a single watermark instance, the developer can tile or repeat the watermark across the entire image. This can create a more prominent and difficult-to-remove watermark pattern.

public static void watermarkTiling(){
        try {
            // Load the original image
            File originalImageFile = new File("D:/workspaces/image/Set_of_programmers_02_02.jpg");
            BufferedImage image = ImageIO.read(originalImageFile);

            // Create a Graphics2D object
            Graphics2D g2d = (Graphics2D) image.getGraphics();

            // Set the watermark text
            String watermarkText = "Watermark";

            // Set the font and color
            Font font = new Font("Arial", Font.BOLD, 40);
            g2d.setFont(font);
            g2d.setColor(new Color(255, 255, 255, 128)); // White color with 50% transparency

            // Calculate the width and height of the text
            FontMetrics fontMetrics = g2d.getFontMetrics();
            int textWidth = fontMetrics.stringWidth(watermarkText);
            int textHeight = fontMetrics.getHeight();

            // Set the angle of rotation (optional)
            double angle = Math.toRadians(-45);

            // Loop through the image and draw the watermark text
            for (int y = 0; y < image.getHeight(); y += textHeight + 20) {
                for (int x = 0; x < image.getWidth(); x += textWidth + 20) {
                    g2d.rotate(angle, x + textWidth / 2, y + textHeight / 2);
                    g2d.drawString(watermarkText, x, y + fontMetrics.getAscent());
                    g2d.rotate(-angle, x + textWidth / 2, y + textHeight / 2); // Reset rotation
                }
            }

            // Dispose of the Graphics2D object
            g2d.dispose();

            // Save the watermarked image to a new file
            String path = "D:/workspaces/image/watermarked_rotated_image_" + System.currentTimeMillis() + ".png";
            ImageIO.write(image, "jpg", new File(path));

            System.out.println("Tiled watermark added successfully.");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

5. Watermark Opacity

The developer can adjust the opacity or transparency level of visible watermarks. This can be useful for creating subtle watermarks that don’t significantly obscure the underlying image content.

g2d.setColor(new Color(255, 255, 255, 128)); // White color with 50% transparency

6. Watermark custom font

The developer wants to change fonts to identify the watermark text. The developer can use the developer’s fonts.

File fontFile = new File("D:\\workspace\\resources\\fonts\\" + "THSarabunNew.ttf"); // Replace with your font file path
// Create Font object from the font file
Font font = Font.createFont(Font.TRUETYPE_FONT, fontFile);
font = font.deriveFont(Font.PLAIN, 24f);
g2d.setFont(font);

By leveraging these customization options, the developer can create watermarks tailored to the developer’s specific branding, security, or artistic requirements, ensuring a unique and effective watermarking solution for the developer’s Java applications.

Conclusion

Image watermarking is a powerful technique for protecting intellectual property rights, maintaining brand integrity, and deterring unauthorized use of visual content. Java provides robust tools and libraries, such as ImageIO, enabling developers to integrate watermarking functionality into their applications seamlessly.

Finally

In this guide, developers explored the importance of image watermarking and the steps involved in adding watermarks to images using Java. Developers also delved into customization options for watermarking implementations.

Leave a Comment

Your email address will not be published. Required fields are marked *