The global market has become so open and vigilant in this fast-growing world. The security of your digital assets has become a serious concern. Hence, your business images need to have a mark of your ownership to avoid any possibility of theft or forgery. However, in this blog post, we will learn how to add watermark to images using Java imaging library. By the end of this guide, you will be able to build a watermark creator programmatically for your Java application.
The following sections will be covered:
Java imaging library installation
Adding a watermark to popular image formats programmatically is a matter of a few lines of source code. Therefore, you can download the API’s JAR file or install using the following configurations.
<repository> <id>AsposeJavaAPI</id> <name>Aspose Java API</name> <url>https://repository.aspose.com/repo/</url> </repository> <dependency> <groupId>com.aspose</groupId> <artifactId>aspose-imaging</artifactId> <version>20.12</version> <classifier>jdk16</classifier> </dependency>
Add watermark to images using Java
Now, let’s write a few lines of source code using methods exposed by this Java imaging library. There is a long list of methods available that you can see here.
Please follow the steps and the code snippet:
- Create an instance of an Image class and load an existing image.
- Create and initialize an instance of the Graphics class with the source image.
- Now, instantiate an instance of the Font class and set the font settings.
- Create an instance of SolidBrush class that is intended for drawing continuously with a specific color.
- Invoke the setColor method to set the brush color.
- Initialize an object of StringFormat class that encapsulates text layout information.
- Set text alignment information in the center of the vertical plane by calling the setAlignment method.
- Set formatting information by calling the setFormatFlags method.
- Draw a String using the drawString object and Font, at a specific Point.
- Call the save method to save the image with changes.
Copy & paste the following code into your main file:
You can see the output in the image below:
Get a Free License
You can avail a free temporary license to try the API without evaluation limitations.
Finally, you have learned how to add watermark to images using Java. This blog post will really help you if you are looking to develop a watermark creator for your business software. In addition, you may visit the documentation to know the advanced features of this Java imaging library.
Moreover, we suggest you follow our Getting Started guide.
Finally, conholdate.com is writing new blog posts. Therefore, please stay in touch for regular updates.
Ask a question
You can let us know about your questions or queries on our forum.
How do you create a watermark in Java?
Please follow this link to know the code snippet and the steps that demonstrate how to add watermark to images programmatically.