生成图片验证码

如标题,下面上java代码,粘贴复制即可用

  /**
     * 生成图片验证码
     * @param request
     * @param response
     */
    @RequestMapping(value = "/createCode", method = RequestMethod.GET)
    public void createCode(HttpServletRequest request,HttpServletResponse response) 
    {
    //    HttpSession session = request.getSession();
        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setHeader("P3P", "CP=CAO PSA OUR");
        response.setDateHeader("Expires", 0);
        
        CheckCodeImage imageCode = new CheckCodeImage();
        try {
            ImageIO.write(imageCode.creatImage(), "JPEG",response.getOutputStream());
        } catch (IOException e) {
            logger.error("生成验证码异常",e);
        }
        
        request.getSession().setAttribute("CheckCode", imageCode.getSRand());
        
        System.out.println("###验证码:"+imageCode.getSRand());
        logger.info("###验证码:" + imageCode.getSRand());
    }
 1 package com.ulic.gis.util;
 2 
 3 import java.awt.Color;
 4 import java.awt.Font;
 5 import java.awt.Graphics;
 6 import java.awt.image.BufferedImage;
 7 import java.util.Random;
 8 
 9 /** 
10  * 图片验证码对象
11  */
12 public class CheckCodeImage {
13 
14     private String sRand = "";
15 
16     /**
17      * 生成随机背景条纹
18      */
19     public Color getRandColor(int fc, int bc) {
20         Random random = new Random();
21         if (fc > 255)
22             fc = 255;
23         if (bc > 255)
24             bc = 255;
25         int r = fc + random.nextInt(bc - fc);
26         int g = fc + random.nextInt(bc - fc);
27         int b = fc + random.nextInt(bc - fc);
28         return new Color(r, g, b);
29     }
30 
31     public BufferedImage creatImage() {
32         
33         // 在内存中创建图象
34         int width = 60;//验证码图片长度
35         int height = 20;//
36         
37         BufferedImage image = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);
38         
39         // 获取图形上下文
40         Graphics g = image.getGraphics();
41         // 生成随机类
42         Random random = new Random();
43         // 设定背景色
44         g.setColor(getRandColor(200, 250));
45         g.fillRect(0, 0, width, height);
46         // 设定字体
47         g.setFont(new Font("Times New Roman", Font.PLAIN, 18));
48         // 画边框
49         // g.setColor(new Color());
50         // g.drawRect(0,0,width-1,height-1);
51         // 随机产生155条干扰线,使图象中的认证码不易被其它程序探测到
52         g.setColor(getRandColor(160, 200));
53         for (int i = 0; i < 155; i++) {
54             int x = random.nextInt(width);
55             int y = random.nextInt(height);
56             int xl = random.nextInt(12);
57             int yl = random.nextInt(12);
58             g.drawLine(x, y, x + xl, y + yl);
59         }
60         
61         // 取随机产生的认证码(4位数字)
62         for (int i = 0; i < 4; i++) {
63             String rand = String.valueOf(random.nextInt(10));
64             sRand += rand;
65             // 将认证码显示到图象中
66             g.setColor(new Color(20 + random.nextInt(110), 20 + random
67                     .nextInt(110), 20 + random.nextInt(110)));// 调用函数出来的颜色相同,可能是因为种子太接近,所以只能直接生成
68             g.drawString(rand, 13 * i + 6, 16);
69         }
70         
71         // 图象生效
72         g.dispose();
73         
74         return image;
75     }
76 
77     public String getSRand() {
78         return sRand;
79     }
80 
81     public void setSRand(String rand) {
82         sRand = rand;
83     }
84 
85 }

注意需要导入正确路径,否则不能正常使用

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

html中的图片标签,注意图片的src

<img title="点击刷新" src="<%=basePath_login%>/createCode"  id="checkCodeImg" border="0" onClick="updateCheckCode()" />

少许javascript代码,用于点击刷新图片验证码

function updateCheckCode(){
    $('#checkCodeImg').attr('src','<%=request.getContextPath%>/createCode?nocache='+ new Date().getTime());
}
原文地址:https://www.cnblogs.com/yinyl/p/10373245.html