java web 验证码-数字不变形
controller代码:
import java.awt.Color; import java.awt.Font; import java.awt.Graphics2D; import java.awt.image.BufferedImage; import java.io.IOException; import java.util.Random; import javax.imageio.ImageIO; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller @RequestMapping("randomCheckCode") public class RandomCheckCodeController { // 验证码图片的宽度。 private int width = 60; // 验证码图片的高度。 private int height = 20; // 验证码字符个数 private int codeCount = 4; // 字体高度 private int fontHeight; //字体的x坐标 private int x = 0; private int y = 0; private static Random random = new Random();
//去除0、1、i、l等容易混淆字符 public static final String VERIFY_CODES = "23456789abcdefghjkmnprstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ"; private static Color getRandColor(int fc, int bc) { if (fc > 255) fc = 255; if (bc > 255) bc = 255; int r = fc + random.nextInt(bc - fc); int g = fc + random.nextInt(bc - fc); int b = fc + random.nextInt(bc - fc); return new Color(r, g, b); } /** * 初始化图片属性 */ public void init() { x = width / (codeCount ); fontHeight = height - 2; y = height - 4; } @RequestMapping("generate") public void generateNumber(HttpServletRequest request, HttpServletResponse resp) { init(); // 定义图像buffer BufferedImage buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics2D g = buffImg.createGraphics(); // 填充背景色 // g.setColor(getRandColor(200, 250)); g.setColor(Color.WHITE); g.fillRect(0, 0, width, height); // 创建字体,字体的大小应该根据图片的高度来定 Font font = new Font("Fixedsys", Font.PLAIN, fontHeight); // 设置字体 g.setFont(font); // 画一个边框 g.setColor(Color.GRAY); g.drawRect(0, 0, width - 1, height - 1); // 随机产生160条干扰线,使图象中的验证码不易被其它程序探测到 g.setColor(Color.BLACK); for (int i = 0; i < 10; i++) { int x = random.nextInt(width); int y = random.nextInt(height); int xl = random.nextInt(12); int yl = random.nextInt(12); g.drawLine(x, y, x + xl, y + yl); } // randomCode用于保存随机产生的验证码,以便用户登录后进行验证 StringBuffer randomCode = new StringBuffer(); int red = 0, green = 0, blue = 0; // 随机产生codeCount数字的验证码。 for (int i = 0; i < codeCount; i++) { // 得到随机产生的验证码数字 char ch = VERIFY_CODES.charAt(random.nextInt(VERIFY_CODES.length())); // 产生随机的颜色分量来构造颜色值,这样输出的每位数字的颜色值都将不同 red = random.nextInt(255); green = random.nextInt(255); blue = random.nextInt(255); // 用随机产生的颜色将验证码绘制到图像中 g.setColor(new Color(red, green, blue)); g.drawString(String.valueOf(ch), i*x+1, y); // 将产生的四个随机数组合在一起 randomCode.append(ch); } // 将四位数字的验证码保存到Session中 HttpSession session = request.getSession(); session.setAttribute("validateCode", randomCode.toString()); // 禁止图像缓存 resp.setHeader("Pragma", "no-cache"); resp.setHeader("Cache-Control", "no-cache"); resp.setDateHeader("Expires", 0); resp.setContentType("image/jpeg"); // 将图像输出到Servlet输出流中 try { ServletOutputStream sos = resp.getOutputStream(); ImageIO.write(buffImg, "jpeg", resp.getOutputStream()); sos.close(); } catch (IOException e) { e.printStackTrace(); } } }
jsp代码:
<img src="${ctx }/randomCheckCode/generate1?t=1" id="CreateCheckCode" align="middle" onclick="reloadPic()"> <script type="text/javascript"> function reloadPic() { document.getElementById("checkCode").src = document.getElementById("checkCode").src + "1"; } </script>
效果: