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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With