1、直接上工具類代碼
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;
import java.util.Random;
/**
* @Auther: liuzujie
* @Date: ***
* @Desc: 生成驗證碼
*/
public class ValidateCodeUtil {
private int width = 160;
private int height = 40;
private int codeCount = 5;
private int lineCount = 150;
private String code = null;
private BufferedImage buffImg = null;
private char[] codeSequence = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
public ValidateCodeUtil() {
createCode();
}
public ValidateCodeUtil(int width, int height) {
this.width = width;
this.height = height;
createCode();
}
public ValidateCodeUtil(int width, int height, int codeCount, int lineCount) {
this.width = width;
this.height = height;
this.codeCount = codeCount;
this.lineCount = lineCount;
createCode();
}
private void createCode() {
int x = 0;
int fontHeight = 0;
int codeY = 0;
int red = 0;
int green = 0;
int blue = 0;
x = this.width / (this.codeCount + 2);
fontHeight = this.height - 2;
codeY = this.height - 4;
this.buffImg = new BufferedImage(this.width, this.height, 1);
Graphics2D g = this.buffImg.createGraphics();
Random random = new Random();
g.setColor(Color.WHITE);
g.fillRect(0, 0, this.width, this.height);
Font font = new Font("Fixedsys", 0, fontHeight);
g.setFont(font);
for (int i = 0; i < this.lineCount; i++) {
int xs = random.nextInt(this.width);
int ys = random.nextInt(this.height);
int xe = xs + random.nextInt(this.width / 8);
int ye = ys + random.nextInt(this.height / 8);
red = random.nextInt(255);
green = random.nextInt(255);
blue = random.nextInt(255);
g.setColor(new Color(red, green, blue));
g.drawLine(xs, ys, xe, ye);
}
StringBuffer randomCode = new StringBuffer();
for (int i = 0; i < this.codeCount; i++) {
String strRand = String.valueOf(this.codeSequence[random.nextInt(this.codeSequence.length)]);
red = random.nextInt(255);
green = random.nextInt(255);
blue = random.nextInt(255);
g.setColor(new Color(red, green, blue));
g.drawString(strRand, (i + 1) * x, codeY);
randomCode.append(strRand);
}
this.code = randomCode.toString();
}
public void write(String path)
throws IOException {
OutputStream sos = new FileOutputStream(path);
write(sos);
}
public void write(OutputStream sos)
throws IOException {
ImageIO.write(this.buffImg, "png", sos);
sos.close();
}
public BufferedImage getBuffImg() {
return this.buffImg;
}
public String getCode() {
return this.code;
}
public static void main(String[] args) { //測試
ValidateCodeUtil vCode = new ValidateCodeUtil(160, 40, 5, 150);
try {
String path = "D:/" + new Date().getTime() + ".png";
System.out.println(vCode.getCode() + " >" + path);
vCode.write(path);
} catch (IOException e) {
e.printStackTrace();
}
}
}
2、提供給前端的接口代碼
@RequestMapping({"/getCaptcha"})
public void captcha(HttpServletRequest request, HttpServletResponse response, HttpSession session)
throws IOException {
response.setDateHeader("Expires", 0L);
response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
response.addHeader("Cache-Control", "post-check=0, pre-check=0");
response.setHeader("Pragma", "no-cache");
response.setContentType("image/jpeg");
ValidateCodeUtil vCode = new ValidateCodeUtil(160, 40, 1, 25);
String code = vCode.getCode();
ServletOutputStream out = response.getOutputStream();
vCode.write(out);
}
3、前端
<div class="img-wrap login-code" οnclick="getValidateCode()"> <img id="canvasValidate" /></div>
function getValidateCode(){ //後面加個時間參數可以解決IE下不刷新的問題
$("#canvasValidate").attr("src",top.global.ctx+"/captcha?"+new Date().getTime());
}