How to add watermark for images using Java
- Details
- Written by Nam Ha Minh
- Last Updated on 10 August 2019   |   Print Email
It’s interesting and fairly simple to add/embed a watermark over an image using Java graphics API. The steps are as follows:
- Read the source image file into a BufferedImage object using the ImageIO.read() method.
- Obtain graphics context of the BufferedImage object.
- Using the Graphics2D object to pain the watermark which can be a String, an image or whatever can be drawn with the Graphics2D’s API. But basically, the watermark is usually translucent so an alpha channel is needed.
- Write the output image using the ImageIO.write() method.
Using this technique, you can develop more complex programs that provide watermark-related functionalities such as adding watermarks in batch, embedding watermarks conditionally, etc.
In this tutorial, we introduce code examples for two typical scenarios: textual watermark and image watermark.
1. Java code example to add textual watermark for an image
The following utility method adds a textual watermark over an image. The code comments are self-explanatory:
/** * Embeds a textual watermark over a source image to produce * a watermarked one. * @param text The text to be embedded as watermark. * @param sourceImageFile The source image file. * @param destImageFile The output image file. */ static void addTextWatermark(String text, File sourceImageFile, File destImageFile) { try { BufferedImage sourceImage = ImageIO.read(sourceImageFile); Graphics2D g2d = (Graphics2D) sourceImage.getGraphics(); // initializes necessary graphic properties AlphaComposite alphaChannel = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.1f); g2d.setComposite(alphaChannel); g2d.setColor(Color.BLUE); g2d.setFont(new Font("Arial", Font.BOLD, 64)); FontMetrics fontMetrics = g2d.getFontMetrics(); Rectangle2D rect = fontMetrics.getStringBounds(text, g2d); // calculates the coordinate where the String is painted int centerX = (sourceImage.getWidth() - (int) rect.getWidth()) / 2; int centerY = sourceImage.getHeight() / 2; // paints the textual watermark g2d.drawString(text, centerX, centerY); ImageIO.write(sourceImage, "png", destImageFile); g2d.dispose(); System.out.println("The tex watermark is added to the image."); } catch (IOException ex) { System.err.println(ex); } }
Example usage:
File sourceImageFile = new File("e:/Test/Watermark/SwingEmailSender.png"); File destImageFile = new File("e:/Test/Watermark/text_watermarked.png"); addTextWatermark("CodeJava", sourceImageFile, destImageFile);
The sample source image:
The output image:
2. Java code example to add image watermark for an image
And the following utility method adds an image watermark over an image:
/** * Embeds an image watermark over a source image to produce * a watermarked one. * @param watermarkImageFile The image file used as the watermark. * @param sourceImageFile The source image file. * @param destImageFile The output image file. */ static void addImageWatermark(File watermarkImageFile, File sourceImageFile, File destImageFile) { try { BufferedImage sourceImage = ImageIO.read(sourceImageFile); BufferedImage watermarkImage = ImageIO.read(watermarkImageFile); // initializes necessary graphic properties Graphics2D g2d = (Graphics2D) sourceImage.getGraphics(); AlphaComposite alphaChannel = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.3f); g2d.setComposite(alphaChannel); // calculates the coordinate where the image is painted int topLeftX = (sourceImage.getWidth() - watermarkImage.getWidth()) / 2; int topLeftY = (sourceImage.getHeight() - watermarkImage.getHeight()) / 2; // paints the image watermark g2d.drawImage(watermarkImage, topLeftX, topLeftY, null); ImageIO.write(sourceImage, "png", destImageFile); g2d.dispose(); System.out.println("The image watermark is added to the image."); } catch (IOException ex) { System.err.println(ex); } }
Example usage:
File sourceImageFile = new File("e:/Test/Watermark/SwingEmailSender.png"); File watermarkImageFile = new File("e:/Test/Watermark/CodeJavaLogo.png"); File destImageFile = new File("e:/Test/Watermark/text_watermarked.png"); addImageWatermark(watermarkImageFile, sourceImageFile, destImageFile);
The sample source image:
The watermark image:
The output image:
Other Java Graphics Tutorials:
- How to resize images using Java
- How to convert image format using Java
- How to capture screenshot programmatically in Java
- How to draw text vertically with Java Graphics2D
- How to draw image with automatic scaling
- Creating Zoomable User Interfaces Programs with Piccolo2D Framework
- Drawing lines examples with Java Graphics2D
- Drawing Rectangles Examples with Java Graphics2D
- Using JFreechart to draw line chart with CategoryDataset
- Using JFreechart to draw XY line chart with XYDataset
Comments
Maybe you need to use image recognition and machine learning.