<%@ page import="java.awt.*,java.awt.image.*,java.util.*,com.sun.image.codec.jpeg.*" %>
<%!
//根据提供的ab产生随机的颜色变化范围
Color getColor(int a,int b){
int n=b-a;
Random rd=new Random();
int cr=a+rd.nextInt(n);
int cg=a+rd.nextInt(n);
int cb=a+rd.nextInt(n);
return new Color(cr,cg,cb);
}
%>
<% //下边三行取消客户端游览器缓存验证码的功能
response.setHeader("Pragma","No-cache");
response.setHeader("Cache-Control","no-cache");
response.setDateHeader("Expires", 0);
int width=60, height=20;
//在内存中生成一个图像
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
Random random = new Random();
g.setColor(getColor(200,250));
g.fillRect(0, 0, width, height);
g.setFont(new Font("Times New Roman",Font.BOLD,18));
g.setColor(getColor(160,200));
for (int i=0;i<160;i++)
{
int x = random.nextInt(width);
int y = random.nextInt(height);
int xl = random.nextInt(12);
int yl = random.nextInt(12);
g.drawLine(x,y,x+xl,y+yl);
}
String number=String.valueOf(1000+random.nextInt(8999));
String name=request.getParameter("name");
session.setAttribute(name,number);
g.setColor(getColor(20,130));
int x=(int)(width*0.2);
int y=(int)(height*0.8);
g.drawString(number,x,y);
g.dispose();
JPEGImageEncoder encoder=JPEGCodec.createJPEGEncoder(response.getOutputStream());
encoder.encode(image);
out.close();
%>