動態(tài)生成圖片驗證碼
參數(shù)設置:
可選配置
- 圖片的寬度(width)
- 圖片高度(height)
- 圖片背景顏色(bgColor)
- 驗證碼個數(shù)(textSize)
默認值:
width:70
height:35
bgColor:white
textSize:4
使用方式:
DynamicGenerateImageUtil imageUtil = new DynamicGenerateImageUtil.Builder().build();
// outputFilePath 文件輸出路徑
imageUtil.output(new FileOutputStream(new File(outputFilePath)));
package com.hzy.util;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;
/**
* 動態(tài)生成圖片驗證碼
*/
public final class DynamicGenerateImageUtil {
/**
* 圖片寬度
*/
private static int w = 70;
/**
* 圖片高度
*/
private static int h = 35;
/**
* 隨機源
*/
private static Random r = new Random();
/**
* 可選字體
*/
private static String[] fontNames = {"宋體", "華文楷體", "黑體", "微軟雅黑", "楷體_GB2312"};
/**
* 可選字符
*/
private static String codes = "23456789abcdefghjkmnopqrstuvwxyzABCDEFGHJKMNOPQRSTUVWXYZ";
/**
* 背景色
*/
private static Color bgColor = new Color(255, 255, 255);
/**
* 驗證碼上的文本
*/
private static String text;
/**
* 驗證碼的個數(shù)
*/
private static int textSize = 4;
public DynamicGenerateImageUtil(Builder builder) {
w = builder.width > 0 ? builder.width : w;
h = builder.height > 0 ? builder.height : h;
bgColor = builder.bgColor;
textSize = builder.textSize;
}
public static class Builder {
private int textSize;
private int width;
private int height;
private Color bgColor;
public Builder textSize(int codeSize) {
this.textSize = codeSize;
return this;
}
public Builder width(int width) {
this.width = width;
return this;
}
public Builder height(int height) {
this.height = height;
return this;
}
public Builder bgColor(Color bgColor) {
this.bgColor = bgColor;
return this;
}
public DynamicGenerateImageUtil build() {
return new DynamicGenerateImageUtil(this);
}
}
/**
* 生成隨機的顏色
*
* @return
*/
private static Color randomColor() {
int red = r.nextInt(150);
int green = r.nextInt(150);
int blue = r.nextInt(150);
return new Color(red, green, blue);
}
/**
* 生成隨機的字體
*
* @return
*/
private static Font randomFont() {
int index = r.nextInt(fontNames.length);
//生成隨機的字體名稱
String fontName = fontNames[index];
//生成隨機的樣式:0,無樣式。1,粗體。2,斜體。3,粗體+斜體
int style = r.nextInt(4);
//生成隨機字號,24-28
int size = r.nextInt(4) + 24;
return new Font(fontName, style, size);
}
private static void drawLine(BufferedImage image) {
//一共畫三條
int num = 3;
Graphics2D g2 = (Graphics2D) image.getGraphics();
//生成兩個點的坐標.即4個點的值。
for (int x = 0; x < num; x++) {
int x1 = r.nextInt(w);
int y1 = r.nextInt(h);
int x2 = r.nextInt(w);
int y2 = r.nextInt(h);
g2.setStroke(new BasicStroke(1.5F));
//干擾線是藍色
g2.setColor(Color.BLUE);
g2.drawLine(x1, y1, x2, y2);
}
}
/**
* 隨機生成一個字符
*
* @return
*/
private static char randomChar() {
int index = r.nextInt(codes.length());
return codes.charAt(index);
}
/**
* 創(chuàng)建BufferedImage
*
* @return
*/
private static BufferedImage createImage() {
BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_BGR);
Graphics2D g2 = (Graphics2D) image.getGraphics();
g2.setColor(bgColor);
g2.fillRect(0, 0, w, h);
return image;
}
/**
* 對外提供獲取驗證碼的方法
*/
private static BufferedImage getImage() {
//創(chuàng)建圖片緩沖區(qū)
BufferedImage image = createImage();
//得到繪制環(huán)境
Graphics2D g2 = (Graphics2D) image.getGraphics();
//用來裝載生成的驗證碼文本
StringBuilder sb = new StringBuilder();
//循環(huán)codeSize次,每次生成一個字符
for (int x = 0; x < textSize; x++) {
//隨機生成一個字母
String s = randomChar() + "";
//把字母添加到sb中
sb.append(s);
//設置當前字符的x軸坐標
float f = x * 1.0F * w / textSize;
//設置隨機字體
g2.setFont(randomFont());
//設置隨機顏色
g2.setColor(randomColor());
//畫圖
g2.drawString(s, f, h - 5);
}
//把生成的字符串賦給this.text
text = sb.toString();
//添加干擾線
drawLine(image);
return image;
}
/**
* 返回驗證碼圖片上的文本
*
* @return
*/
public String getText() {
return text;
}
/**
* 保存圖片到指定的數(shù)出流
*/
public void output(OutputStream out) throws IOException {
ImageIO.write(getImage(), "JPEG", out);
}
}