验证码

001package cn.cnvc.servlet;
002 
003import java.awt.Color;
004import java.awt.Font;
005import java.awt.Graphics;
006import java.awt.image.BufferedImage;
007import java.io.IOException;
008import java.util.Random;
009 
010import javax.imageio.ImageIO;
011import javax.servlet.http.HttpServletRequest;
012import javax.servlet.http.HttpServletResponse;
013 
014public class Captcha {
015 
016    private int lines = 5;
017    private int length = 5;
018    private int spacing = 18;
019    private int height = 35;
020    private HttpServletRequest request;
021    private HttpServletResponse response;
022    private String code;
023    private Font font=new Font("微软雅黑", Font.BOLD, 20);
024    public void setFont(Font font) {
025        this.font = font;
026    }
027    public Font getFont() {
028        return font;
029    }
030    public String getCode() {
031        return code;
032    }
033     
034    public Captcha(){
035         
036    }
037    public Captcha(int lines, int length, int spacing, int height,
038            HttpServletRequest request, HttpServletResponse response) {
039        super();
040        this.lines = lines;
041        this.length = length;
042        this.spacing = spacing;
043        this.request = request;
044        this.response = response;
045    }
046 
047    /**
048     * 干扰线数量和字符长度
049     * @param lines
050     * @param length
051     */
052    public Captcha(int lines, int length) {
053        super();
054        this.lines = lines;
055        this.length = length;
056    }
057     
058     
059    public int getLines() {
060        return lines;
061    }
062 
063    public void setLines(int lines) {
064        this.lines = lines;
065    }
066 
067    public int getLength() {
068        return length;
069    }
070 
071    public void setLength(int length) {
072        this.length = length;
073    }
074 
075    public int getSpacing() {
076        return spacing;
077    }
078 
079    public void setSpacing(int spacing) {
080        this.spacing = spacing;
081    }
082 
083    public int getHeight() {
084        return height;
085    }
086 
087    public void setHeight(int height) {
088        this.height = height;
089    }
090     
091     
092     
093    public HttpServletRequest getRequest() {
094        return request;
095    }
096    public void setRequest(HttpServletRequest request) {
097        this.request = request;
098    }
099    public HttpServletResponse getResponse() {
100        return response;
101    }
102    public void setResponse(HttpServletResponse response) {
103        this.response = response;
104    }
105    /**
106     * 输出验证码
107     * @throws IOException
108     */
109    public String out() throws IOException{
110        String code=getCode(length);
111        BufferedImage image=new BufferedImage(length*(spacing+2), height, BufferedImage.TYPE_3BYTE_BGR);
112        Graphics g=image.getGraphics();
113         
114        g.setFont(font);
115        g.fillRect(0, 0, image.getWidth(),image.getHeight());
116        //画干扰线
117        int line=lines;//default 5条干扰线
118        Random random=new Random();
119        for(int i=0;i<line;i++){
120            //x1,x2
121            //y1,y2
122            int x1=random.nextInt(image.getWidth());
123            int x2=random.nextInt(image.getWidth());
124            int y1=random.nextInt(image.getHeight());
125            int y2=random.nextInt(image.getHeight());
126            g.setColor(new Color(random.nextInt(200),random.nextInt(200),random.nextInt(200)));
127            g.drawLine(x1, y1, x2, y2);
128        }
129        //绘制每个字符
130        for(int i=0;i<length;i++){
131            g.setColor(new Color(random.nextInt(200),random.nextInt(200),random.nextInt(200)));
132            g.drawString(String.valueOf(code.charAt(i)), i*this.spacing+random.nextInt(10), image.getHeight()/2+random.nextInt(15));   
133        }
134         
135        g.dispose();
136        response.setContentType("image/jpeg");
137        response.setHeader("Pragma","no-cache");
138        response.setHeader("Cache-Control","no-cache");
139        response.setIntHeader("Expires",-1);
140        ImageIO.write(image,"JPEG",response.getOutputStream());
141        return code;
142    }
143     
144    private String getCode(int length){
145        Random random=new Random();
146        StringBuffer buffer=new StringBuffer();
147        for(int i=0;i<length;i++){
148            buffer.append(random.nextInt(10));
149        }
150        return buffer.toString();
151    }
152}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值