Skip to main content

Add watermark to jpeg

2 replies [Last post]
Joined: 2007-10-08
Points: 0


I need to add a watermark to jpeg image file. What I was trying to do was that I had a watermark in PNG format The watermark has transparent background with text having opacity 75%.
Using JAI overlay, I am trying to place that png file on the jpeg file. It gives error that both images should have same number of bands and data type. Can anyone help me in this regard?
Or can you provide me some code for watermarking ?

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Joined: 2007-09-24
Points: 0

hi Umair123,

Here i have add a watermark in a jpeg image, which is in String format. You may convert try or convert to PNG format. It may helps you.

tempimg = inpPlnr.getAsBufferedImage();
Graphics2D g2d = (Graphics2D) tempimg.getGraphics();

g2d.drawImage(/*photo.getImage()*/tempimg, 0, 0, null);

//Create an alpha composite of 40%

AlphaComposite alpha = AlphaComposite.getInstance(AlphaComposite.SRC_OVER,0.4f);


g2d.setFont(new Font("Arial", Font.BOLD, 30));

String watermrk = "DCKAP Technology © 2007";

FontMetrics fontMetrics = g2d.getFontMetrics();
Rectangle2D rect = fontMetrics.getStringBounds(watermrk, g2d);

(tempimg.getWidth() - (int) rect.getWidth()) / 2,
(tempimg.getHeight() - (int) rect.getHeight()) / 2);

textwater = (PlanarImage)(new RenderedImageAdapter(tempimg));

inpPlnr is a planarImage.
tempimg is a BufferedImage
textwater is a Planarimage

and i have imported JAVA JAI package here. You can download that in sun site.

I hope it helps you


Joined: 2004-06-19
Points: 0

Your description of the problem is not clear, but I suspect that you are trying to load the PNG watermark over the top of the JPEG image by telling the PNG reader to accept the JPEG image as its destination. Since it is unlikely both readers work with the same image formats, the PNG reader is complaining the destination is of the wrong type. JAI is overkill for this and the standard Java IO libraries can easily do this for you. Load both the watermark PNG image and JPEG image into BufferedImages using ImageIO. Then just obtain the graphics object for the JPEG and use drawImage() to paste the watermark over the top. You can call setComposite on the Graphics obtained from the BufferedImage to set the opacity to draw at too. i.e. something like

Graphics2D g=image.createGraphics();
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, opacity));

Then just save the image back to wherever.
Then just