RandomValidateCode.java
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.FileOutputStream;
import java.util.Random;
import javax.imageio.ImageIO;
/**
* @see https://my.oschina.net/cnzxzc/blog/657088
*
* @author:
* @date: 2016年9月18日 下午12:20:53
*/
public class RandomValidateCode {
private static Random random = new Random();
private static int width = 70;//图片宽
private static int height = 30;//图片高
private static int lineNum = 5;//干扰线数量
private static int pointNum = 30;//噪点数量
private static int stringNum = 4;//随机产生字符数量
private static Font font = new Font("Times New Roman", Font.ROMAN_BASELINE, 20);
private static char[] index = {'a','A','0'};//验证码索引
/**
* 生成验证码字符串
*/
public static char[] getValidateString(){
char[] c = new char[stringNum];
for(int i = 0; i<stringNum; i++) {
int offset = random.nextInt(3);
switch (offset) {
case 0:
case 1:
c[i] = (char) ((int) index[offset] + random.nextInt(26));
break;
case 2:
c[i] = (char) ((int) index[offset] + random.nextInt(10));
break;
}
}
return c;
}
/**
* 生成图片并输出
* */
public static String makeValidateImage(OutputStream out){
//验证码
char[] code = getValidateString();
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g = image.createGraphics();
g.setFont(font);
g.setColor(Color.GREEN);
g.drawRect(0, 0, width, height);
g.setColor(Color.WHITE);
g.fillRect(0, 0, width, height);
//绘制干扰线
drowLine(g);
//绘制噪点
drowPoint(g);
//绘制随机字符
drowString(g, code);
try {
ImageIO.write(image, "JPEG", out);
} catch (Exception e){
e.printStackTrace();
}finally {
g.dispose();
}
return new String(code);
}
/**
* 绘制字符串
* */
private static void drowString(Graphics g, char[] text){
g.setColor(new Color(random.nextInt(101), random.nextInt(111), random.nextInt(121)));
int x = random.nextInt(width);
int y = random.nextInt(height);
//防止画图画出范围
while(x>25){ x -= 20; }
while(y<13){ y += 10; }
g.drawChars(text, 0, stringNum, x, y);
}
/**
* 绘制干扰线
*/
private static void drowLine(Graphics g){
for(int i = 0; i < lineNum; i++) {
g.setColor(new Color(random.nextInt(101), random.nextInt(111), random.nextInt(121)));
int x1 = random.nextInt(width);
int y1 = random.nextInt(height);
int x2 = random.nextInt(width);
int y2 = random.nextInt(height);
g.drawLine(x1, y1, x2, y2);
}
}
/**
* 绘制噪声点
*/
private static void drowPoint(Graphics g){
for(int i = 0; i < pointNum; i++) {
g.setColor(new Color(random.nextInt(101), random.nextInt(111), random.nextInt(121)));
int x = random.nextInt(width);
int y = random.nextInt(height);
g.drawLine(x, y, x, y);
}
}
public static void main(String[] args) {
//验证码
char[] text = getValidateString();
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g = image.createGraphics();
g.setFont(font);
g.setColor(Color.GREEN);
g.drawRect(0, 0, width, height);
g.setColor(Color.WHITE);
g.fillRect(0, 0, width, height);
//绘制干扰线
drowLine(g);
//绘制噪点
drowPoint(g);
//绘制随机字符
drowString(g, text);
try {
ImageIO.write(image, "JPEG", new FileOutputStream("C:/Users/Administrator/Desktop/text.jpg"));
} catch (Exception e){
e.printStackTrace();
}finally {
g.dispose();
}
}
}