Adding watermarks to images is a common requirement when you need to protect visual assets or brand content. Aspose.Imaging Cloud SDK for Java provides a powerful library that simplifies watermark operations on the server side. In this guide you will learn how to add text and image Watermarks to photos in Java with Aspose.Imaging cloud sdk, covering setup, code walkthrough, a complete example, and equivalent REST calls.

Setting Up Aspose.Imaging Cloud SDK for Java

Before you start, make sure you have the following:

  • Java 8 or higher installed.
  • Maven or Gradle for dependency management.
  • An Aspose Cloud account with clientId and clientSecret.

Add the SDK to your Maven project:

<dependency>
    <groupId>com.aspose</groupId>
    <artifactId>aspose-imaging-cloud</artifactId>
    <version>25.4</version>
</dependency>

You can also download the library directly from the release page. After adding the dependency, obtain your cloud credentials from the Aspose Cloud console and keep them handy for the next steps.

Adding Text and Image Watermarks to Photos in Java with Aspose.Imaging Cloud SDK

The following steps demonstrate how to add text and image Watermarks to photos in Java with Aspose.Imaging cloud sdk using the ImagingApi class. Each step extracts a small portion of the full program for clarity.

Step 1: Initialize the Imaging API Client

Create an instance of ImagingApi with your client credentials.

String clientId = "clientId";
String clientSecret = "clientSecret";
ImagingApi api = new ImagingApi(clientId, clientSecret);

The ImagingApi class is documented in the API reference.

Step 2: Add a Text Watermark

Specify the watermark text, font, color, and other options, then call putImageWatermarkText.

String inputImage = "input.jpg";
String outputTextWatermarked = "output_text_watermark.jpg";
String watermarkText = "Sample Watermark";
String fontFamily = "Arial";
Integer fontSize = 24;
String fontStyle = "Bold";
String textColor = "FF0000";
Double transparency = 0.5;
Double rotationAngle = 0.0;
Boolean background = false;
Boolean border = false;

byte[] textResult = api.putImageWatermarkText(
        inputImage,
        null,
        null,
        null,
        outputTextWatermarked,
        watermarkText,
        fontFamily,
        fontSize,
        fontStyle,
        textColor,
        transparency,
        rotationAngle,
        background,
        border
);
Files.write(Paths.get(outputTextWatermarked), textResult);

Step 3: Add an Image Watermark

Provide the watermark image and configure its transparency and scaling.

String watermarkImage = "logo.png";
String outputImageWatermarked = "output_image_watermark.jpg";
Double imgTransparency = 0.3;
Double imgRotationAngle = 0.0;
Boolean scale = true;
Boolean imgBackground = false;
Boolean imgBorder = false;

byte[] imageResult = api.putImageWatermarkImage(
        inputImage,
        null,
        null,
        null,
        outputImageWatermarked,
        watermarkImage,
        imgTransparency,
        imgRotationAngle,
        scale,
        imgBackground,
        imgBorder
);
Files.write(Paths.get(outputImageWatermarked), imageResult);

Step 4: Verify the Output

After the two calls, you will have output_text_watermark.jpg and output_image_watermark.jpg saved locally. These files contain the applied watermarks and can be used directly in your application.

Complete Code Example: Watermarking Photos with Aspose.Imaging Cloud SDK

The following code demonstrates the complete workflow from initialization to saving both watermarked images.

import com.aspose.imaging.cloud.sdk.api.ImagingApi;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

public class AddWatermarkExample {
    public static void main(String[] args) {
        String clientId = "clientId";
        String clientSecret = "clientSecret";
        ImagingApi api = new ImagingApi(clientId, clientSecret);
        try {
            // Add text watermark
            String inputImage = "input.jpg";
            String outputTextWatermarked = "output_text_watermark.jpg";
            String watermarkText = "Sample Watermark";
            String fontFamily = "Arial";
            Integer fontSize = 24;
            String fontStyle = "Bold";
            String textColor = "FF0000";
            Double transparency = 0.5;
            Double rotationAngle = 0.0;
            Boolean background = false;
            Boolean border = false;

            byte[] textResult = api.putImageWatermarkText(
                    inputImage,
                    null,
                    null,
                    null,
                    outputTextWatermarked,
                    watermarkText,
                    fontFamily,
                    fontSize,
                    fontStyle,
                    textColor,
                    transparency,
                    rotationAngle,
                    background,
                    border
            );
            Files.write(Paths.get(outputTextWatermarked), textResult);

            // Add image watermark
            String watermarkImage = "logo.png";
            String outputImageWatermarked = "output_image_watermark.jpg";
            Double imgTransparency = 0.3;
            Double imgRotationAngle = 0.0;
            Boolean scale = true;
            Boolean imgBackground = false;
            Boolean imgBorder = false;

            byte[] imageResult = api.putImageWatermarkImage(
                    inputImage,
                    null,
                    null,
                    null,
                    outputImageWatermarked,
                    watermarkImage,
                    imgTransparency,
                    imgRotationAngle,
                    scale,
                    imgBackground,
                    imgBorder
            );
            Files.write(Paths.get(outputImageWatermarked), imageResult);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // No explicit resource cleanup required for ImagingApi
        }
    }
}

