验证码代码如下
package com.tshn.checkwork.controller.system; import java.awt.BasicStroke; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.geom.AffineTransform; import java.awt.image.BufferedImage; import java.io.IOException; import java.util.Random; import javax.imageio.ImageIO; import javax.servlet.ServletException; 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("/checkCodeAction/") public class CheckCodeController { @RequestMapping("service") public void service(HttpSession session, HttpServletResponse response) throws IOException { // 禁止缓存 response.setHeader("Pragma", "No-cache"); response.setHeader("Cache-Control", "No-cache"); response.setDateHeader("Expires", 0); // 指定生成的响应是图片 response.setContentType("image/jpeg"); int fontSize = 25; //int width = 100; int width = fontSize*4+10; int height = 41; BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); // 创建BufferedImage类的对象 Graphics g = image.getGraphics(); // 创建Graphics类的对象 Graphics2D g2d = (Graphics2D) g; // 通过Graphics类的对象创建一个Graphics2D类的对象 Random random = new Random(); // 实例化一个Random对象 Font mFont = new Font("华文宋体", Font.BOLD, fontSize); // 通过Font构造字体 //g.setColor(Color.WHITE); // 改变图形的当前颜色为随机生成的颜色 g.setColor(getRandColor(200,250)); g.fillRect(0, 0, width, height); // 绘制一个填色矩形 // 画一条折线 BasicStroke bs = new BasicStroke(1f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL); // 创建一个供画笔选择线条粗细的对象 g2d.setStroke(bs); // 改变线条的粗细 g.setColor(Color.DARK_GRAY); // 设置当前颜色为预定义颜色中的深灰色 int[] xPoints = new int[3]; int[] yPoints = new int[3]; for (int j = 0; j < 3; j++) { xPoints[j] = random.nextInt(width - 1); yPoints[j] = random.nextInt(height - 1); } g.drawPolyline(xPoints, yPoints, 3); // 生成并输出随机的验证文字 g.setFont(mFont); char[] chs = { '1', 'l', '0', 'O', 'o', 'L' }; String sRand = ""; int itmp = 0; for (int i = 0; i < 4; i++) { if (random.nextInt(2) == 1) { itmp = random.nextInt(26) + 65; // 生成A~Z的字母 } else { itmp = random.nextInt(10) + 48; // 生成0~9的数字 } boolean canAdd = true; for (char c : chs) { if (c == itmp) { i = i - 1; canAdd = false; } } if (canAdd == true) { char ctmp = (char) itmp; sRand += String.valueOf(ctmp); Color color = new Color(20 + random.nextInt(110), 20 + random.nextInt(110), 20 + random.nextInt(110)); g.setColor(color); /**** 随机缩放文字并将文字旋转指定角度 **/ // 将文字旋转指定角度 Graphics2D g2d_word = (Graphics2D) g; AffineTransform trans = new AffineTransform(); trans.rotate(random.nextInt(45) * 3.14 / 180, 20 * i + fontSize/2, fontSize/2+1); // 缩放文字 float scaleSize = random.nextFloat() + 0.8f; if (scaleSize >= 1.1f) scaleSize = 1f; trans.scale(scaleSize, scaleSize); g2d_word.setTransform(trans); /************************/ g.drawString(String.valueOf(ctmp), fontSize * i + 10, (height-fontSize)/2+10); //g.drawString(String.valueOf(ctmp), fontSize * i + 10, 10); } } // 将生成的验证码保存到Session中 session.setAttribute("randCheckCode", sRand); g.dispose(); ImageIO.write(image, "JPEG", response.getOutputStream()); } /** * 论坛的回复帖验证码 * @author zhanglei * @param session * @param response * @throws IOException */ @RequestMapping("imgService") public void imgService(HttpSession session, HttpServletResponse response) throws IOException { // 禁止缓存 response.setHeader("Pragma", "No-cache"); response.setHeader("Cache-Control", "No-cache"); response.setDateHeader("Expires", 0); // 指定生成的响应是图片 response.setContentType("image/jpeg"); int fontSize = 25; //int width = 100; int width = fontSize*4; int height = 25; BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); // 创建BufferedImage类的对象 Graphics g = image.getGraphics(); // 创建Graphics类的对象 Graphics2D g2d = (Graphics2D) g; // 通过Graphics类的对象创建一个Graphics2D类的对象 Random random = new Random(); // 实例化一个Random对象 Font mFont = new Font("华文宋体", Font.BOLD, fontSize); // 通过Font构造字体 //g.setColor(Color.WHITE); // 改变图形的当前颜色为随机生成的颜色 g.setColor(getRandColor(200,250)); g.fillRect(0, 0, width, height); // 绘制一个填色矩形 // 画一条折线 BasicStroke bs = new BasicStroke(1f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL); // 创建一个供画笔选择线条粗细的对象 g2d.setStroke(bs); // 改变线条的粗细 g.setColor(Color.DARK_GRAY); // 设置当前颜色为预定义颜色中的深灰色 int[] xPoints = new int[3]; int[] yPoints = new int[3]; for (int j = 0; j < 3; j++) { xPoints[j] = random.nextInt(width - 1); yPoints[j] = random.nextInt(height - 1); } g.drawPolyline(xPoints, yPoints, 3); // 生成并输出随机的验证文字 g.setFont(mFont); char[] chs = { '1', 'l', '0', 'O', 'o', 'L' }; String sRand = ""; int itmp = 0; for (int i = 0; i < 4; i++) { if (random.nextInt(2) == 1) { itmp = random.nextInt(26) + 65; // 生成A~Z的字母 } else { itmp = random.nextInt(10) + 48; // 生成0~9的数字 } boolean canAdd = true; for (char c : chs) { if (c == itmp) { i = i - 1; canAdd = false; } } if (canAdd == true) { char ctmp = (char) itmp; sRand += String.valueOf(ctmp); Color color = new Color(20 + random.nextInt(110), 20 + random.nextInt(110), 20 + random.nextInt(110)); g.setColor(color); /**** 随机缩放文字并将文字旋转指定角度 **/ // 将文字旋转指定角度 Graphics2D g2d_word = (Graphics2D) g; AffineTransform trans = new AffineTransform(); trans.rotate(random.nextInt(45) * 3.14 / 180, 20 * i + fontSize/2, fontSize/2+1); // 缩放文字 float scaleSize = random.nextFloat() + 0.8f; if (scaleSize >= 1.1f) scaleSize = 1f; trans.scale(scaleSize, scaleSize); g2d_word.setTransform(trans); /************************/ g.drawString(String.valueOf(ctmp), fontSize * i + 10, (height-fontSize)/2+10); //g.drawString(String.valueOf(ctmp), fontSize * i + 10, 10); } } // 将生成的验证码保存到Session中 论坛回帖的验证码 session.setAttribute("forumCheckCode", sRand); g.dispose(); ImageIO.write(image, "JPEG", response.getOutputStream()); } //设置背景颜色 add by yanght@2012-07-09 public Color getRandColor(int fc,int bc){//给定范围获得随机颜色 Random random = new Random(); 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); } @RequestMapping("service1.do") public String serv() { return "/check.jsp"; } }
js:
//验证码 $("#fresh").click(function(){ $("#img").attr("src","checkCodeAction/imgService.do?time="+new Date()); /* $.ajax({ url:"checkCodeAction/service1.do", type:"post", datatype:"html", success : function(data) { $("#img_div").html('').append(data); }, error : function() { alert("ajax请求出错"); } }); */ });
html:
<!-- 验证码 --> <span style="float: left; margin-left: 15px; margin-top: 8px;">验证码: <input style=" 100px; height: 22px;"/> <span id="img_div" ><img src="checkCodeAction/imgService.do" id="img" style=" 100px; height: 25px;"/></span> <a id="fresh" href="javascript:void(0);">换一个</a> </span>