![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]()
![]() |

|
| Graphic tutorials All knowledge and info |
![]() |
|
Watermarking Images in a Java Servlet
|
LinkBack | Thread Tools | Display Modes |
|
|
#1 (permalink) |
|
Administrator
Posts: 18,720
Join Date: Jan 2006
Rep Power: 10
IM:
|
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. 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. 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. 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. 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. 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. 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 Original ImageWrapping It Up Below is the complete example of creating a watermark on an image from a servlet:
|
|
|
|
|
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
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 |