import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;


public class CheckCode {

public static void main(String args[]) throws InterruptedException{

CheckCode c = new CheckCode();
char [] cs = c.CreateCheckCode(4);
System.out.println(new String(cs));
c.setIsDrawLine(true);
c.setIsChangeBackgroud(false);
c.CreateCheckCodeImage(cs);
c.saveCheckCodeImage("123.jpg");
System.out.println("Ok");

}

//保存生成的验证码
private BufferedImage checkCodeImg = null;


/**
* 随机的字符就从这里面抽取
*/
private char [] chars;

private ImageIcon img = null;

//图片宽
private int width = 100;
//图片高
private int height = 40;

//颜色集
private Color [] colors = new Color[]{

Color.black,Color.blue,Color.cyan,Color.darkGray,
Color.gray,Color.green,Color.lightGray,Color.magenta,
Color.orange,Color.pink,Color.red,Color.white,
Color.yellow
};

//是否画干扰线
private boolean isDrawLine = false;
//是否随机更改背景
private boolean isChangeBackgroud = false;

/**
* 无参的构造器,默认采用A~B,0~9作为验证码
*/
public CheckCode(){

this.setChars(new char[]{'0','1','2','3','4','5','6','7','8','9',
'A','B','C','D','E','F','G','H','I','Z','K','L','N','M','O','P','Q','R','S','T','U','V','W','S','Y','Z'});
}

/**
*
@param chars 要随机生成的字符数组集
*/
public CheckCode(char [] chars){

this.setChars(chars);
}

/**
* 生成验证码
*
@param len 验证码长度
*/
public char [] CreateCheckCode(int len){

Random r = new Random();

char [] tempChars = new char[len];

for(int i = 0; i < len; i++){
tempChars[i] = getChars()[((int)Math.floor(r.nextFloat() * getChars().length))];
}

return tempChars;
}

/**
* 生成验证码图片
*
@param len 要生成的验证码字符数组
*/
public BufferedImage CreateCheckCodeImage(char [] chars){

Random r = new Random();

//得到背景颜色色值
int bgColorIndex = ((int)(Math.floor(r.nextFloat() * colors.length)));

Color bgColor = colors[bgColorIndex];

//得到字体颜色色值
int fontColorIndex = ((int)(Math.floor(r.nextFloat() * colors.length)));

while(fontColorIndex == bgColorIndex){

fontColorIndex = ((int)(Math.floor(r.nextFloat() * colors.length)));
}
Color fontColor = colors[fontColorIndex];

//创建一个空白图片
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

Graphics2D g2D = image.createGraphics();

//刷新背景
if(this.getIsChangeBackgroud()){

g2D.setBackground(bgColor);

}else{

/*
//设置是否随机改变背景色
image = g2D.getDeviceConfiguration().createCompatibleImage(width, height, Transparency.TRANSLUCENT);
g2D.dispose();
g2D = image.createGraphics();
*/
g2D.setBackground(Color.WHITE);
}
g2D.clearRect(0, 0, image.getWidth(), image.getHeight());

//设置字体

int fontSize = this.width / 7;

Font font = new Font("Verdana",Font.ITALIC ,fontSize);

g2D.setFont(font);

int p = this.width / chars.length;


//画字体
g2D.setColor(fontColor);
for(int i = 0; i < chars.length; i++){

g2D.drawString(String.valueOf(chars[i]), i * p + (5.0f * this.width / 100.0f), (this.height - fontSize) / 2 + fontSize / 1.3f);

//得到字体颜色色值
//fontColorIndex = ((int)(Math.floor(r.nextFloat() * colors.length)));

while(fontColorIndex == bgColorIndex){

fontColorIndex = ((int)(Math.floor(r.nextFloat() * colors.length)));
}
Color color = colors[fontColorIndex];
g2D.setColor(color);


}

if(this.getIsDrawLine()){
//随机生成划线
Point points [] = new Point[5];

int x= 0,y = 0;
for(int i = 0; i < points.length; i++){


points[i] = new Point((int)(Math.floor(r.nextFloat() * this.width)), (int)(Math.floor(r.nextFloat() * this.height)));

if(i > 0){

g2D.drawLine(points[i - 1].x,points[i - 1].y,points[i].x,points[i].y);

try {
Thread.sleep(10);
} catch (InterruptedException e) {

e.printStackTrace();
}
}
}
}

this.setCheckCodeImg(image);
return image;
}

/**
* 保存验证码图片到指定的位置
*
@param fileName 验证码图片要保存的位置
*/
public void saveCheckCodeImage(String fileName){

if(this.getCheckCodeImg() == null){
return;
}

try {

File file = new File(fileName);
file.deleteOnExit();

ImageIO.write(this.getCheckCodeImg(), "png",file);


} catch (IOException e1) {

e1.printStackTrace();
}
}

/**
* set chars all check code to array
*
@param chars all check code to array
*/
public void setChars(char [] chars) {
this.chars = chars;
}
/**
*
@return all check code to array
*/
public char [] getChars() {
return chars;
}

/**
* set is draw line
*
@param isDrawLine
*/
public void setIsDrawLine(boolean isDrawLine) {
this.isDrawLine = isDrawLine;
}

/**
*
@return is draw line
*/
public boolean getIsDrawLine() {
return isDrawLine;
}

private void setCheckCodeImg(BufferedImage checkCodeImg) {
this.checkCodeImg = checkCodeImg;
}

public BufferedImage getCheckCodeImg() {
return checkCodeImg;
}
/**
* set change backgroud is random
*
@param isChangeBackgroud
*/
public void setIsChangeBackgroud(boolean isChangeBackgroud) {
this.isChangeBackgroud = isChangeBackgroud;
}

public boolean getIsChangeBackgroud() {
return isChangeBackgroud;
}
}

 

posted on 2012-01-25 01:10  风里的叶子  阅读(646)  评论(0)    收藏  举报