前端
<input id="Id" type="text" class="form-control" placeholder="请输入验证码...">
<img id="verifyCode" src="/getVerifyCode.do" οnclick="refersh()"/>
<script>
function refersh() {
var date = new Date(); // 创建一个 Date 对象的 一个 实例
$("#verifyCode")[0].src = "/getVerifyCode.do?" + date.getTime();
}
</script>
后台
@RequestMapping("getVerifyCode.do")
public void getVerifyCode(HttpServletRequest request, HttpServletResponse response){
try {
HttpSession session = request.getSession();
String verifyCode = VerifyCode.createVerifyCode(response.getOutputStream());
session.setAttribute(BaseContext.session_yzm, verifyCode);
} catch (IOException e) {
logWrite.initLog();
logWrite.writeLog(e.getMessage());
logWrite.closeLog(LogPath.debugLog);
}
}
Util
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;
import javax.imageio.ImageIO;
public class VerifyCode {
/**
* 生成验证码
* @param width
* @param height
* @param imgType
* @param output
* @return
*/
public static String createVerifyCode(OutputStream output){
String imgType = "jpg";
int width = 100;
int height = 30;
int times = 4;
width = width*times;
height = height*times;
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D graphic = (Graphics2D)image.getGraphics();
graphic.setColor(getColor(180, 240));
graphic.fillRect(0, 0, width, height);
// 在 "画板"上生成干扰线条 ( 50 是线条个数)
/*for (int i = 0; i < 50; i++) {
graphic.setColor(colors[random.nextInt(colors.length)]);
final int x = random.nextInt(width);
final int y = random.nextInt(height);
final int w = random.nextInt(20);
final int h = random.nextInt(20);
final int signA = random.nextBoolean() ? 1 : -1;
final int signB = random.nextBoolean() ? 1 : -1;
graphic.drawLine(x, y, x + w * signA, y + h * signB);
}*/
// 在 "画板"上绘制字母
//graphic.setFont(new Font("Comic Sans MS", Font.BOLD, 30));
graphic.setFont(new Font("SimHei", Font.BOLD, 25*times));
int length = 4;
String verifyCode = getVerifyCode(length);//获取验证码
for (int i = 1; i <= length; i++) {
graphic.setColor(getColor(50, 160));//字体颜色
int x = width / (length + 1) * i;
int y = height / 2 + 25;
//设置旋转角度和坐标原点
graphic.translate(x, y);
graphic.drawString(String.valueOf(verifyCode.charAt(i-1)), 0, 0);
//恢复旋转角度和坐标原点
graphic.translate(-x, -y);
}
graphic.dispose();
try {
ImageIO.write(image, imgType, output);//输出到前端字符
} catch (IOException e) {
e.printStackTrace();
}
return verifyCode;
}
/**
* 获取验证码
* @return
*/
public static String getVerifyCode(int length){
StringBuffer verifyCode = new StringBuffer();
String container = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
Random random = new Random();
for(int i=0; i<length; i++){
verifyCode.append(container.charAt(random.nextInt(container.length())));
}
return verifyCode.toString();
}
/**
* 获取颜色
* @param min
* @param max
* @return
*/
public static Color getColor(int min, int max){
return new Color(getRandom(min, max), getRandom(min, max), getRandom(min, max));
}
/**
* 获取随机数
* @param min
* @param max
* @return
*/
public static int getRandom(int min, int max){
return (int)Math.floor(Math.random()*(max-min)+min);
}
public static void main(String[] args) {
try {
createVerifyCode(new FileOutputStream("D:\\vertifyCode.jpg"));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
效果