Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I watermark an image in Java?

How can I create a watermark over an image using Java? I need user-entered text to be added to a provided position over an image. Any sample code/suggestions will help.

like image 209
Pit Digger Avatar asked Mar 28 '11 13:03

Pit Digger


People also ask

How do I add a watermark to a PNG?

Press and hold down the ALT key, then type the numbers 0169 on your number keypad. Release ALT, and the copyright symbol should appear. Add text to the copyright symbol if you want the copyright watermark to include more information.

Can you display images in Java?

Images can be from a static source, such as a JPEG file, or a dynamic one, such as a video stream or a graphics engine. AWT Images are created with the getImage() and createImage() methods of the java. awt. Toolkit class.


2 Answers

In Thumbnailator, one can add a text caption to an existing image by using the Caption image filter:

// Image to add a text caption to.
BufferedImage originalImage = ...;

// Set up the caption properties
String caption = "Hello World";
Font font = new Font("Monospaced", Font.PLAIN, 14);
Color c = Color.black;
Position position = Positions.CENTER;
int insetPixels = 0;

// Apply caption to the image
Caption filter = new Caption(caption, font, c, position, insetPixels);
BufferedImage captionedImage = filter.apply(originalImage);

In the above code, the text Hello World will be drawn on centered on the originalImage with a Monospaced font, with a black foreground color, at 14 pt.

Alternatively, if a watermark image is to be applied to an existing image, one can use the Watermark image filter:

BufferedImage originalImage = ...;
BufferedImage watermarkImage = ...;

Watermark filter = new Watermark(Positions.CENTER, watermarkImage, 0.5f);
BufferedImage watermarkedImage = filter.apply(originalImage);

The above code will superimpose the watermarkImage on top of the originalImage, centered with an opacity of 50%.

Thumbnailator will run on plain old Java SE -- one does not have to install any third party libraries. (However, using the Sun Java runtime is required.)

Full disclosure: I am the developer for Thumbnailator.

like image 96
coobird Avatar answered Sep 21 '22 06:09

coobird


You can have a look to the "Drawing the Watermark" section of http://web.archive.org/web/20080324030029/http://blog.codebeach.com/2008/02/watermarking-images-in-java-servlet.html

Or you may use the GIF4J library http://www.gif4j.com/java-gif4j-pro-gif-image-watermark.htm#gifimagewatermarkapply

like image 23
Wavyx Avatar answered Sep 21 '22 06:09

Wavyx