随机验证码---支持大小写字母、数字、随机字体、文字大小(三)
来源:互联网 发布:如何举报淘宝店铺违规 编辑:程序博客网 时间:2024/05/29 03:44
java随机产生验证码,可以随机生成数字、大写字母、小写字母。还可以随机生成文字字体、及大小。在图片上面可能字体都不不同、大小不等。
public class ValidCodeUtils {/** * 验证码生成工具类 *//********************************************************************* * 验证码宽度 */ public static int WIDTH = 60; /*** * 验证码高度 */ public static int HEIGHT = 20; /********************************************************************** * 验证码背景颜色COLOR_FC_BG 应当小于COLOR_BC_BG */ public static int COLOR_FC_BG = 200; /*** * 验证码背景颜色COLOR_FC_BG 应当小于COLOR_BC_BG */ public static int COLOR_BC_BG = 250; /********************************************************************** * 验证码背景干扰线颜色COLOR_FC_LINE 应当小于COLOR_BC_LINE */ public static int COLOR_FC_LINE = 160; /*** * 验证码背景干扰线颜色COLOR_FC_LINE 应当小于COLOR_BC_LINE */ public static int COLOR_BC_LINE = 200; /*************************************************************************** * 验证码颜色COLOR_FC_CODE 应当小于COLOR_BC_CODE */ public static int COLOR_FC_CODE = 20; /*** * 验证码颜色COLOR_FC_CODE 应当小于COLOR_BC_CODE */ public static int COLOR_BC_CODE = 170; /*************************************************************************** * 生成在指定范围内的颜色 * @param fc 范围fc color值 小于255 * @param bc 范围bc color值 小于255 * @return Color */ private static Color getRandColor(int fc, int bc) { Random random = new Random(); if (fc < 0) fc = 0; if (bc < 0) bc = 1; if (fc > 255) fc = 255; if (bc > 255) bc = 255; if (bc == fc) bc += 10; int temp = 0; if (bc < fc) { temp = bc; bc = fc; fc = temp; } 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); } /** * function:生成图片方法 * @param request HttpServletRequest * @param response HttpServletResponse * @return boolean * @throws Exception */ public static boolean getImage(HttpServletRequest request, HttpServletResponse response) throws Exception{ response.reset(); response.setContentType("image/jpeg"); // 设置页面不缓存 response.setHeader("Pragma", "No-cache"); response.setHeader("Cache-Control", "no-cache"); response.setDateHeader("Expires", 0); // 在内存中创建图象 BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB); // 获取图形上下文 Graphics img = image.getGraphics(); // 生成随机类 Random random = new Random(); // 设定背景色 img.setColor(getRandColor(COLOR_FC_BG, COLOR_BC_BG)); img.fillRect(0, 0, WIDTH, HEIGHT); // 设定字体 img.setFont(new Font("Times New Roman", Font.PLAIN, 18)); // 画边框 // g.setColor(new Color()); // g.drawRect(0,0,width-1,height-1); // 随机产生155条干扰线,使图象中的认证码不易被其它程序探测到 img.setColor(getRandColor(COLOR_FC_LINE, COLOR_BC_LINE)); for (int i = 0; i < 155; i++) { int x = random.nextInt(WIDTH); int y = random.nextInt(HEIGHT); int xl = random.nextInt(12); int yl = random.nextInt(12); img.drawLine(x, y, x + xl, y + yl); } // 取随机产生的认证码(4位数字) String codeValue = ""; for (int i = 0; i < 4; i++) { //String rand = String.valueOf(random.nextInt(10)); String rand = getRandomChar(); codeValue = codeValue.concat(rand); img.setFont(getRandomFont());//随机字体 // 将认证码显示到图象中 img.setColor(getRandColor(COLOR_FC_CODE, COLOR_BC_CODE)); img.drawString(rand, 13 * i + 6, 16); } request.getSession().setAttribute("codeValue", codeValue); // 图象生效 img.dispose(); // 输出图象到页面 return ImageIO.write(image, "JPEG", response.getOutputStream()); } /** * 随机生成字符,含大写、小写、数字 * function: 功能 * @return */ public static String getRandomChar() { int index = (int) Math.round(Math.random() * 2); String randChar = ""; switch (index) { case 0://大写字符 randChar = String.valueOf((char)Math.round(Math.random() * 25 + 65)); break; case 1://小写字符 randChar = String.valueOf((char)Math.round(Math.random() * 25 + 97)); break; default://数字 randChar = String.valueOf(Math.round(Math.random() * 9)); break; } return randChar; } /** * function: 随机生成字体、文字大小 * @return */ public static Font getRandomFont() { String[] fonts = {"Georgia", "Verdana", "Arial", "Tahoma", "Time News Roman", "Courier New", "Arial Black", "Quantzite"}; int fontIndex = (int)Math.round(Math.random() * (fonts.length - 1)); int fontSize = (int) Math.round(Math.random() * 4 + 16); return new Font(fonts[fontIndex], Font.PLAIN, fontSize); } }
0 0
- 随机验证码---支持大小写字母、数字、随机字体、文字大小(三)
- Java web 开发: 随机生成验证码,支持大小写字母、数字;随机字体
- Java 随机生成验证码,支持大小写字母、数字;随机字体
- Java web 开发: 随机生成验证码,支持大小写字母、数字;随机字体
- Java web 开发: 随机生成验证码,支持大小写字母、数字;随机字体
- 生成随机的验证码?大小写字母和数字
- PHP简单验证码类:字母+数字 字体随机扭曲
- C++ 随机 大小写字母数字
- java随机生成数字字母验证码
- 生成六位随机图片验证码(包括数字 大小写字母 部分汉字)
- 随机验证码(包括数字、大小写字母),输入“0”结束
- Java 随机生成六位数验证码过程(含大小写字母及数字)
- Java随机产生4位验证码(数字、大小写字母组成)
- asp.net2.0生成一个复杂的验证码,背景随机,字体随机,中英文数字随机
- asp.net2.0生成一个复杂的验证码,背景随机,字体随机,中英文数字随机
- C#生成一个复杂的验证码、背景随机、字体随机、中英文数字随机
- ASP数字、字母验证码(带随机杂点)
- C# 生成验证码,取随机数字加字母
- 学习鸟哥的Linux私房菜笔记(8)——文件查找与文件管理2
- 创建支持多种屏幕尺寸的Android应用
- 内部类问题
- 在win7—64位上使用DEBUG调试汇编程序
- 使用CCTMXTiledMap出错的问题!
- 随机验证码---支持大小写字母、数字、随机字体、文字大小(三)
- PS打开图片失败的解决方式
- Web技术发展历史
- BI入门之而Oracle BIEE (Business Intelligence) 11g 11.1.1.6.0 学习(3)创建一个简单的分析
- 希腊人4
- POJ分类
- 错误: 很多未定义标识符错误 vc2010 ——解决方法
- jmp指令对应的机器码
- 子序列的个数(解)--【英雄会】