Skip to content

Instantly share code, notes, and snippets.

@madan712
Created November 6, 2012 17:10
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save madan712/4026080 to your computer and use it in GitHub Desktop.
Save madan712/4026080 to your computer and use it in GitHub Desktop.
Simple Java program to Watermark an Image
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;
import javax.swing.ImageIcon;
public class WatermarkImage {
public static void main(String[] args) {
File origFile = new File("C:/OrignalImage.jpg");
ImageIcon icon = new ImageIcon(origFile.getPath());
// create BufferedImage object of same width and height as of original image
BufferedImage bufferedImage = new BufferedImage(icon.getIconWidth(),
icon.getIconHeight(), BufferedImage.TYPE_INT_RGB);
// create graphics object and add original image to it
Graphics graphics = bufferedImage.getGraphics();
graphics.drawImage(icon.getImage(), 0, 0, null);
// set font for the watermark text
graphics.setFont(new Font("Arial", Font.BOLD, 30));
//unicode characters for (c) is \u00a9
String watermark = "\u00a9 JavaXp.com";
// add the watermark text
graphics.drawString(watermark, 0, icon.getIconHeight() / 2);
graphics.dispose();
File newFile = new File("C:/WatermarkedImage.jpg");
try {
ImageIO.write(bufferedImage, "jpg", newFile);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(newFile.getPath() + " created successfully!");
}
}
Copy link

ghost commented Apr 10, 2014

Nice Job man
Helped me a Lot
can we remove that Watermark Using Java ?

@M-Tayyab
Copy link

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;
import javax.swing.ImageIcon;
public class WatermarkImage {
public static void main(String[] args) {
File origFile = new File("C:/tayyab.jpg");
ImageIcon icon = new ImageIcon(origFile.getPath());
// create BufferedImage object of same width and height as of original image
BufferedImage bufferedImage = new BufferedImage(icon.getIconWidth(),
icon.getIconHeight(), BufferedImage.TYPE_INT_RGB);
// create graphics object and add original image to it
Graphics graphics = bufferedImage.getGraphics();
graphics.drawImage(icon.getImage(), 1, 0, null);
// set font for the watermark text
graphics.setFont(new Font("Arial", Font.BOLD, 30));
//unicode characters for (c) is \u00a9
String watermark = "\u00b9 JavaXp.com";
// add the watermark text
graphics.drawString(watermark, 0, icon.getIconHeight() / 2);
graphics.dispose();
File newFile = new File("D:/Thesis java/WatermarkedImage.jpg");
try {
boolean write = ImageIO.write(bufferedImage, "jpg", newFile);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(newFile.getPath() + " created successfully!");
}
}

@gillesbraun
Copy link

Thank you so very much!
I used this snippet to add another image on top of my image, like this:

private void addWatermark(File imageFile) {
        try {
            File watermarkFile = new File(getClass().getResource("/watermark.png").toURI());
            BufferedImage image = ImageIO.read(imageFile);
            BufferedImage watermark = ImageIO.read(watermarkFile);
            Graphics g = image.getGraphics();
            g.drawImage(watermark, 0, 0, 150, 150, null);
            ImageIO.write(image, "jpg", imageFile);
        } catch (IOException | URISyntaxException e) {
            e.printStackTrace();
        }
    }

@gusthavosouza
Copy link

Hi manda, thanks a lot for share this snap code.
Two questions:
Whats the difference of ImageIO.read(file) instaed BufferedImage bufferedImage = new BufferedImage(icon.getIconWidth(),
icon.getIconHeight(), BufferedImage.TYPE_INT_RGB);
We have memory problem or something else?

Thnaks

@Sowmi123mn89
Copy link

Can you send source code for same program in java using discrete fourier transform

@Sowmi123mn89
Copy link

Adding watermark to an image using discrete fourier transform source code in java

@Sowmi123mn89
Copy link

I need reply plss

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment