验证码

package cn.cnvc.servlet;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Captcha {

	private int lines = 5;
	private int length = 5;
	private int spacing = 18;
	private int height = 35;
	private HttpServletRequest request;
	private HttpServletResponse response;
	private String code;
	private Font font=new Font("微软雅黑", Font.BOLD, 20);
	public void setFont(Font font) {
		this.font = font;
	}
	public Font getFont() {
		return font;
	}
	public String getCode() {
		return code;
	}
	
	public Captcha(){
		
	}
	public Captcha(int lines, int length, int spacing, int height,
			HttpServletRequest request, HttpServletResponse response) {
		super();
		this.lines = lines;
		this.length = length;
		this.spacing = spacing;
		this.request = request;
		this.response = response;
	}

	/**
	 * 干扰线数量和字符长度
	 * @param lines
	 * @param length
	 */
	public Captcha(int lines, int length) {
		super();
		this.lines = lines;
		this.length = length;
	}
	
	
	public int getLines() {
		return lines;
	}

	public void setLines(int lines) {
		this.lines = lines;
	}

	public int getLength() {
		return length;
	}

	public void setLength(int length) {
		this.length = length;
	}

	public int getSpacing() {
		return spacing;
	}

	public void setSpacing(int spacing) {
		this.spacing = spacing;
	}

	public int getHeight() {
		return height;
	}

	public void setHeight(int height) {
		this.height = height;
	}
	
	
	
	public HttpServletRequest getRequest() {
		return request;
	}
	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}
	public HttpServletResponse getResponse() {
		return response;
	}
	public void setResponse(HttpServletResponse response) {
		this.response = response;
	}
	/**
	 * 输出验证码
	 * @throws IOException
	 */
	public String out() throws IOException{
		String code=getCode(length);
		BufferedImage image=new BufferedImage(length*(spacing+2), height, BufferedImage.TYPE_3BYTE_BGR);
		Graphics g=image.getGraphics();
		
		g.setFont(font);
		g.fillRect(0, 0, image.getWidth(),image.getHeight());
		//画干扰线
		int line=lines;//default 5条干扰线
		Random random=new Random();
		for(int i=0;i<line;i++){
			//x1,x2
			//y1,y2
			int x1=random.nextInt(image.getWidth());
			int x2=random.nextInt(image.getWidth());
			int y1=random.nextInt(image.getHeight());
			int y2=random.nextInt(image.getHeight());
			g.setColor(new Color(random.nextInt(200),random.nextInt(200),random.nextInt(200)));
			g.drawLine(x1, y1, x2, y2);
		}
		//绘制每个字符
		for(int i=0;i<length;i++){
			g.setColor(new Color(random.nextInt(200),random.nextInt(200),random.nextInt(200)));
			g.drawString(String.valueOf(code.charAt(i)), i*this.spacing+random.nextInt(10), image.getHeight()/2+random.nextInt(15));	
		}
		
		g.dispose();
		response.setContentType("image/jpeg");
		response.setHeader("Pragma","no-cache");
		response.setHeader("Cache-Control","no-cache");
		response.setIntHeader("Expires",-1);
		ImageIO.write(image,"JPEG",response.getOutputStream());
		return code;
	}
	
	private String getCode(int length){
		Random random=new Random();
		StringBuffer buffer=new StringBuffer();
		for(int i=0;i<length;i++){
			buffer.append(random.nextInt(10));
		}
		return buffer.toString();
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值