java生成图片验证码--公用

来源:互联网 发布:mac os x 10.12 cdr 编辑:程序博客网 时间:2024/05/21 15:05

   公用的验证码类
// 图形验证码的字符集合,系统将随机从这个字符串中选择一些字符作为验证码private static String codeChars = "23456789abcdefghkmnpqrstuvwxyzABCDEFGHKLMNPQRSTUVWXYZ";// 返回一个随机颜色(Color对象)private static Color getRandomColor(int minColor, int maxColor) {Random random = new Random();// 保存minColor最大不会超过255if (minColor > 255)minColor = 255;// 保存minColor最大不会超过255if (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);}public String getValidationCode(String realwordpath ) 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));}String pathString=realwordpath+"\\code.png";File file = new File(pathString);      ImageIO.write(image, "png", file);     g.dispose();    return validationCode.toString();    //byte[] data = ((DataBufferByte) image.getData().getDataBuffer()).getData();    } catch (Exception e) {e.printStackTrace();  return "1235";}}


原来这里指定生成的图片会放到d盘下,修改了一下代码可以将地址改为项目的路径,realwordpath指的是项目的全路径图片存放地址

在jsp页面中这样使用

<input type="text" style="width:50%; height:2.5em;" placeholder="请输入验证码" name="code" id="code" />
<img src="<%=path%>/resources/images/code.png" width="78" height="35" />


控制器中记录验证码的值,方便校验

@RequestMapping(value="/tolist")public String tolist(Model model,HttpServletRequest request,@RequestParam String  type) throws IOException{String str="";String wordPath="/resources/images/";String realwordpath = request.getSession().getServletContext().getRealPath(wordPath);File fileDir =new File(realwordpath);       //如果文件夹不存在则创建        if  (!fileDir .exists()  && !fileDir .isDirectory())          {           fileDir .mkdir();        }     str=applyLawerService.getValidationCode(realwordpath);
    model.addAttribute("str", str.toLowerCase());    return "lawerChoose/webPage/ApplyLawer";}


0 0
原创粉丝点击