Watermarking Images in a Java Servlet

In our previous tutorial, we showed how to create dynamic images images in a servlet. In this Java tutorial, we are going to take it a step further by dynamically adding a text watermark to an image as it is requested.

Setting up the Servlet
In the web.xml, you will need to configure a filter that will be used to call the servlet. By creating a filter, this will simplify the url used to access the servlet and image. To an end user, the filter will look like part of the directory structure for the image.

The servlet will be invoked when the url contains the pattern /watermark/*. In our example, we will place the images in a directory called photos in the web application directory. To view the image without the watermark, you would use the url http://webserver/webapp/photos/car.jpg. To invoke the servlet, you would use the url http://webserver/webapp/watermark/photos/car.jpg.

<servlet>
<servlet-name>com.codebeach.servlet.WatermarkServlet</servlet-name>
<servlet-class>com.codebeach.servlet.WatermarkServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>com.codebeach.servlet.WatermarkServlet</servlet-name>
<url-pattern>/watermark/*</url-pattern>
</servlet-mapping>
Getting the File Name
When the servlet is invoked, the first thing we need to do is to know which file is being requested to have a watermark added.
File file = new File(req.getPathTranslated());
if (!file.exists())
{
res.sendError(res.SC_NOT_FOUND);
return;
}
The getPathTranslated() method of the HttpServletRequest object provides the file name with the that was specified on the url after the watermark filter. If the requested file does not exist, this will return a not found error.

Creating the AlphaComposite
To allow us to see through the watermark, we will create an AlphaComposite. For our example, we will use a value of 50%. Since we are only drawing text in our tutorial for the watermark, we could have easily created a Color object where the alpha level was 50%. AlphaComposite allows more flexibility since it can be used when you draw anything on top of the the source image. So if you want to use your logo as a watermark, the code for blending it with the source image will be the same.

//Create an alpha composite of 50%
AlphaComposite alpha = AlphaComposite.getInstanceAlphaComposite.SRC_OVER,0.5f);
g2d.setComposite(alpha);
Drawing the Watermark
Since we have set the AlphaComposite, we now need to draw the text on the source image. For this example, we will set the color to white, enable text anti-aliasing, and a font of Arial Bold 30 point. There are a number of ways you could draw the watermark on the image. For this example, we will simply center the text by determining the sizes of the image and rectangle of the rendered string.
g2d.setColor(Color.white);
g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

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

String watermark = "Copyright © 2008";

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

int centerX = (photo.getIconWidth() - (int) rect.getWidth()) / 2;
int centerY = (photo.getIconHeight() - (int) rect.getHeight()) / 2;
g2d.drawString(watermark, centerX, centerY);

//Free graphic resources
g2d.dispose();
Creating a JPG
The final step is to write the image to the response output stream as a jpg. To do this, we will use the ImageIO class that was introduced with Java 1.4. The ImageIO class allows you to write an Image object to JPG, PNG, BMP, and WBMP. In Java 1.6, you will be able to write an Image as GIF.
//Set the mime type of the image
res.setContentType("image/jpg");

//Write the image as a jpg
OutputStream out = res.getOutputStream();
ImageIO.write(bufferedImage, "jpg", out);
out.close();

The ImageIO class will write the bufferedImage as a jpg to the output stream from the HttpServletResponse object.

The Results
To access the image, you can place the servlet call in an <img> tag or directly from the URL.

Original Image


Watermarked Image



Download the complete example


1 comments:

Anonymous said...

There is missing a "(" in Line 2 of the third Code Snippet.

Could you please add the Lines where "g2d" is defined?

thanks for the good work.

Post a Comment