package com.swu.scm.util;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import javax.imageio.ImageIO;
public class VCodeGenerator {
// 定义随机选取的字符
final private char[] chars = "2345678ABCDEFGHJKLMPQRSTUVWXYabcdefhkmnqrstuvwx"
.toCharArray();
// 定义随机选取的字体
private static String[] fontNames = new String[] { "Courier", "Arial",
"Verdana", "Georgia", "Times", "Tahoma" };
// 定义随机选取的字体格式
private static int[] fontStyle = new int[] { Font.PLAIN, Font.BOLD,
Font.ITALIC, Font.BOLD | Font.ITALIC };
private int width = 160; // 图片的长度
private int height = 60; // 图片的高度
private int charCnt = 4; // 图片的字符数
private int disturbLineNum = 10; //干扰线数量
private OutputStream os; // 输出流
public VCodeGenerator(OutputStream os) {
this.os = os;
}
public VCodeGenerator(int width, int height, int charCnt, OutputStream os) {
this.width = width;
this.height = height;
this.charCnt = charCnt;
this.os = os;
}
public void drawCode() throws IOException {
BufferedImage bi = new BufferedImage(this.width, this.height,
BufferedImage.TYPE_INT_RGB);
Graphics2D g = bi.createGraphics();
g.setColor(new Color(245, 245, 245));
g.fillRect(0, 0, width, height);
//画干扰线
drawDisturbLine(g);
BufferedImage[] bis = new BufferedImage[charCnt];
//随机取得四个字符
char[] codes = generateCode();
//将该矩形分成四个小的正方形框 每个框画一个字符
for(int i = 0; i < charCnt; i++) {
bis[i] = generateBuffImg(codes[i]);
g.drawImage(bis[i], null, (int) ((this.height * 0.6) * i), 0);
}
g.dispose();
ImageIO.write(bi, "gif", os);
}
//在一个小矩形框内画字符
public BufferedImage generateBuffImg(char c) {
String temp = Character.toString(c);
//随机取得一个字体的颜色
Color forecolor = getRandomColor();
Color backcolor = new Color(255, 255, 255, 0);
//随机获得字体
String fontName = getRandomFont();
//获得字体样式
int fontStyle = getRandomStyle();
//随机字休大小
int fontSize = getRandomSize();
int strX = (this.height - fontSize) / 2;
int strY = (this.height - fontSize) / 2 + fontSize;
//旋转中心位置
double arch = getRandomArch();
BufferedImage ret = new BufferedImage(this.height, this.height, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = ret.createGraphics();
//画小方形
g.setColor(backcolor);
g.fillRect(0, 0, this.height, this.height);
//写入字符
g.setColor(forecolor);
g.setFont(new Font(fontName, fontStyle, fontSize));
g.rotate(arch, this.height / 2, this.height / 2);
g.drawString(temp, strX, strY);
g.dispose();
return ret;
}
private void drawDisturbLine(Graphics2D graphics) {
for (int i = 0; i < disturbLineNum; i++) {
graphics.setColor(getRandomColor());
int x = (int) (Math.random() * 10000) % (this.width + 1) + 1;
int x1 = (int) (Math.random() * 10000) % (this.width + 1) + 1;
int y = (int) (Math.random() * 10000) % (this.height + 1) + 1;
int y1 = (int) (Math.random() * 10000) % (this.height + 1) + 1;
graphics.drawLine(x, y, x1, y1);
}
}
//旋转中心位置
private double getRandomArch() {
return ((int) (Math.random() * 1000) % 2 == 0 ? -1 : 1) * Math.random();
}
private int getRandomSize() {
int max = (int) (this.height * 0.9);
int min = (int) (this.height * 0.6);
return (int) (Math.random() * 1000 % (max - min + 1) + min);
}
//获得字体样式
private int getRandomStyle() {
int stypePos = (int) ((Math.random() * 1000) % fontStyle.length);
return fontStyle[stypePos];
}
//随机获得字体
private String getRandomFont() {
int fontPos = (int) ((Math.random() * 1000) % fontNames.length);
return fontNames[fontPos];
}
//随机取得一个字体的颜色
private Color getRandomColor() {
int r = (int) ((Math.random() * 1000) % 200);
int g = (int) ((Math.random() * 1000) % 200);
int b = (int) ((Math.random() * 1000) % 200);
return new Color(r, g, b);
}
//随机取得四个字符
public char[] generateCode() {
char[] ret = new char[charCnt];
for(int i = 0; i < charCnt; i++) {
int letterPos = (int) ((Math.random() * 1000) % chars.length);
ret[i] = chars[letterPos];
}
return ret;
}
public static void main(String[] args) throws IOException {
FileOutputStream fos = new FileOutputStream("c://temp.gif");
new VCodeGenerator(fos).drawCode();
}
}
[/color]