Created
November 6, 2012 17:10
-
-
Save madan712/4026080 to your computer and use it in GitHub Desktop.
Simple Java program to Watermark an Image
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!"); | |
} | |
} |
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();
}
}
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
Can you send source code for same program in java using discrete fourier transform
Adding watermark to an image using discrete fourier transform source code in java
I need reply plss
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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!");
}
}