import java.awt.Color; 004 import java.awt.Font; 005 import java.awt.Graphics; 006 import java.awt.image.BufferedImage; 007 import java.util.Random; 008 009 import javax.imageio.ImageIO; 010 import javax.servlet.http.HttpServletResponse; 011 012 import org.apache.struts2.ServletActionContext; 013 014 public class CheckAction { 015 016 017 018 019 public String exec() throws Exception { 020 021 HttpServletResponse response = ServletActionContext.getResponse(); 022 int codeLength = 4;// 验证码长度 023 int mixTimes = 1;// 模糊程度参数 024 Color bgColor = getRandColor(200, 250);// 背景颜色 025 Color bfColor = new Color(0, 0, 0);// 字体颜色 026 boolean ifRandomColor = true;// 单个字符是否颜色随机 027 boolean ifMixColor = false;// 模糊线是否颜色随机 028 029 // 设置页面不缓存 030 response.setHeader("Pragma", "No-cache"); 031 response.setHeader("Cache-Control", "no-cache"); 032 response.setDateHeader("Expires", 0); 033 // 在内存中创建图象 034 int width = 13 * codeLength + 6, height = 20; 035 BufferedImage image = new BufferedImage(width, height, 036 BufferedImage.TYPE_INT_RGB); 037 // 获取图形上下文 038 Graphics g = image.getGraphics(); 039 // 设定背景色 040 g.setColor(bgColor); 041 g.fillRect(0, 0, width, height); 042 // 设定字体 043 g.setFont(new Font("Arail", Font.BOLD, 18)); 044 // 画边框 045 g.setColor(new Color(33, 66, 99)); 046 g.drawRect(0, 0, width - 1, height - 1); 047 // 随机产生干扰线,使图象中的认证码不易被其它程序探测到 048 g.setColor(getRandColor(160, 200)); 049 for (int i = 0; i < mixTimes * codeLength / 10; i++) { 050 if (ifMixColor) { 051 g.setColor(getRandColor(160, 200)); 052 } 053 int x = random.nextInt(width); 054 int y = random.nextInt(height); 055 int xl = random.nextInt(12); 056 int yl = random.nextInt(12); 057 g.drawLine(x, y, x + xl, y + yl); 058 } 059 // 取随机产生的认证码(4位数字) 060 061 // 向图片中输出数字和字母 062 Random r=new Random(); 063 StringBuffer sb = new StringBuffer(); 064 065 char[] ch = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".toCharArray(); 066 067 int index, len = ch.length; 068 String sRand = ""; 069 for (int i = 0; i < codeLength; i ++) { 070 071 index = r.nextInt(len); 072 073 String rand =String.valueOf(ch[index]); 074 sRand += rand; 075 // 将认证码显示到图象中 076 if (ifRandomColor) 077 g.setColor(getRandColor(20, 110, 0)); 078 else 079 g.setColor(bfColor); 080 // 调用函数出来的颜色相同,可能是因为种子太接近,所以只能直接生成 081 g.drawString(rand, 13 * i + 6, 16); 082 } 083 // 将认证码存入SESSION 084 // 图象生效 085 ServletActionContext.getRequest().getSession().setAttribute("rand", 086 sRand); 087 g.dispose(); 088 // 输出图象到页面 089 ImageIO.write(image, "PNG", response.getOutputStream()); 090 return "success"; 091 092 } 093 094 private static Random random = new Random(); 095 096 private Color getRandColor(int fc, int bc) { 097 return getRandColor(fc, bc, fc); 098 } 099 100 private Color getRandColor(int fc, int bc, int interval) { 101 if (fc > 255) { 102 fc = 255; 103 } 104 if (bc > 255) { 105 bc = 255; 106 } 107 int r = fc + random.nextInt(bc - interval); 108 int g = fc + random.nextInt(bc - interval); 109 int b = fc + random.nextInt(bc - interval); 110 return new Color(r, g, b); 111 } 112 113 } [代码] [Java]代码 001 package org.lsf.action; 002 003 import java.awt.Color; 004 import java.awt.Font; 005 import java.awt.Graphics; 006 import java.awt.image.BufferedImage; 007 import java.util.Random; 008 009 import javax.imageio.ImageIO; 010 import javax.servlet.http.HttpServletResponse; 011 012 import org.apache.struts2.ServletActionContext; 013 014 public class CheckAction { 015 016 017 018 019 public String exec() throws Exception { 020 021 HttpServletResponse response = ServletActionContext.getResponse(); 022 int codeLength = 4;// 验证码长度 023 int mixTimes = 1;// 模糊程度参数 024 Color bgColor = getRandColor(200, 250);// 背景颜色 025 Color bfColor = new Color(0, 0, 0);// 字体颜色 026 boolean ifRandomColor = true;// 单个字符是否颜色随机 027 boolean ifMixColor = false;// 模糊线是否颜色随机 028 029 // 设置页面不缓存 030 response.setHeader("Pragma", "No-cache"); 031 response.setHeader("Cache-Control", "no-cache"); 032 response.setDateHeader("Expires", 0); 033 // 在内存中创建图象 034 int width = 13 * codeLength + 6, height = 20; 035 BufferedImage image = new BufferedImage(width, height, 036 BufferedImage.TYPE_INT_RGB); 037 // 获取图形上下文 038 Graphics g = image.getGraphics(); 039 // 设定背景色 040 g.setColor(bgColor); 041 g.fillRect(0, 0, width, height); 042 // 设定字体 043 g.setFont(new Font("Arail", Font.BOLD, 18)); 044 // 画边框 045 g.setColor(new Color(33, 66, 99)); 046 g.drawRect(0, 0, width - 1, height - 1); 047 // 随机产生干扰线,使图象中的认证码不易被其它程序探测到 048 g.setColor(getRandColor(160, 200)); 049 for (int i = 0; i < mixTimes * codeLength / 10; i++) { 050 if (ifMixColor) { 051 g.setColor(getRandColor(160, 200)); 052 } 053 int x = random.nextInt(width); 054 int y = random.nextInt(height); 055 int xl = random.nextInt(12); 056 int yl = random.nextInt(12); 057 g.drawLine(x, y, x + xl, y + yl); 058 } 059 // 取随机产生的认证码(4位数字) 060 061 // 向图片中输出数字和字母 062 Random r=new Random(); 063 StringBuffer sb = new StringBuffer(); 064 065 char[] ch = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".toCharArray(); 066 067 int index, len = ch.length; 068 String sRand = ""; 069 for (int i = 0; i < codeLength; i ++) { 070 071 index = r.nextInt(len); 072 073 String rand =String.valueOf(ch[index]); 074 sRand += rand; 075 // 将认证码显示到图象中 076 if (ifRandomColor) 077 g.setColor(getRandColor(20, 110, 0)); 078 else 079 g.setColor(bfColor); 080 // 调用函数出来的颜色相同,可能是因为种子太接近,所以只能直接生成 081 g.drawString(rand, 13 * i + 6, 16); 082 } 083 // 将认证码存入SESSION 084 // 图象生效 085 ServletActionContext.getRequest().getSession().setAttribute("rand", 086 sRand); 087 g.dispose(); 088 // 输出图象到页面 089 ImageIO.write(image, "PNG", response.getOutputStream()); 090 return "success"; 091 092 } 093 094 private static Random random = new Random(); 095 096 private Color getRandColor(int fc, int bc) { 097 return getRandColor(fc, bc, fc); 098 } 099 100 private Color getRandColor(int fc, int bc, int interval) { 101 if (fc > 255) { 102 fc = 255; 103 } 104 if (bc > 255) { 105 bc = 255; 106 } 107 int r = fc + random.nextInt(bc - interval); 108 int g = fc + random.nextInt(bc - interval); 109 int b = fc + random.nextInt(bc - interval); 110 return new Color(r, g, b); 111 } 112 113 } 114 //jsp页面代码 115 <INPUT id="yanzhen" class=regtxt style="width:60px;" title=请输入验证码 maxLength=5 size=16 116 name="yanzhen" > <a href="javascript:reloadVerifyCode()"><img src="<%=request.getContextPath()%>/checks_exec" id="safecode" /></a> 117 //点击换图片js代码 118 119 function reloadVerifyCode(){ 120 121 var timenow = new Date().getTime(); 122 123 document.getElementById("safecode").src="<%=request.getContextPath()%>/checks_exec?d="+timenow; 124