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 in Java. This can be useful especially when you need to add a watermark to a batch of images including PNG, JPG, and BMP.
- Java API to Add Watermark to Images
- Steps to Add Watermark in Images in Java
- Add Watermark to an Image in Java
- Add Diagonal Watermark to images in Java
- Get Free API License
Add Watermark to Images in Java - API Installation
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>
How to Add Watermark in Images in Java
Aspose.Imaging for Java has made the watermarking of images so easy that can be done within a few lines of code. The following are the steps required to add watermark in an image in Java.
- Load the image file from disk.
- Create a watermark and set its properties.
- Apply watermark to the image.
- Save the image to desired location.
Let’s now write the code to watermark an image in Java.
Add Watermark to Images in 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 in Java.
Insert Diagonal Watermark to an Image
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 in Java.
Watermark Images in Java with a Free License
You can get a free temporary license and insert watermark to images 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.