J2EE 验证码功能 JDK自带Jar
例子1:
http://my.oschina.net/0x4ad/blog/366751
http://my.oschina.net/0x4ad/blog/366751
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
Java代码 /** * 生成验证码 * * @return * @throws IOException */ public String imageCode() throws IOException { // 在内存中创建图象 int width = 65, height = 20; BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); // 获取图形上下文 Graphics g = image.getGraphics(); // 生成随机类 Random random = new Random(); // 设定背景色 g.setColor(getRandColor(230, 255)); g.fillRect(0, 0, 100, 25); // 设定字体 g.setFont(new Font("Arial", Font.CENTER_BASELINE | Font.ITALIC, 18)); // 产生0条干扰线, g.drawLine(0, 0, 0, 0); // 取随机产生的认证码(4位数字) String sRand = ""; for (int i = 0; i < 4; i++) { String rand = String.valueOf(random.nextInt(10)); sRand += rand; // 将认证码显示到图象中 g.setColor(getRandColor(100, 150));// 调用函数出来的颜色相同,可能是因为种子太接近,所以只能直接生成 g.drawString(rand, 15 * i + 6, 16); } for(int i=0;i<(random.nextInt(5)+5);i++){ g.setColor(new Color(random.nextInt(255)+1,random.nextInt(255)+1,random.nextInt(255)+1)); g.drawLine(random.nextInt(100),random.nextInt(30),random.nextInt(100),random.nextInt(30)); } String pageId = request().getParameter("pageId"); String key = pageId + "_checkCode"; // 将验证码存入页面KEY值的SESSION里面 session().setAttribute(key, sRand); // 图象生效 g.dispose(); ServletOutputStream responseOutputStream = response().getOutputStream(); // 输出图象到页面 ImageIO.write(image, "JPEG", responseOutputStream); // 以下关闭输入流! responseOutputStream.flush(); responseOutputStream.close(); // 获得页面key值 return null; } |
例子2:
http://my.oschina.net/chenhao901007/blog/368611
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
Java代码 import java.awt.*; import java.awt.image.BufferedImage; import java.io.*; import java.util.Random; import javax.imageio.ImageIO; public class ValidationCode { // 图形验证码的字符集合,系统将随机从这个字符串中选择一些字符作为验证码 private static String codeChars = "%#23456789abcdefghkmnpqrstuvwxyzABCDEFGHKLMNPQRSTUVWXYZ"; // 返回一个随机颜色(Color对象) private static Color getRandomColor(int minColor, int maxColor) { Random random = new Random(); // 保存minColor最大不会超过255 if (minColor > 255) minColor = 255; // 保存minColor最大不会超过255 if (maxColor > 255) maxColor = 255; // 获得红色的随机颜色值 int red = minColor + random.nextInt(maxColor - minColor); // 获得绿色的随机颜色值 int green = minColor + random.nextInt(maxColor - minColor); // 获得蓝色的随机颜色值 int blue = minColor + random.nextInt(maxColor - minColor); return new Color(red, green, blue); } protected static void getValidationCode() throws IOException { try { // 获得验证码集合的长度 int charsLength = codeChars.length(); // 设置图形验证码的长和宽(图形的大小) int width = 90, height = 30; BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics g = image.getGraphics();// 获得用于输出文字的Graphics对象 Random random = new Random(); g.setColor(getRandomColor(180, 250));// 随机设置要填充的颜色 g.fillRect(0, 0, width, height);// 填充图形背景 // 设置初始字体 g.setFont(new Font("Times New Roman", Font.ITALIC, height)); g.setColor(getRandomColor(120, 180));// 随机设置字体颜色 // 用于保存最后随机生成的验证码 StringBuilder validationCode = new StringBuilder(); // 验证码的随机字体 String[] fontNames = { "Times New Roman", "Book antiqua", "Arial" }; // 随机生成3个到5个验证码 for (int i = 0; i < 3 + random.nextInt(3); i++) { // 随机设置当前验证码的字符的字体 g.setFont(new Font(fontNames[random.nextInt(3)], Font.ITALIC, height)); // 随机获得当前验证码的字符 char codeChar = codeChars.charAt(random.nextInt(charsLength)); validationCode.append(codeChar); // 随机设置当前验证码字符的颜色 g.setColor(getRandomColor(10, 100)); // 在图形上输出验证码字符,x和y都是随机生成的 g.drawString(String.valueOf(codeChar), 16 * i + random.nextInt(7), height - random.nextInt(6)); } File file = new File("d:\\code.png"); ImageIO.write(image, "png", file); System.out.println(validationCode.toString()); //byte[] data = ((DataBufferByte) image.getData().getDataBuffer()).getData(); g.dispose(); } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) throws IOException{ getValidationCode(); } } |
