xiaoguozi's Blog
Pay it forword - 我并不觉的自豪,我所尝试的事情都失败了······习惯原本生活的人不容易改变,就算现状很糟,他们也很难改变,在过程中,他们还是放弃了······他们一放弃,大家就都是输家······让爱传出去,很困难,也无法预料,人们需要更细心的观察别人,要随时注意才能保护别人,因为他们未必知道自己要什么·····
<%@ 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
="";
    
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));
    
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)
{
    
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 小果子 阅读(106) 评论(0)  编辑 收藏 引用 所属分类: 学习笔记

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理