<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ page import="java.awt.image.*" %>
<%@ page import="java.awt.*" %>
<%@ page import="java.util.*" %>
<%@ page import="javax.imageio.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<%
response.setContentType("image/jpeg");
response.setHeader("Pragma","No-cache");
response.setHeader("Cache-Control","no-cache");
response.setDateHeader("Expires", 0);
HttpSession mysession=request.getSession();
int width=65,height=30;
BufferedImage bi=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
Graphics g=bi.getGraphics();
g.setColor(getRandColor(200,255));
g.fillRect(0,0,width,height);
String code="";
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
for(int i=0;i<4;i++)
{
String num=String.valueOf(rand.nextInt(10));
code+=num;
g.setColor(getRandColor(0,100));
g.setFont(new Font("Times New Roman",Font.PLAIN,18));
g.drawString(num,5+i*10+rand.nextInt(5), 12+rand.nextInt(8));
}
int lines=5;
g.setColor(getRandColor(100,150));
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
for(int i=0;i<lines;i++)
{
int x0=rand.nextInt(width);
int y0=rand.nextInt(height);
int x1=rand.nextInt(width);
int y1=rand.nextInt(height);
g.drawLine(x0,y0,x1,y1);
}
mysession.setAttribute("code",code);
g.dispose();
ServletOutputStream sos=response.getOutputStream();
ImageIO.write(bi,"jpeg",sos);
sos.flush();
sos.close();
%>
<%!
Random rand=new Random();
Color getRandColor(int min,int max)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
int r=min+rand.nextInt(max-min);
int g=min+rand.nextInt(max-min);
int b=min+rand.nextInt(max-min);
return new Color(r,g,b);
}
%>
</body>
</html>
随机码可以自己扩展, 我这里只用了(0-9)10个数字
posted on 2009-05-13 22:24
小果子 阅读(112)
评论(0) 编辑 收藏 引用 所属分类:
学习笔记