Watermark is a convenient way of protecting the content and claiming the copyrights. By applying a watermark to digital documents or images, you can avoid unauthorized usage or theft. In accordance with that, this article covers how to automate watermarking feature for images within Java applications. Particularly, you will learn how to add watermark to images using Java. This can be useful especially when you need to add a watermark to a batch of images.
- Java API to Add Watermark to Images
- Add Watermark to an Image in Java
- Add Diagonal Watermark to images in Java
- Get Free API License
Java API to Add Watermark to Images
In order to add a watermark to images, we’ll use Aspose.Imaging for Java. It is an image processing API that lets you manipulate a wide range of image formats from within your Java applications. You can either download the API’s JAR or install it within your Maven-based applications 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
The following are the steps to add a watermark to the images using Aspose.Imaging.
- Load an image file using the Image class.
- Create an object of the Graphics class and initialize it with Image object.
- Create and initialize Font and SolidBrush objects.
- Add watermark using Graphics.drawString(String s, Font font, Brush brush, float x, float y) method.
- Save the image using Image.save() method.
The following code sample shows how to add watermark to an image using Java.
Add Diagonal Watermark to images in Java
In certain cases, the watermark is applied to the images diagonally. For such cases, Aspose.Imaging for Java provides watermark transformation options using which you can rotate the watermark. The following are the steps to add a diagonal watermark to an image.
- Load an image file using the Image class.
- Create an object of the Graphics class and initialize it with Image object.
- Create and initialize Font and SolidBrush objects.
- Get image size in the Size object.
- Create a new Matrix object and set translation and transformation to the desired angle.
- Use Graphics.setTransform(Matrix) method to set transformation of the watermark.
- Add watermark using Graphics.drawString(String s, Font font, Brush brush, float x, float y) method.
- Save the image using Image.save() method.
The following code sample shows how to add a diagonal watermark to image using Java.
Get a Free API License
You can get a free temporary license in order to try the API without evaluation limitations.
Conclusion
Watermark is used to protect digital documents and images from unauthorized or illegal usage. In order to protect digital images, this article covered how to add a watermark to images using Java. Furthermore, you have seen how to apply the transformation to the watermarks. You can explore more about the Java image processing API using the documentation.