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.HttpServletResponse;
public class AuthImage {
// 图片类型(JPEG)
public static final String TYPE_JPEG = "JPEG";
// 图片类型(PNG)
public static final String TYPE_PNG = "PNG";
// 图片的宽度
private int width = 80;
// 图片的高度
private int height = 18;
// 随机数工具类
private Random random;
// 验证码
private String authCode;
public AuthImage(){
this.random=new Random();
}
/**
* @param width
* @param height
*/
public AuthImage(int width,int height){
this.width=width;
this.height=height;
this.random=new Random();
}
/**
* @param response
* @throws IOException
*/
public void outImage(HttpServletResponse response) throws IOException{
this.outImage(response, TYPE_JPEG);
}
/**
* @param response
* @param style
* @throws IOException
*/
public void outImage(HttpServletResponse response,int style) throws IOException{
switch(style){
case 1 : this.outImage(response, TYPE_JPEG);
break;
case 2 : this.outImage(response, TYPE_PNG);
break;
default : this.outImage(response);
}
}
/**
* @param response
* @param style
* @throws IOException
*/
private void outImage(HttpServletResponse response,String style) throws IOException{
BufferedImage image=new BufferedImage(this.width,this.height, BufferedImage.TYPE_INT_BGR);
this.drawImage(image.getGraphics());
ImageIO.write(image,style, response.getOutputStream());
}
/**
* 画成图片
*
* @param g
*/
private void drawImage(Graphics g){
BufferedImage image=new BufferedImage(this.width,this.height,BufferedImage.TYPE_INT_BGR);
Graphics imageG=image.getGraphics();
Color c=imageG.getColor();
this.drawBackground(imageG);
this.drawMixedColor(imageG);
this.drawString(imageG);
g.drawImage(image, 0, 0, null);
g.setColor(c);
}
/**
* 画背景
*
* @param g
*/
private void drawBackground(Graphics g){
int red=200+random.nextInt(56);
int green=100+random.nextInt(51);
int yellow=200+random.nextInt(56);
g.setColor(new Color(red,green,yellow));
g.fillRect(0, 0, width, height);
g.setColor(new Color(240,100,170));
g.drawRect(0, 0, width-1, height-1);
}
/**
* 画杂色,给背景添加杂色
*
* @param g
*/
private void drawMixedColor(Graphics g){
for(int i=0;i<6;i++){
int red=200-random.nextInt(40);
int green=100+random.nextInt(40);
int yellow=200-random.nextInt(40);
int startX=random.nextInt(this.width/2);
int startY=random.nextInt(this.height/2);
int endX=random.nextInt(this.width-1);
int endY=random.nextInt(this.height-1);
g.setColor(new Color(red,green,yellow));
g.drawLine(startX, startY, endX, endY);
}
}
/**
* 画显示字符串
*
* @param g
* @return
*/
private void drawString(Graphics g){
StringBuilder rndCode=new StringBuilder();
int x=13;
Font font=new Font("宋体",Font.BOLD,14);
g.setFont(font);
for(int i=0;i<6;i++){
int y=13+random.nextInt(3);
String rnd=CodeUtils.getDigitUpperCode(this.random);
int red=this.random.nextInt(160);
int green=this.random.nextInt(80);
int yellow=this.random.nextInt(160);
g.setColor(new Color(red,green,yellow));
g.drawString(rnd, x+10*i, y);
rndCode.append(rnd);
}
this.authCode=rndCode.toString();
}
/**
* @return
*/
public String getAuthCode() {
return authCode;
}
}
AuthImage image=new AuthImage();
image.outImage(response);
String code=image.getAuthCode();
System.out.println(code);