package mawen.util;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.util.Random;
public class ValidCode {
/*
* px2pt:像素(px)转换为磅(pt)的系数,Windows缺省为0.75;MAC缺省为1 validString:验证字符串
*/
private double px2pt;
private String fontName;
private String validString;
private int fontSize;
private int margin;
private int padding;
private int length;
private int offset;
private int interference;
private int foreground_low;
private int foreground_high;
private int background_low;
private int background_high;
final static private char[] CHARSET = { '0', '1', '2', '3', '4', '5', '6',
'7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K',
'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
final static private int CHARSET_SIZE = CHARSET.length;
public ValidCode() {
reset();
}
/*
* 生成随机数字字符串
*/
public static String getRandomID(int length) {
StringBuffer sb = new StringBuffer();
Random random = new Random();
for (int i = 0; i < length; i++) {
sb.append(CHARSET[random.nextInt(CHARSET_SIZE)]);
}
return sb.toString();
}
public String getRandomID() {
return getRandomID(this.length);
}
/*
* 生成(绘画)验证码图像
*/
public BufferedImage getImage(final String str) {
int length = str.length();
int width = this.getWidth(length);
int height = this.getHeight();
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics(); // 获取图形上下文
g.setColor(getBackgroundColor()); // 设定背景色
g.fillRect(0, 0, width, height);
g.setFont(new Font(this.fontName, Font.BOLD, this.fontSize)); // 设定字体
lineInterfere(g, width, height); // 加上干扰纹
for (int i = 0; i < length; i++) { // 绘画认证码
g.setColor(getForegroundColor());
g.drawString(str.substring(i, i + 1), (int) (this.margin + i
* (this.padding + this.fontSize / this.px2pt)), height
- this.margin);
}
lineInterfere(g, width, height); // 加上干扰纹
g.dispose(); // 图象生效
return image;
}
public BufferedImage getImage() {
if ((this.validString == null) || this.validString.equalsIgnoreCase(""))
this.validString = getRandomID(this.length);
return getImage(this.validString);
}
/*
* 生成随机前景颜色
*/
private Color getForegroundColor() {
return getRandomColor(foreground_low, foreground_high);
}
/*
* 生成随机背景颜色
*/
private Color getBackgroundColor() {
return getRandomColor(background_low, background_high);
}
/*
* 生成随机的RGB颜色
*/
protected static Color getRandomColor(int low, int high) {
low = (low < 0) ? 0 : low;
low = (low > 255) ? 255 : low;
high = (high < low) ? low : high;
high = (high > 255) ? 255 : high;
Random random = new Random();
int r = low + random.nextInt(high - low);
int g = low + random.nextInt(high - low);
int b = low + random.nextInt(high - low);
return new Color(r, g, b);
}
/*
* 随机产生多条干扰线,使图象中的认证码不易被其它程序探测到
*/
private void lineInterfere(Graphics g, int width, int height) {
Random random = new Random();
for (int i = 0; i < interference; i++) {
int x = random.nextInt(width);
int y = random.nextInt(height);
int xl = random.nextInt(width);
int yl = random.nextInt(height);
g.setColor(getBackgroundColor());
g.drawLine(x, y, x + xl, y + yl);
}
}
/*
* 重新设置类参数为缺省值
*/
public void reset() {
this.px2pt = 1.2;
this.fontName = "Arial";
this.fontSize = 14;
this.margin = 4;
this.padding = 0;
this.offset = 0;
this.foreground_low = 0;
this.foreground_high = 112;
this.background_low = 204;
this.background_high = 256;
this.interference = 7;
this.length = 4;
}
public int getBackground_high() {
return background_high;
}
public void setBackground_high(int background_high) {
this.background_high = background_high;
}
public String getFontName() {
return fontName;
}
public void setFontName(String fontName) {
this.fontName = fontName;
}
public int getFontSize() {
return fontSize;
}
public void setFontSize(int fontSize) {
this.fontSize = fontSize;
}
public int getLength() {
return length;
}
public void setLength(int length) {
this.length = length;
}
public int getMargin() {
return margin;
}
public void setMargin(int margin) {
this.margin = margin;
}
public int getHeight() {
return (int) (2 * this.margin + this.fontSize / this.px2pt);
}
public int getWidth() {
return this.getWidth(this.length);
}
protected int getWidth(int lenght) {
return (int) (this.offset + 2 * this.margin + length
* (this.padding + this.fontSize / this.px2pt));
}
public void setBackground_low(int background_low) {
this.background_low = background_low;
}
public void setForeground_high(int foreground_high) {
this.foreground_high = foreground_high;
}
public void setForeground_low(int foreground_low) {
this.foreground_low = foreground_low;
}
public void setInterference(int interference) {
this.interference = interference;
}
public int getPadding() {
return padding;
}
public void setPadding(int padding) {
this.padding = padding;
}
public String getValidString() {
return validString;
}
public void setValidString(String validString) {
this.validString = validString.trim();
this.length = this.validString.length();
}
public int getOffset() {
return offset;
}
public void setOffset(int offset) {
this.offset = offset;
}
public double getPx2pt() {
return this.px2pt;
}
public void setPx2pt(double px2pt) {
this.px2pt = px2pt;
}
}
评论0
最新资源