Watermarking Images in a Java Servlet


In our previous tutorial, we showed how to create dynamic images images in a servlet. In this 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.

  1. <servlet>
  2. <servlet-name>com.codebeach.servlet.WatermarkServletservlet-name>
  3. <servlet-class>com.codebeach.servlet.WatermarkServletservlet-class>
  4. servlet>
  5. <servlet-mapping>
  6. <servlet-name>com.codebeach.servlet.WatermarkServletservlet-name>
  7. <url-pattern>/watermark/*url-pattern>
  8. 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.
  1. File file = new File(req.getPathTranslated());
  2. if (!file.exists())
  3. {
  4. res.sendError(res.SC_NOT_FOUND);
  5. return;
  6. }
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.

  1. //Create an alpha composite of 50%
  2. AlphaComposite alpha = AlphaComposite.getInstanceAlphaComposite.SRC_OVER,0.5f);
  3. 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.
  1. g2d.setColor(Color.white);
  2. g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
  3. RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
  4. g2d.setFont(new Font("Arial", Font.BOLD, 30));
  5. String watermark = "Copyright © 2008";
  6. FontMetrics fontMetrics = g2d.getFontMetrics();
  7. Rectangle2D rect = fontMetrics.getStringBounds(watermark, g2d);
  8. int centerX = (photo.getIconWidth() - (int) rect.getWidth()) / 2;
  9. int centerY = (photo.getIconHeight() - (int) rect.getHeight()) / 2;
  10. g2d.drawString(watermark, centerX, centerY);
  11. //Free graphic resources
  12. 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.
  1. //Set the mime type of the image
  2. res.setContentType("image/jpg");
  3. //Write the image as a jpg
  4. OutputStream out = res.getOutputStream();
  5. ImageIO.write(bufferedImage, "jpg", out);
  6. 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 tag or directly from the URL.

Original Image
Watermarked Image


Watermarked Image

Comments

Anonymous said…
Hi-ya people, neat board I find It vastly useful and its helped me alot
I hope to be able to contribute & help other people like this message board has helped me

_________________
[url=http://iphoneusers.com]jailbreak iphone 4.0[/url]
Anonymous said…
Whats's Up im fresh on here. I stumbled upon this message board I have found It extremely accommodating and it's helped me out a lot. I should be able to give something back & support other users like its helped me.

Thanks, See Ya Later
Anonymous said…
Sup i am fresh here, I stumbled upon this message board I have found It exceedingly helpful & its helped me a lot. I should be able to contribute and help others like its helped me.

Thanks Everyone, See Ya Later.
Anonymous said…
Finally the future of safe smoking is here! It looks, feels & tasts like a real cigarette but it's not a traditional cigarette. It's called an electronic cigarette People who already have experienced this incredible smokeless device are amazed by it's performance which contains No Tar,Ash,Odor or Secondhand Smoke.

[URL=http://www.electroniccig.com][B]electronic cig[/B][/URL]
Anonymous said…
Hi-ya i'm fresh on here. I hit upon this website I find It exceedingly accommodating and it has helped me a lot. I should be able to give something back and aid other users like it has helped me.

Thank's, See Ya Around.
Anonymous said…
Hey i'm fresh to this, I came upon this website I find It extremely helpful and its helped me tons. I hope to give something back and assist others like it has helped me.

Thanks, See You About.
Anonymous said…
Hi i'm new on here. I stumbled upon this board I have found It quite helpful and it's helped me a lot. I should be able to give something back and guide other people like it has helped me.

Thanks a load, See You About.
Anonymous said…
Hey i am fresh to this. I stumbled upon this website I have found It truly accommodating and it's helped me out alot. I should be able to contribute & support other people like its helped me.

Thanks, See Ya Later.
Anonymous said…
Greetings i'm fresh to this, I came upon this message board I find It extremely useful & it's helped me loads. I should be able to give something back and aid other users like it has helped me.

Cheers, See You Later
Anonymous said…
Good Day everyone, brilliant chat board I find It amply accessible & its helped me out tons
I hope to contribute and assist other users like this message board has helped me
Anonymous said…
Good Day im fresh here. I hit upon this board I find It extremely accommodating and it has helped me a great deal. I hope to give something back & aid other people like it has helped me.

Thanks a load, See Ya Later.
Anonymous said…
Good Day i'm new on here. I came accross this message board I find It vastly helpful & it's helped me a great deal. I hope to give something back and help others like its helped me.

Thanks Everyone, See Ya About.

Popular posts from this blog

PostgreSQL bytea and oid

Microservices Architecture with Spring Boot in 15mins

Adding MySQL datasource to JBOSS AS 7