Now,I can generate a image which contains a random number on web page with Servlet, but I
CAN NOT extract the number of the image with one common Java Application which I want to write.
Any one can help me?
My Code to generate image on web page (looks disorder, please format in your own IDE)
//--------------------------------------------------
package com.hib.img;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Date;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class ValidateCodeImage extends HttpServlet {
private static final long serialVersionUID = 1L;
private static String name;
private int width;
private int height;
private int length;
private int interlinenum;
private int maxfontsize;
private int minfontsize;
private String randomCode;
public void init(ServletConfig config) throws ServletException {
super.init(config);
name = config.getInitParameter("name");
width = Integer.parseInt(config.getInitParameter("width")) ;
height = Integer.parseInt(config.getInitParameter("height") );
length = Integer.parseInt(config.getInitParameter("length") );
maxfontsize = Integer.parseInt(config.getInitParameter("maxfonts ize"));
minfontsize = Integer.parseInt(config.getInitParameter("minfonts ize"));
interlinenum = Integer.parseInt(config.getInitParameter("interlin enum"));
}
public void doGet(HttpServletRequest req, HttpServletResponse res) {
//set necessary response infomation
res.setContentType("image/jpeg");
res.setHeader("Pragma", "No-cache");
res.setHeader("Cache-Control", "No-cache");
res.setDateHeader("Expires", 0L);
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics graph = image.getGraphics();
//set background color and fill the rectangle in specified color
graph.setColor(ImageUtil.getRandomColor(200,55));
graph.fillRect(0, 0, width, height);
//set interrupt line's color and draw random line in specifed color
graph.setColor(ImageUtil.getRandomColor(100,27));
for (int i = 0; i < interlinenum; i++) {
ImageUtil.drawRandomLine(graph, width, height);
}
//set the random code color and draw random code
graph.setColor(ImageUtil.getRandomColor(60,3));
drawRandomCode(graph,width,height,length);
//set session value
HttpSession session = req.getSession();
session.setAttribute(name, this.randomCode);
//release the resource of the image
graph.dispose();
//output the image to the page
try {
ImageIO.write(image, "JPEG", res.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
}
public void doPost(HttpServletRequest req, HttpServletResponse res){
doGet(req,res);
}
/**
*
* @param graph graphics context
* @param width width of the range of the graphics context
* @param height height of the range of the graphics context
* @param length length of the random code
*/
private void drawRandomCode(Graphics graph,int width,int height,int length){
StringBuffer sb = new StringBuffer();
String eachCode = null;
int fontsize = minfontsize;
int y = 0;
Random r = new Random(new Date().getTime());
for(int index=0;index<length;index++){
//random code
eachCode = String.valueOf(r.nextInt(10));
//constrct complete random code
sb.append(eachCode);
//random fontsize
fontsize = minfontsize + r.nextInt(maxfontsize-minfontsize);
//random y axes
y = height/2 + r.nextInt(height/2);
//set font and draw random code
graph.setFont(new Font("Times New Roman", Font.BOLD, fontsize));
graph.drawString(eachCode, (int)(index*((double)width/(double)length)), y);
}
this.randomCode = sb.toString();
}
/**
* @return the name
*/
public static String getName() {
return name;
}
}
package com.hib.img;
import java.awt.Color;
import java.awt.Graphics;
import java.util.Date;
import java.util.Random;
public class ImageUtil {
public static final int MAX_COLOR_VALUE = 255;
private static Random r = new Random(new Date().getTime());
public static Color getRandomColor(int base,int range) {
if(range > MAX_COLOR_VALUE){
range = MAX_COLOR_VALUE;
}
int red = base + r.nextInt(range);
int green = base + r.nextInt(range);
int blue = base + r.nextInt(range);
return new Color(red, green, blue);
}
/**
*
* @param graph graphics context
* @param width width of the range of the graphics context
* @param height height of the range of the graphics context
*/
public static void drawRandomLine(Graphics graph, int width, int height) {
int sx = r.nextInt(width);
int ex = r.nextInt(width);
int sy = r.nextInt(height);
int ey = r.nextInt(height);
graph.drawLine(sx, sy, ex, ey);
}
}