Worried about how to programmatically write text on an image using Java? In this article, we will learn to add text and image watermarks to images using Java. Previously, we have already seen the same using C# in one of the earlier posts.
There can be two ways to add watermark to images. Either you want to add the watermark with the personalized text or add an image watermark over the source image. We will see both scenarios.
Java Text and Image Watermarking API
In the examples below, we will be using GroupDocs.Watermark for Java API for adding text and image-based watermarking of JPG and PNG images. It will be better if you download the watermark API from the downloads section or integrate it into your Maven-based applications with the configurations mentioned on the same page.
Insert Text to Images as Watermark using Java
By following the below-mentioned steps and java code we can quickly add text to any image file as a watermark. I have watermarked the following JPG and PNG images using the same steps and below-mentioned code.
Currently, in addition to the shown JPG and PNG, this Java API also supports BMP, GIF, JP2, TIFF & WebP image formats for adding watermarks on it.
Steps to apply the watermark to any image file in Java
- Instantiate the TextWatermark object with the personalized text and style.
- Adjust the text watermark settings according to the image.
- Instantiate the Watermarker with the source image.
- Insert the watermark to the image by calling the add method.
- Get the output image using the save method.
Here is the Java source code that adds the text watermark to JPG image. If we need to apply the watermark to image other than JPG, no big change required. Just provide that image with the extension to the Watermarker and the save method. That’s it.
We can also change the style, orientation, and appearance of the watermark text.
Insert Image Watermark on Images using Java
Instead of adding text on an image, we can also add an image as a watermark on the source image. Follow the similar steps mentioned above but now you must use the ImageWatermark class instead of TextWatermark used earlier to add text over the JPG and PNG images.
This image is created using the below mentioned Java source code and shows how we can add a PNG image watermark over the source JPG image:
We have seen how to add text and image as a watermark on any image programmatically using Java. Furthermore, we change the text style and orientation of the watermark text.