Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to create a watermark on an image using Java OpenCV library?
Following Java example draws a watermark (“Hello”) on the given image and saves it back.
Example
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class WaterMarkExample {
public static void main(String[] args) throws IOException {
//Reading the contents of an image
File file = new File("D:\Images\test1.jpg");
BufferedImage img = ImageIO.read(file);
//Creating an empty image for output
int height = img.getHeight();
int width = img.getWidth();
BufferedImage res = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
//Creating a graphics object
Graphics graphics = res.getGraphics();
graphics.drawImage(img, 0, 0, null);
//Creating font for water mark
Font font = new Font("Arial", Font.PLAIN, 45);
graphics.setFont(font);
graphics.setColor(new Color(255, 150, 200, 30));
//Drawing the water mark string on the image
graphics.drawString("Hello", width/7, height/5);
//Disposing the string
graphics.dispose();
//Writing the result image.
file = new File("D:\Images\watermark.jpg");
ImageIO.write(res, "jpg", file);
}
}
Input

Output

Advertisements