package com.chintrip.util; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.util.Random; import javax.imageio.ImageIO; /** * @author haisheng * */ public class ImgUtil { public static byte[] creatImage(char[] rands,int WIDTH, int HEIGHT) throws IOException { // 创建内存图象并获得其图形上下文 BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB); Graphics g = image.getGraphics(); // 产生图像 drawBackground(g, WIDTH, HEIGHT); drawRands(g, rands); // 结束图像的绘制过程,完成图像 g.dispose(); // 将图像输出到客户端 ByteArrayOutputStream bos = new ByteArrayOutputStream(); ImageIO.write(image, "JPEG", bos); byte[] buf = bos.toByteArray(); bos.close(); return buf; } // 获取随机颜色 private static Color randomColor() { Random rand = new Random(); return new Color(Math.abs(rand.nextInt()) % 256, Math.abs(rand .nextInt()) % 256, Math.abs(rand.nextInt()) % 256); } private static void drawRands(Graphics g, char[] rands) { g.setFont(new Font(null, Font.ITALIC | Font.BOLD, 24)); // 在不同的高度上输出验证码的每个字符 g.setColor(randomColor()); g.drawString("" + rands[0], 1, 19); g.setColor(randomColor()); g.drawString("" + rands[1], 16, 25); g.setColor(randomColor()); g.drawString("" + rands[2], 31, 20); g.setColor(randomColor()); g.drawString("" + rands[3], 46, 23); System.out.println(rands); } private static void drawBackground(Graphics g, int WIDTH, int HEIGHT) { // 画背景 g.setColor(new Color(0xDCDCDC)); g.fillRect(0, 0, WIDTH, HEIGHT); // 随机产生120个干扰点 for (int i = 0; i < 120; i++) { int x = (int) (Math.random() * WIDTH); int y = (int) (Math.random() * HEIGHT); int red = (int) (Math.random() * 255); int green = (int) (Math.random() * 255); int blue = (int) (Math.random() * 255); g.setColor(new Color(red, green, blue)); g.drawOval(x, y, 1, 0); } } }
package com.chintrip.util; public class GenerateRandomCode { public static char[] checkCode(int length) { String chars = "123456789ABCDEFGHIJKLMNPQRSTUVWXYZ"; char[] rands = new char[length]; for (int i = 0; i < length; ++i) { int rand = (int)(Math.random() * 34.0D); rands[i] = chars.charAt(rand); } return rands; } public static String randomCode(int length) { String chars = "0123456789ABCDEFGHIJKLMNPQRSTUVWXYZO"; StringBuffer bf = new StringBuffer(); for (int i = 0; i < length; ++i) { int rand = (int)(Math.random() * 36.0D); bf.append(chars.charAt(rand)); } return bf.toString(); } public static String randomNumberCode(int length) { String chars = "0123456789"; StringBuffer bf = new StringBuffer(); for (int i = 0; i < length; ++i) { int rand = (int)(Math.random() * 10); bf.append(chars.charAt(rand)); } return bf.toString(); } public static void main(String[] args) { for(int i=0;i<20;i++){ System.out.println(randomNumberCode(6)); } } }
package com.chintrip.controller; @Controller @RequestMapping("/imgValidate") public class ImgValidateAction { private static int WIDTH = 80; private static int HEIGHT = 25; @RequestMapping("") public void index(HttpServletRequest request, HttpServletResponse response) throws IOException{ HttpSession session = request.getSession(); response.setContentType("image/jpeg"); ServletOutputStream sos = response.getOutputStream(); //设置浏览器不要缓存此图片 response.setHeader("Pragma","No-cache"); response.setHeader("Cache-Control","no-cache"); // 产生随机的认证码 char[] rands = GenerateRandomCode.checkCode(4); byte [] buf =ImgUtil.creatImage(rands,WIDTH, HEIGHT); response.setContentLength(buf.length); //下面的语句也可写成:bos.writeTo(sos); sos.write(buf); sos.close(); //将当前验证码存入到Session session.setAttribute(SysConstant.session_check_code,new String(rands)); } }
$(function(){ changeValidateCode(); }); function changeValidateCode(){ var timenow = new Date().getTime(); $("#getimage").src=" <%=path%>/imgValidate?p="+timenow; }
<a href="javascript:changeValidateCode();"><img
id="getimage" style="border: 0px none;"
src="<%=path%>/common_images/loading.gif"
align="absmiddle" title="点击图片刷新验证码"> </a>