几个简单使用到的类
BufferedImage : 图像 Graphics2D :图像的上下文 Color : 颜色对象 Font : 字体对象 具体信息大家可以查一下JDK文档。我用的是这个 这个。
具体实现
VerifyCode.class
包含了验证码图片的基本信息,包括但不局限于图片的尺寸、背景颜色、干扰线的数目,干扰线的颜色、验证码的格式、验证码的颜色等等。
import java.awt.*;
import java.util.Random;
public class VerifyCode {
private int width;
private int height;
private int lineCount;
private Color lineColor;
private int lineLength;
private Color bgColor;
private Color codeColor;
private int codeType;
private int angle;
private Font font;
private static Random random;
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public int getLineCount() {
return lineCount;
}
public void setLineCount(int lineCount) {
this.lineCount = lineCount;
}
public Color getBgColor() {
return bgColor;
}
public void setBgColor(Color bgColor) {
this.bgColor = bgColor;
}
public Color getCodeColor() {
return codeColor;
}
public void setCodeColor(Color codeColor) {
this.codeColor = codeColor;
}
public int getCodeType() {
return codeType;
}
public void setCodeType(int codeType) {
this.codeType = codeType;
}
public int getAngle() {
return angle;
}
public void setAngle(int angle) {
this.angle = angle;
}
public Font getFont() {
return font;
}
public void setFont(Font font) {
this.font = font;
}
public static Random getRandom() {
return random;
}
public static void setRandom(Random random) {
VerifyCode.random = random;
}
public Color getLineColor() {
return lineColor;
}
public void setLineColor(Color lineColor) {
this.lineColor = lineColor;
}
public int getLineLength() {
return lineLength;
}
public void setLineLength(int lineLength) {
this.lineLength = lineLength;
}
static {
random = new Random();
}
public VerifyCode() {
}
public VerifyCode(int width, int height) {
this.width = width;
this.height = height;
}
public VerifyCode(int width, int height, int lineCount) {
this.width = width;
this.height = height;
this.lineCount = lineCount;
}
public VerifyCode(int width, int height, int lineCount , int codeType) {
this.width = width;
this.height = height;
this.lineCount = lineCount;
this.codeType = codeType;
}
public VerifyCode(int width, int height, int lineCount , int codeType , int angle) {
this.width = width;
this.height = height;
this.lineCount = lineCount;
this.codeType = codeType;
this.angle = angle;
}
public VerifyCode(int width, int height, int lineCount, int codeType, int angle, Font font) {
this.width = width;
this.height = height;
this.lineCount = lineCount;
this.codeType = codeType;
this.angle = angle;
this.font = font;
}
public VerifyCode(int width, int height, int lineCount, Color bgColor) {
this.width = width;
this.height = height;
this.lineCount = lineCount;
this.bgColor = bgColor;
}
public VerifyCode(int width, int height, int lineCount, Color bgColor, Color codeColor) {
this.width = width;
this.height = height;
this.lineCount = lineCount;
this.bgColor = bgColor;
this.codeColor = codeColor;
}
public VerifyCode(int width, int height, int lineCount, Color bgColor, Color codeColor, int codeType) {
this.width = width;
this.height = height;
this.lineCount = lineCount;
this.bgColor = bgColor;
this.codeColor = codeColor;
this.codeType = codeType;
}
public VerifyCode(int width, int height, int lineCount, Color bgColor, Color codeColor, int codeType, int angle) {
this.width = width;
this.height = height;
this.lineCount = lineCount;
this.bgColor = bgColor;
this.codeColor = codeColor;
this.codeType = codeType;
this.angle = angle;
}
public VerifyCode(int width, int height, int lineCount, Color bgColor, Color codeColor, int codeType, int angle, Font font) {
this.width = width;
this.height = height;
this.lineCount = lineCount;
this.bgColor = bgColor;
this.codeColor = codeColor;
this.codeType = codeType;
this.angle = angle;
this.font = font;
}
}
VerifyCodeUtil.class
该类是验证码图片的工具类,用于生成验证码图片,也可以将生成的验证码图片以输出流的方式输出到文件或者相应的response中。
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;
public class VerifyCodeUtil {
public static BufferedImage getVerifyCodeImage(VerifyCode verifyCode) {
if (verifyCode == null)
return null;
if(verifyCode.getWidth() <=105 || verifyCode.getHeight() <= 35) {
verifyCode.setWidth(105);
verifyCode.setHeight(35);
}
int width = verifyCode.getWidth();
int height = verifyCode.getHeight();
final BufferedImage bufferedImage = new BufferedImage(width , height, BufferedImage.TYPE_INT_RGB);
final Graphics2D graphics2D = (Graphics2D) bufferedImage.getGraphics();
final Random random = VerifyCode.getRandom();
if(verifyCode.getBgColor() != null) {
graphics2D.setColor(verifyCode.getBgColor());
}else {
graphics2D.setColor(Color.WHITE);
}
graphics2D.fillRect(0 , 0 , verifyCode.getWidth() , verifyCode.getHeight());
graphics2D.drawRect(0 , 0 , verifyCode.getWidth()-1 , verifyCode.getHeight() - 1);
int count = verifyCode.getLineCount();
Color lineColor = verifyCode.getLineColor();
int lineLength = verifyCode.getLineLength();
if (lineLength == 0)
lineLength = 1;
if(count == 0) {
count = 20;
}
for (int i =0; i <count; i++) {
if(lineColor != null) {
graphics2D.setColor(lineColor);
}else {
graphics2D.setColor(getRandomColor(random));
}
int x = random.nextInt(width - 1 - 1);
int y = random.nextInt(height - 1 -1);
int xEnd = x + random.nextInt(lineLength);
int yEnd = y + random.nextInt(lineLength);
graphics2D.drawLine(x, y , xEnd, yEnd);
}
final String code = getRandomCode(random , 0);
Color fontColor = verifyCode.getCodeColor();
Font font = verifyCode.getFont();
int angle = verifyCode.getAngle();
int rotateAngle = 0;
for(int i =0; i < code.length(); i++) {
if(fontColor == null) {
graphics2D.setColor(getRandomColor(random));
}else {
graphics2D.setColor(fontColor);
}
if (font == null) {
graphics2D.setFont(getRandomFont(random , height));
}else{
graphics2D.setFont(font);
}
int x = (width/5)*i + width/5;
if(angle != 0) {
rotateAngle = random.nextInt(angle) - angle/2;
graphics2D.rotate(Math.toRadians(rotateAngle) ,x , height/2);
}
graphics2D.drawString(String.valueOf(code.charAt(i)) , x , height/2);
if(angle != 0) {
graphics2D.rotate(-Math.toRadians(rotateAngle) ,x , height/2);
}
}
graphics2D.dispose();
return bufferedImage;
}
public static Color getRandomColor(Random random) {
return new Color(random.nextInt(255) , random.nextInt(255), random.nextInt(255));
}
public static String getRandomCode(Random random , int type) {
String targetString = "QWERTYUIOPASDFGHJKLZXCVBNMqwertyuioplkjhgfdsazxcvbnm0123456789";
StringBuilder stringBuilder = new StringBuilder();
int base = 0;
int length = 51;
if(type == 1) {
length = 9;
base = 52;
}
if(type == 2) {
length = 61;
}
for(int i =0; i < 4; i++) {
stringBuilder.append(targetString.charAt(random.nextInt(length) + base));
}
return stringBuilder.toString();
}
public static Font getRandomFont(Random random , int height) {
return new Font("楷体" , Font.BOLD , random.nextInt(20) + height/3);
}
public static void storeImagetoF(BufferedImage image , OutputStream out) throws IOException {
ImageIO.write(image ,"JPEG", out);
}
}
UtilTest.class
简单测试类
import java.awt.*;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
public class UtilTest {
public static void main(String[] args) {
VerifyCode verifyCode = new VerifyCode(300 , 100 , 200 , Color.WHITE , Color.BLACK, 2 , 45);
for (int i = 0; i < 5; i++) {
try{
Thread.sleep(1000);
String imageName = getNowDate();
String totalPath = "F:\\学习记录\\image\\" + imageName +".jpg";
VerifyCodeUtil.storeImagetoF(VerifyCodeUtil.getVerifyCodeImage(verifyCode) , new FileOutputStream(totalPath));
}catch (Exception exception){
exception.printStackTrace();
}
}
}
public static String getNowDate() {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
Date date = new Date(System.currentTimeMillis());
return simpleDateFormat.format(date);
}
}
测试结果
默认生成的验证码文件,由于默认是宽是一百,高是三十,所以放大后会模糊。 自定义数据生成为验证码图片
|