I'm trying to add watermark to an image on my website. The way I want it to work is to show the watermark when the image is downloaded or shown on other websites. On my website I want to show it without the watermark. It would be awesome if the watermark is placed on the picture. Is this possible to do the above mentioned by using some picture meta-data or http headers for example? Or is it impossible to perform this and should I do it by adding a footer upon upload and hiding it?
Asked
Active
Viewed 3,095 times
5
-
Look for "java watermark image" - there are plenty of posts to do this. It isn't JavaEE per se but can be implemented in, for example, a servlet. It would be very easy to display the image without it on your site and with it downloaded. Of course, that assumes that the version on your site is not desirable for some reason - for example, resolution. Otherwise people will just download it directly from you without the watermark. – stdunbar Aug 25 '16 at 15:42
-
Yeah, I can use google, adding watermark itself isn't my problem. I'm talking about adding watermark upon Right Click -> Save/Show Image. – Fubundzer Aug 25 '16 at 21:45
-
Disable right click using Javascript and have a "save image" button. On the saved image, add the water mark. It won't stop anybody a bit determined but it will be better than nothing. – stdunbar Aug 25 '16 at 21:57
-
Just remember that the user can always either capture whatever is on their screen and perhaps even capture the image without the watermark through the browser. – user1803551 Aug 28 '16 at 03:13
1 Answers
4
You can reference the follow link:
Add text watermark to image
static void addTextWatermark(String text, File sourceImageFile, File destImageFile) {
try {
BufferedImage sourceImage = ImageIO.read(sourceImageFile);
Graphics2D g2d = (Graphics2D) sourceImage.getGraphics();
// initializes necessary graphic properties
AlphaComposite alphaChannel = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.1f);
g2d.setComposite(alphaChannel);
g2d.setColor(Color.BLUE);
g2d.setFont(new Font("Arial", Font.BOLD, 64));
FontMetrics fontMetrics = g2d.getFontMetrics();
Rectangle2D rect = fontMetrics.getStringBounds(text, g2d);
// calculates the coordinate where the String is painted
int centerX = (sourceImage.getWidth() - (int) rect.getWidth()) / 2;
int centerY = sourceImage.getHeight() / 2;
// paints the textual watermark
g2d.drawString(text, centerX, centerY);
ImageIO.write(sourceImage, "png", destImageFile);
g2d.dispose();
System.out.println("The tex watermark is added to the image.");
} catch (IOException ex) {
System.err.println(ex);
}
}
Add image watermark to image
static void addImageWatermark(File watermarkImageFile, File sourceImageFile, File destImageFile) {
try {
BufferedImage sourceImage = ImageIO.read(sourceImageFile);
BufferedImage watermarkImage = ImageIO.read(watermarkImageFile);
// initializes necessary graphic properties
Graphics2D g2d = (Graphics2D) sourceImage.getGraphics();
AlphaComposite alphaChannel = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.3f);
g2d.setComposite(alphaChannel);
// calculates the coordinate where the image is painted
int topLeftX = (sourceImage.getWidth() - watermarkImage.getWidth()) / 2;
int topLeftY = (sourceImage.getHeight() - watermarkImage.getHeight()) / 2;
// paints the image watermark
g2d.drawImage(watermarkImage, topLeftX, topLeftY, null);
ImageIO.write(sourceImage, "png", destImageFile);
g2d.dispose();
System.out.println("The image watermark is added to the image.");
} catch (IOException ex) {
System.err.println(ex);
}
}

Huỳnh Ngọc Bang
- 1,572
- 1
- 19
- 22