Watermarking is a convenient way to protect content and assert copyright. By adding watermarks to digital documents or images, unauthorized use or theft can be avoided. Therefore, this article shows you how to automatically add watermarks to images in your Java application. In particular, you’ll learn how to use Java to add watermarks to images.
- Adds a watermark to the image in Java
- Adds a diagonal watermark to the image in Java
To add watermarks to images, we’ll use Aspose.Imaging, an image processing API that lets you process multiple image formats from Java applications. If you haven’t used it yet, you can download the latest version **
Use Java to add a watermark to the image
Here are the steps to add a watermark to an image using Aspose.Imaging.
- Load the Image file using the Image class.
- Create an object of the Graphics class and initialize it with an Image object.
- Create and initialize Font and SolidBrush objects.
- Add the watermark using the graphics.drawstring (String s, Font Font, Brush Brush, float x, float y) method.
- Save the Image using the image.save () method.
The following code example demonstrates how to add a watermark to an image using Java.
// Load image
com.aspose.imaging.Image image = com.aspose.imaging.Image.load("image.png");
// Create and initialize an instance of Graphics class
Graphics graphics= new Graphics(image);
// Creates an instance of Font
Font font = new Font("Times New Roman", 16, FontStyle.Bold);
// Create an instance of SolidBrush and set its properties
SolidBrush brush = new SolidBrush();
brush.setColor(Color.getBlack());
brush.setOpacity(100);
// Draw a string using the SolidBrush and Font objects at specific point
graphics.drawString("Watermark by Aspose.Imaging for Java", font, brush, new PointF(image.getWidth()-100, image.getHeight()-100));
// Save image
image.save("watermarked-image.png");
Copy the code
Adds a diagonal watermark to the image in Java
In some cases, the watermark is applied diagonally to the image. For this case, Aspose.Imaging for Java provides watermark conversion options that you can use to rotate the watermark. Here are the steps to add a diagonal watermark to the image.
- Load the Image file using the Image class.
- Create an object of the Graphics class and initialize it with an Image object.
- Create and initialize Font and SolidBrush objects.
- Gets the image size in the size object.
- Create a new Matrix object and set the translation and transform to the desired Angle.
- Use the graphics.setTransform (Matrix) method to set up the conversion of the watermark.
- Add the watermark using the graphics.drawstring (String s, Font Font, Brush Brush, float x, float y) method.
- Save the Image using the image.save () method.
The following code example shows how to add diagonal watermarks to an image using Java.
// Load image com.aspose.imaging.Image image = com.aspose.imaging.Image.load("image.png"); // Create and initialize an instance of Graphics class Graphics graphics= new Graphics(image); // Creates an instance of Font Font font = new Font("Times New Roman", 16, FontStyle.Bold); // Create an instance of SolidBrush and set its properties SolidBrush brush = new SolidBrush(); brush.setColor(Color.getBlack()); brush.setOpacity(100); Size sz = graphics.getImage().getSize(); // Create an object of Matrix class for transformation Matrix matrix = new Matrix(); // First a translation then a rotation matrix.translate(sz.getWidth() / 2, sz.getHeight() / 2); Matrix. The rotate (45.0 f); // Set the Transformation through Matrix graphics.setTransform(matrix); // Draw a string using the SolidBrush and Font objects at specific point graphics.drawString("Watermark by Aspose.Imaging for Java", font, brush, 0, 0); // Save image image.save("watermarked-image.png");Copy the code
If you have any questions or requirements, please feel free to join the Aspose Technology Exchange Group (761297826), we are happy to provide you with inquiries and consultation.