Error » Graphic and Arts Error !! » Graphic tutorials » Watermarking Images in a Java Servlet

Graphic tutorials All knowledge and info

Post New Thread Reply
  Watermarking Images in a Java Servlet
LinkBack Thread Tools Display Modes
Old 21-Dec-2006, 01:47 AM   #1 (permalink)
Administrator
 
Anilrgowda's Avatar

Posts: 18,720
Join Date: Jan 2006
Rep Power: 10 Anilrgowda is on a distinguished road

IM:
Default 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.


com.codebeach.servlet.WatermarkServlet
com.codebeach.servlet.WatermarkServlet



com.codebeach.servlet.WatermarkServlet
/watermark/*
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.
Loading and Drawing the Image
Next,we will load the image and draw it onto a BufferedImage. It is being drawn on to a BufferedImage to allow us to modify the image by adding a watermark to it before it is sent to the web browser.
ImageIcon photo = new ImageIcon(req.getPathTranslated());

//Create an image 200 x 200

BufferedImage bufferedImage = new BufferedImage(photo.getIconWidth(),
photo.getIconHeight(),
BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = (Graphics2D) bufferedImage.getGraphics();
g2d.drawImage(photo.getImage(), 0, 0, null);

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.getInstance(AlphaComposite.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 � 2006";

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

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

//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 tag or directly from the URL.
Original Image

Watermarked Image

Wrapping It Up
Below is the complete example of creating a watermark on an image from a servlet:

package com.codebeach.servlet;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.awt.*;
import java.awt.image.*;
import javax.imageio.*;
import javax.swing.ImageIcon;
import java.awt.geom.Rectangle2D;

public class WatermarkServlet extends HttpServlet
{
public void doGet(HttpServletRequest req, HttpServletResponse res)
{

try
{
File file = new File(req.getPathTranslated());
if (!file.exists())
{
res.sendError(res.SC_NOT_FOUND);
return;
}

ImageIcon photo = new ImageIcon(req.getPathTranslated());

//Create an image 200 x 200
BufferedImage bufferedImage = new BufferedImage(photo.getIconWidth(),
photo.getIconHeight(),
BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = (Graphics2D) bufferedImage.getGraphics();

g2d.drawImage(photo.getImage(), 0, 0, null);

//Create an alpha composite of 50%
AlphaComposite alpha = AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
0.5f);
g2d.setComposite(alpha);

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 � 2006";

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

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

//Free graphic resources
g2d.dispose();

//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();
}
catch (IOException ioe)
{
}
}
}
Anilrgowda is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit!
Reply With Quote
   


   
Post New Thread Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Bytescout Watermarking 1.00 Beta 3 Xtreme Application Downloads 0 03-Mar-2008 01:55 AM
Microsoft licenses its audio watermarking technology Anilrgowda Microsoft 0 15-Aug-2007 11:44 PM
The Javalog.txt file is created in the Windows\Java folder when Java logging is enabl Anilrgowda Microsoft windows vista error 0 29-Jan-2007 10:09 AM
Guide to Watermarking Your Pictures Anilrgowda Graphic tutorials 1 06-Jan-2007 02:19 AM
Creating Images in a Java Servlet Anilrgowda Programming tutorials 0 21-Dec-2006 01:51 AM


All times are GMT -8. The time now is 10:17 AM.

Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0

DMCA Policy

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228