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();
}
}
验证码
最新推荐文章于 2019-04-10 11:20:10 发布