Creating Dynamic image

May 21, 2008

Some website prompt you to enter dynamic number generated in the image , especially when you are filling registration form ….and usually found at the end of registration form. User must enter exactly same number or alphanumeric character found on the image , this is because to make sure that Human is using webapplication and keeping away from robots/sypwares or any hacking software’s to fill the form.

Below is simple java code for how to generate dynamic image .This is a sample servlet that dynamically generates JPEG images. Currently all image properties are hard-coded, but it should be trivial to provide a jsp form to collect them (e.g., color, font, dimensions, messages). The generated image contains dynamic data such as the appserver name, and OS name.

import com.sun.image.codec.jpeg.JPEGCodec;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.servlet.http.*;
import javax.servlet.*;

public class ImageServlet extends HttpServlet {
private static final int WIDTH = 450;
private static final int HEIGHT = 200;
private static final Color BACKGROUND_COLOR = new Color(100,149,237);
private static final Color COLOR = new Color(0,0,139);
private static final Font FONT = new Font("Times New Roman", Font.BOLD, 46);
private static final Font FOOT_FONT = new Font("Courier", Font.ITALIC, 14);
private static final Color FOOT_COLOR = Color.BLACK;

@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
   response.setContentType("image/jpg");
   ServletOutputStream out = response.getOutputStream();
   BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_BYTE_INDEXED);
   Graphics graphics = image.getGraphics();
   graphics.setColor(BACKGROUND_COLOR);
   graphics.fillRect(0, 0, image.getWidth(), image.getHeight());
   graphics.setColor(COLOR);
   graphics.setFont(FONT);
   graphics.drawString("Hello World!", 10, HEIGHT/2); //insert String or any random number
   graphics.setFont(FOOT_FONT);
   graphics.setColor(FOOT_COLOR);
   graphics.drawString("Created by " + getServletContext().getServerInfo(), 10, HEIGHT - 30);
   graphics.drawString("for http://javahowto.blogspot.com/ on " + System.getProperty("os.name"), 10, HEIGHT - 10);
   JPEGCodec.createJPEGEncoder(out).encode(image);
}
}

The web.xml file is as simple as declaring and mapping a servlet:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
   <servlet-name>image</servlet-name>
   <servlet-class>ImageServlet</servlet-class>
</servlet>
<servlet-mapping>
   <servlet-name>image</servlet-name>
   <url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>

And i hope you know rest of the things to deploy the webapp ......

output...

Entry Filed under: java. .

1 Comment Add your own

  • 1. Syed Aslam  |  May 22, 2008 at 4:51 am

    Hmm.. this is cool. pretty useful..

    Reply

Leave a Comment

Required

Required, hidden

Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Trackback this post  |  Subscribe to the comments via RSS Feed


Categories

AD

May 2008
M T W T F S S
« Apr   Jun »
 1234
567891011
12131415161718
19202122232425
262728293031  

Archives

1

Blog Stats

Talk To Me

Text