Friday, January 31, 2025
Google search engine
HomeLanguagesJavaImage Processing in Java – Watermarking an Image

Image Processing in Java – Watermarking an Image

Prerequisites:

In this set, we will be generating a watermark and applying it to an input image. For generating a text and applying it in an image, we will use java.awt.Graphics package. Font and color of the text are applied by using java.awt.Color and java.awt.Font classes.

Methods used in Watermarking an Image:

1. getGraphics() – This method is found in BufferedImage class, and it returns a 2DGraphics object out of the image file.

2. drawImage(Image img, int x, int y, ImageObserver observer) – The x,y location specifies the position for the top-left of the image. The observer parameter notifies the application of updates to an image that is loaded asynchronously. The observer parameter is not frequently used directly and is not needed for the BufferedImage class, so it usually is null.

3. setFont(Font f) – This method is found in the Font class of the awt package, and the constructor takes (FONT_TYPE, FONT_STYLE, FONT_SIZE) as arguments.

4. setColor(Color c) – This method is found in the Color class of the awt package, and the constructor takes (R, G, B, A) as arguments.

5. drawString(String str, int x, int y) – Fond in Graphics class takes the string text, and the location coordinates as x and y as arguments.

Implementation:

Java




// Java code for watermarking an image
  
// For setting color of the watermark text
import java.awt.Color;
  
// For setting font of the watermark text
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 WaterMark {
    public static void main(String[] args)
    {
        BufferedImage img = null;
        File f = null;
  
        // Read image
        try {
            f = new File(
                "C:/Users/hp/Desktop/Image Processing in Java/gfg-logo.png");
            img = ImageIO.read(f);
        }
        catch (IOException e) {
            System.out.println(e);
        }
  
        // create BufferedImage object of same width and
        // height as of input image
        BufferedImage temp = new BufferedImage(
            img.getWidth(), img.getHeight(),
            BufferedImage.TYPE_INT_RGB);
  
        // Create graphics object and add original
        // image to it
        Graphics graphics = temp.getGraphics();
        graphics.drawImage(img, 0, 0, null);
  
        // Set font for the watermark text
        graphics.setFont(new Font("Arial", Font.PLAIN, 80));
        graphics.setColor(new Color(255, 0, 0, 40));
  
        // Setting watermark text
        String watermark = "WaterMark generated";
  
        // Add the watermark text at (width/5, height/3)
        // location
        graphics.drawString(watermark, img.getWidth() / 5,
                            img.getHeight() / 3);
  
        // releases any system resources that it is using
        graphics.dispose();
  
        f = new File(
            "C:/Users/hp/Desktop/Image Processing in Java/GFG.png");
        try {
            ImageIO.write(temp, "png", f);
        }
        catch (IOException e) {
            System.out.println(e);
        }
    }
}


Output:

Note: This code will not run on online ide since it requires an image in the drive.

This article is contributed by Pratik Agarwal. If you like Lazyroar and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the Lazyroar main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Dominic Wardslaus
Dominic Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments