import org.testng.annotations.Test;
import javax.imageio.ImageIO;
import javax.imageio.stream.ImageOutputStream;
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Random;
public class verifyCodeTool {
// 图片高度
private static int height ;
//图片宽度
private static int width ;
// 验证码字符串
private static String code = "";
// 验证码字体
private static String fontStyle = "Microsoft Yahei";
// 内容个数
private static int wordNumber = 6;
// 背景颜色范围
private static int[] backgroundColorRange ={230,255};
// 内容颜色范围
private static int[] contentColorRange = {100,150};
private verifyCodeTool(int width, int height) {
verifyCodeTool.height = height;
verifyCodeTool.width = width;
}
/**
* 默认宽高为 宽 150,高 50
* @return 返回验证码对象
*/
public static verifyCodeTool getInstance(){
return new verifyCodeTool(150,50);
}
/**
* 重载自定义 宽 高
* @param width 宽
* @param height 高
* @return
*/
public static verifyCodeTool getInstance(int width,int height){
return new verifyCodeTool(width,height);
}
/**
* 设置图片的宽高
* @param width
* @param height
* @return
*/
public verifyCodeTool setHeight(int width,int height) {
verifyCodeTool.height = height;
verifyCodeTool.width = width;
return this;
}
/**
* 设置字体 默认字体为 "Microsoft Yahei"
* @param fontStyle
* @return
*/
public verifyCodeTool setFontStyle(String fontStyle) {
verifyCodeTool.fontStyle = fontStyle;
return this;
}
/**
* 设置内容长度
* @param wordNumber
* @return
*/
public verifyCodeTool setWordNumber(int wordNumber) {
verifyCodeTool.wordNumber = wordNumber;
return this;
}
/**
* 设置背景颜色的取值范围
* @param min
* @param max
* @return
*/
public verifyCodeTool setBackgroundColorRange(int min,int max) {
verifyCodeTool.backgroundColorRange[0] = min;
verifyCodeTool.backgroundColorRange[1] = max;
return this;
}
/**
* 设置内容颜色的取值范围
* @param min
* @param max
* @return
*/
public verifyCodeTool setContentColorRange(int min, int max) {
verifyCodeTool.contentColorRange[0] = min;
verifyCodeTool.contentColorRange[1] = max;
return this;
}
/**
* 初始化验证码组件
*/
private static ByteArrayInputStream init() throws IOException {
//创建一个图像缓冲对象
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
// 得到画笔工具
Graphics2D graphics = (Graphics2D)image.getGraphics();
// 设置画笔颜色
graphics.setColor(getRandColor(backgroundColorRange[0],backgroundColorRange[1]));
// 画一个矩形的验证码背景 (填充矩形 fillRect)
graphics.fillRect(0,0,width,height);
// 然后填充内部内容
Random random = new Random();
String[] contData = {"A","B","C","D","E","F","G","H","J","K","M","N","P","Q","R",
"S","T","U","V","W","X","Y","Z","a","b","c","d","e","f","g","h","j","k",
"m","n","p","q","r", "s","t","u","v","w","x","y","z","1","2","3","4","5",
"6","7","8","9","0"};
List<String> strings = Arrays.asList(contData);
// 生成验证码字符内容
for (int i = 0; i < wordNumber;i++ ) {
// 设置字体样式
Font font = new Font(fontStyle, Font.BOLD, width / 6 + random.nextInt(15));
graphics.setFont(font);
graphics.setColor(getRandColor(contentColorRange[0],contentColorRange[1]));
//打乱集合的内容顺序
Collections.shuffle(strings);
int index = random.nextInt(strings.size()-1);
String s = strings.get(index);
code += s;
// 设置随机旋转角度
AffineTransform affineTransform = new AffineTransform();
affineTransform.rotate(Math.toRadians(random.nextInt(70)-35), 0, 0);
Font rotatedFont = font.deriveFont(affineTransform);
graphics.setFont(rotatedFont);
// 将内容画入画布
graphics.drawString(s,(width/wordNumber)*i+random.nextInt(7),height/2 + random.nextInt(7));
}
// 添加杂色干扰
for (int i = 0; i < 10+random.nextInt(10);i++ ) {
// 设置颜色
graphics.setColor(getRandColor(contentColorRange[0],contentColorRange[1]));
int i1 = random.nextInt(width);
int i2 = random.nextInt(height);
graphics.drawLine(i1,i2,i1+random.nextInt(20),i2+random.nextInt(20));
graphics.drawOval(random.nextInt(width),random.nextInt(height),5+random.nextInt(20),5+random.nextInt(20));
}
graphics.dispose();
ByteArrayInputStream byteArrayInputStream = null;
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ImageOutputStream imageOut = ImageIO.createImageOutputStream(byteArrayOutputStream);
ImageIO.write(image, "JPEG",imageOut);
imageOut.close();
// FileOutputStream fileOutputStream = new FileOutputStream("d://xxx.jpeg");
// fileOutputStream.write(byteArrayOutputStream.toByteArray());
// fileOutputStream.close();
byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
return byteArrayInputStream;
}
/**
* 返回验证码图片流
* @return
* @throws IOException
*/
public ByteArrayInputStream getImage() {
try {
return init();
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
/**
* 得到验证码数据
* @return
*/
public String getCode() {
return code;
}
/**
* 获得一个随机的背景色
*/
private static Color getRandColor(int min, int max) {
// 获取随机数
Random random = new Random();
// 判断设置色值不能超过255
if (min > 255) {min = 255;};
if (max > 255) {max = 255;};
int r = min + random.nextInt(max - min);
int g = min + random.nextInt(max - min);
int b = min + random.nextInt(max - min);
return new Color(r,g,b);
}
}
生成输出测试
package com.wang.tools;
import java.io.ByteArrayInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class test {
public static void main(String[] args) throws IOException {
verifyCodeTool instance = verifyCodeTool.getInstance();
ByteArrayInputStream image = instance.getImage();
String code = instance.getCode();
System.out.println(code);
}
}