Note: This code example demonstrates the core functionality. Before using it in your project, make sure to update any file paths and configuration values to match your actual environment, verify that all required dependencies are properly installed, and test thoroughly in your development environment. If you encounter any issues, please refer to the official documentation or reach out to the support team for assistance.

Watermark Operations via REST API Using cURL

If you prefer a pure REST approach, the same operations can be performed with cURL commands.

1. Authenticate and Get an Access Token

curl -X POST "https://api.aspose.cloud/connect/token" \
     -H "Content-Type: application/x-www-form-urlencoded" \
     -d "grant_type=client_credentials&client_id=YOUR_CLIENT_ID&client_secret=YOUR_CLIENT_SECRET"

The response contains an access_token you will use in subsequent calls.

2. Upload the Source Image

curl -X PUT "https://api.aspose.cloud/v3.0/imaging/input.jpg" \
     -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
     -H "Content-Type: image/jpeg" \
     --data-binary "@input.jpg"

3. Apply a Text Watermark

curl -X POST "https://api.aspose.cloud/v3.0/imaging/input.jpg/watermark/text" \
     -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
     -H "Content-Type: application/json" \
     -d '{
           "text": "Sample Watermark",
           "fontFamily": "Arial",
           "fontSize": 24,
           "fontStyle": "Bold",
           "color": "FF0000",
           "transparency": 0.5,
           "rotationAngle": 0,
           "background": false,
           "border": false,
           "outputPath": "output_text_watermark.jpg"
         }'

4. Apply an Image Watermark

curl -X POST "https://api.aspose.cloud/v3.0/imaging/input.jpg/watermark/image" \
     -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
     -H "Content-Type: application/json" \
     -d '{
           "imagePath": "logo.png",
           "transparency": 0.3,
           "rotationAngle": 0,
           "scale": true,
           "background": false,
           "border": false,
           "outputPath": "output_image_watermark.jpg"
         }'

5. Download the Watermarked Files

curl -X GET "https://api.aspose.cloud/v3.0/imaging/output_text_watermark.jpg" \
     -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
     -o "output_text_watermark.jpg"

curl -X GET "https://api.aspose.cloud/v3.0/imaging/output_image_watermark.jpg" \
     -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
     -o "output_image_watermark.jpg"

For more details on request bodies and additional parameters, see the official API documentation.

Key Takeaways

In this article we showed how to add text and image Watermarks to photos in Java with Aspose.Imaging cloud sdk using both the Java library and direct REST calls. The SDK handles authentication, file upload, and watermark rendering with just a few method calls, while the cURL examples give you flexibility for scripting or integration with other platforms. Remember to secure your client credentials and obtain a proper license for production use; you can acquire a temporary license from the temporary license page. With the code and API patterns in hand, you can now protect your images across any Java‑based backend.

FAQs

  • How can I add text and image Watermarks to photos in Java with Aspose.Imaging cloud sdk?
    Use the putImageWatermarkText and putImageWatermarkImage methods of the ImagingApi class provided by Aspose.Imaging Cloud SDK for Java. The methods accept parameters for text, font, color, transparency, rotation, and more.

  • Do I need to upload the source image before applying a watermark via the API?
    Yes. The image must be stored in Aspose Cloud storage first. You can upload it with a simple PUT request as shown in the cURL section.

  • Can I change the watermark opacity and rotation?
    Both the text and image watermark methods include transparency and rotationAngle parameters, giving you full control over the visual appearance.

  • Where can I find more examples and detailed parameter descriptions?
    The official documentation provides extensive guides, and the API reference lists all available methods and their signatures.

Further Reading