Add Watermark to Images using Java

The watermark is a superimposed image, logo, pattern, or text placed over a photograph or image. It can be used to identify the image’s creator. You can add a watermark to any image programmatically on the cloud. This article will be focusing on how to add a watermark to images using a REST API in Java.

The following topics shall be covered in this article:

Watermark REST API and Java SDK

For watermarking an image, I will be using the Java SDK of GroupDocs.Watermark Cloud API. It allows you to programmatically add, remove, search and replace watermarks from images and documents of supported formats such as PDF, Microsoft Word, and Powerpoint. Currently, it also provides .NET SDK as well for the Cloud API.

You can easily use GroupDocs.Watermark Cloud in your Maven-based Java applications by adding the following pom.xml configuration.

<repository>
    <id>groupdocs-artifact-repository</id>
    <name>GroupDocs Artifact Repository</name>
    <url>https://repository.groupdocs.cloud/repo</url>
</repository>
<dependency>
	<groupId>com.groupdocs</groupId>
	<artifactId>groupdocs-watermark-cloud</artifactId>
	<version>19.12</version>
	<packaging>jar</packaging>
</dependency>

Please get your Client ID and Client Secret from the dashboard before you start following the steps and available code examples. Add your ID and Secret in the code as demonstrated below:

Add Text Watermark to Images using a REST API

You can add text watermark to photos or image files by following the simple steps mentioned below:

Upload the JPG Image

Firstly, upload the JPG image file to the Cloud using the code example given below:

As the result, the JPG file will be uploaded to Cloud Storage and will be available in the files section of your dashboard.

Add Text Watermark to JPG Image using Java

You can add a text watermark to the JPEG image programmatically by following the steps given below.

  • Create an instance of WatermarkApi
  • Set the JPEG image file path in the FileInfo model
  • Define WatermarkOptions and set FileInfo
  • Define TextWatermarkOptions
  • Set Text, Font Family, Font Size, and the Text Alignment
  • Set watermark text Foreground color
  • Define watermark Position
  • Define WatermarkDetails and set TextWatermarkOptions and Position
  • Set WatermarkDetails to List
  • Create AddRequest with WatermarkOptions
  • Get results by calling the WatermarkApi.add() method

The following code sample shows how to add text as a watermark to an image using a REST API.

Add text watermark to image

Add text watermark to image

Download the Updated Image

The above code samples will save the watermarked image file on the cloud. You can download it using the following code sample:

Add Image Watermark to Images using REST API

You can add an image or logo watermark to the JPEG image programmatically by following the steps given below.

  • Create an instance of WatermarkApi
  • Set the JPEG image file path in the FileInfo model
  • Define WatermarkOptions and set FileInfo
  • Define ImageWatermarkOptions
  • Set FilePath of a PNG image to watermark with
  • Define watermark Position
  • Define WatermarkDetails and set ImageWatermarkOptions and Position
  • Set WatermarkDetails to List
  • Create AddRequest with WatermarkOptions
  • Get results by calling the WatermarkApi.add() method

The following code sample shows how to add an image as a watermark to a JPEG image using a REST API. Please follow the steps mentioned earlier to upload and download the files.

Add image watermark to image

Add image watermark to image

Try Online

Please try the following free online Watermark tool, which is developed using the above API. https://products.groupdocs.app/watermark/jpeg

Conclusion

In conclusion, you have learned how to add text or image watermark to a JPEG image on the cloud. You also learned how to programmatically upload the image files on the cloud and then download them from the cloud. You can learn more about GroupDocs.Watermark Cloud API from the documentation. We also provide an API Reference section that lets you visualize and interact with our APIs directly through the browser. In case of any ambiguity, feel free to contact support.

See Also