下面是整个代码,main方法里面有用法。里面的代码不全是自己写的,有些是网上找来的。
package *****************;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.Random;
public class VerifyCode {
private int width = 90;
private int heigh = 40;
private Random random = new Random();
private String[] fonts = {"宋体", "微软雅黑", "TimesRoman", "Cambria"};
// private String chars = "123456789abcdefghijklmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ";
private String chars = "123FGHJ49abcrv5yzACstuDEKLMNPQRSwxTUhdef678gimBnojklpqVWXYZ";
private Color backColor = Color.white;
/**
* 获取验证码,
*
* @param length 需要获取的长度
* @return
*/
private String randomValue(int length) {
int randomRang = chars.length();
StringBuilder stringBuilder = new StringBuilder();
for (int n = 0; n < length; n++) {
int index = (int) (Math.random() * randomRang);
stringBuilder.append(chars.charAt(index));
}
return stringBuilder.toString();
}
//获取随机颜色
private Color randomColor() {
int red = random.nextInt(150);
int green = random.nextInt(150);
int blue = random.nextInt(150);
return new Color(red, green, blue);
}
//method:获取随机字体
private Font randomFont() {
int index = random.nextInt(fonts.length);
String fontName = fonts[index];
int style = random.nextInt(4);
int size = 24 + random.nextInt(4);
return new Font(fontName, style, size);
}
/**
* 随机宽度在验证码图片内的一个值
*
* @return
*/
private int randomWidth() {
return (int) (Math.random() * width);
}
/**
* 随机高度在验证码图片内的一个值
*
* @return
*/
private int randomHeight() {
return (int) (Math.random() * heigh);
}
//method:获取随机数字
private String randomNum() {
int index = random.nextInt(chars.length());
return chars.charAt(index) + "";
}
//method:添加干扰线
private void drawLine(BufferedImage image) {
int num = 4;
Graphics2D graphic = (Graphics2D) image.getGraphics();
for (int i = 0; i < num; i++) {
int x1 = random.nextInt(width);
int x2 = random.nextInt(width);
int y1 = random.nextInt(heigh);
int y2 = random.nextInt(heigh);
graphic.setColor(this.randomColor());
graphic.setStroke(new BasicStroke(1.5F));
graphic.drawLine(x1, y1, x2, y2);
}
}
/**
* 添加干扰圆点
*
* @param image
*/
private void drawPoint(BufferedImage image) {
int num = 10;
Graphics2D graphic = (Graphics2D) image.getGraphics();
for (int n = 0; n < num; n++) {
graphic.setColor(this.randomColor());
graphic.fillOval(randomWidth(), randomHeight(), 2, 2);
}
}
//method:绘制方法
private void drawString(Graphics2D graphics, String s, float position) {
graphics.setColor(randomColor());
graphics.setFont(randomFont());
graphics.drawString(s, position, heigh - 10);
}
//获取图片缓存
public BufferedImage getImage(String drawValue) {
BufferedImage bi = new BufferedImage(width, heigh, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = (Graphics2D) bi.getGraphics();
g2.setColor(this.backColor);
g2.fillRect(0, 0, 90, 40);
StringBuilder sb = new StringBuilder();
float p1 = 5.0F;
int distance = width / (drawValue.length()+1);
for (int n = 0; n < drawValue.length(); n++) {
drawString(g2, drawValue.substring(n, n + 1), p1);
p1 += distance;
}
//绘制干扰线
drawLine(bi);
//绘制干扰点
drawPoint(bi);
//返回BufferedImage图片
return bi;
}
//将图片缓存bi输出到指定的输出流out
public static void output(BufferedImage bi, OutputStream out)
throws FileNotFoundException, IOException {
ImageIO.write(bi, "JPEG", out);
}
public static void main(String[] args) {
VerifyCode verifyCode = new VerifyCode();
for (int n = 20; n > 0; n--) {
String code = verifyCode.randomValue(4);
BufferedImage bi = verifyCode.getImage(code);
File file = new File("d:/test/code/" + "n" + n + ".jpeg");
try {
output(bi, new FileOutputStream(file));
} catch (IOException e) {
e.printStackTrace();
}
}
}
}