Adding Text or Image as Watermark to an Image in Java
The following tutorial demonstrates how to add a watermark to an image in java. We show you two examples. The first example adds a text watermark to an image. The second adds an image watermark to an image.
We distinguish
PNG
images fromJPG
images, becausePNG
images support transparency. In case ofPNG
images we set the image type toBufferedImage.TYPE_INT_ARGB
. All other images we set the image type toBufferedImage.TYPE_INT_RGB
.
Adding Text as Watermark to an Image
The following example adds a text watermark to an image.
package com.memorynotfound.image;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class AddTextWatermark {
public static void main(String... args) throws IOException {
// overlay settings
String text = "\u00a9 memorynotfound.com";
File input = new File("/tmp/duke.jpg");
File output = new File("/tmp/duke-text-watermarked.jpg");
// adding text as overlay to an image
addTextWatermark(text, "jpg", input, output);
}
private static void addTextWatermark(String text, String type, File source, File destination) throws IOException {
BufferedImage image = ImageIO.read(source);
// determine image type and handle correct transparency
int imageType = "png".equalsIgnoreCase(type) ? BufferedImage.TYPE_INT_ARGB : BufferedImage.TYPE_INT_RGB;
BufferedImage watermarked = new BufferedImage(image.getWidth(), image.getHeight(), imageType);
// initializes necessary graphic properties
Graphics2D w = (Graphics2D) watermarked.getGraphics();
w.drawImage(image, 0, 0, null);
AlphaComposite alphaChannel = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.4f);
w.setComposite(alphaChannel);
w.setColor(Color.GRAY);
w.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 26));
FontMetrics fontMetrics = w.getFontMetrics();
Rectangle2D rect = fontMetrics.getStringBounds(text, w);
// calculate center of the image
int centerX = (image.getWidth() - (int) rect.getWidth()) / 2;
int centerY = image.getHeight() / 2;
// add text overlay to the image
w.drawString(text, centerX, centerY);
ImageIO.write(watermarked, type, destination);
w.dispose();
}
}
Text Watermark Result
To demonstrate the result, we added the following table.
duke.png (original) | duke-text-watermarked.png | duke-text-watermarked.jpg |
---|---|---|
Size: 9KB | Size: 13KB | Size: 16KB |
Adding an Image Watermark to an Image
The following example adds an image watermark to an image.
package com.memorynotfound.image;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class AddImageWatermark {
public static void main(String... args) throws IOException {
// overlay settings
File input = new File("/tmp/duke.png");
File overlay = new File("/tmp/watermark.png");
File output = new File("/tmp/duke-image-watermarked.png");
// adding text as overlay to an image
addImageWatermark(overlay, "png", input, output);
}
private static void addImageWatermark(File watermark, String type, File source, File destination) throws IOException {
BufferedImage image = ImageIO.read(source);
BufferedImage overlay = resize(ImageIO.read(watermark), 150, 150);
// determine image type and handle correct transparency
int imageType = "png".equalsIgnoreCase(type) ? BufferedImage.TYPE_INT_ARGB : BufferedImage.TYPE_INT_RGB;
BufferedImage watermarked = new BufferedImage(image.getWidth(), image.getHeight(), imageType);
// initializes necessary graphic properties
Graphics2D w = (Graphics2D) watermarked.getGraphics();
w.drawImage(image, 0, 0, null);
AlphaComposite alphaChannel = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.4f);
w.setComposite(alphaChannel);
// calculates the coordinate where the String is painted
int centerX = image.getWidth() / 2;
int centerY = image.getHeight() / 2;
// add text watermark to the image
w.drawImage(overlay, centerX, centerY, null);
ImageIO.write(watermarked, type, destination);
w.dispose();
}
private static BufferedImage resize(BufferedImage img, int height, int width) {
Image tmp = img.getScaledInstance(width, height, Image.SCALE_SMOOTH);
BufferedImage resized = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = resized.createGraphics();
g2d.drawImage(tmp, 0, 0, null);
g2d.dispose();
return resized;
}
}
Image Watermark Result
To demonstrate the result, we added the following table.
duke.png (original) | duke-image-watermarked.png | duke-image-watermarked.jpg |
---|---|---|
Size: 9KB | Size: 26KB | Size: 13KB |
Thanx a lot!
Is there a way to do the text based watermark, but tiled? (all over the image)