Java Graphics2D 绘制图文验证码

Java图文验证码



public class yanzhengma extends HttpServlet {  
    private static final long serialVersionUID = 1L;  
  
      
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
        doPost(request, response) ;  
    }  
  
      
    public void doPost(HttpServletRequest request, HttpServletResponse response)  
  
                  throws ServletException, IOException {  
    	response.setContentType("image/jpeg");
            service(request, response) ;  
       
    }  
     
    //设置验证图片的宽度, 高度, 验证码的个数  
    private int width = 130;  
    private int height = 30;  
    private int codeCount = 4;  
      
    //验证码字体的高度  
    private int fontHeight = 4;  
      
    //验证码中的单个字符基线. 即:验证码中的单个字符位于验证码图形左上角的 (codeX, codeY) 位置处  
    private int codeX = 0;  
    private int codeY = 0;  
      
    //验证码由哪些字符组成  
    char [] codeSequence = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz23456789".toCharArray();  
      
    //初始化验证码图形属性  
    public void init(){  
        fontHeight = height - 2;  
        codeX = width / (codeCount + 2);  
        codeY = height - 4;  
    }  
    @RequestMapping("service")
    public void service(HttpServletRequest request, HttpServletResponse response)  
            throws ServletException, IOException {  
        //定义一个类型为 BufferedImage.TYPE_INT_BGR 类型的图像缓存  
        BufferedImage buffImg = null;  
        //BufferedImage是Image的一个子类,BufferedImage生成的图片在内存里有一个图像缓冲区,利用这个缓冲区我们可以很方便的操作这个图片
      
        buffImg = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);  
        //BufferedImage.TYPE_INT_RGB : 表示一个图像,该图像具有整数像素的 8 位 RGB 颜色
        //在 buffImg 中创建一个 Graphics2D 图像  
        Graphics2D graphics = null;  //创建画笔
        graphics = buffImg.createGraphics();  
          
        //设置一个颜色, 使 Graphics2D 对象的后续图形使用这个颜色  
        graphics.setColor(Color.WHITE);  
          
        //填充一个指定的矩形: x - 要填充矩形的 x 坐标; y - 要填充矩形的 y 坐标; width - 要填充矩形的宽度; height - 要填充矩形的高度  
        graphics.fillRect(0, 0, width, height);  
          
        //创建一个 Font 对象: name - 字体名称; style - Font 的样式常量; size - Font 的点大小  
        Font font = null;  
        font = new Font("", Font.BOLD, fontHeight);  
        //使 Graphics2D 对象的后续图形使用此字体  
        graphics.setFont(font);  
          
        graphics.setColor(Color.BLACK);  
          
        //绘制指定矩形的边框, 绘制出的矩形将比构件宽一个也高一个像素  
        graphics.drawRect(0, 0, width - 1, height - 1);  
          
        //随机产生 15 条干扰线, 使图像中的认证码不易被其它程序探测到  
        Random random = null;  
        random = new Random();  
        graphics.setColor(Color.GREEN);  
        for(int i = 0; i < 20; i++){  
            int x = random.nextInt(width);  
            int y = random.nextInt(height);  
            int x1 = random.nextInt(20);  
            int y1 = random.nextInt(20);  
            graphics.drawLine(x, y, x + x1, y + y1);  
        }  
          
        //创建 randomCode 对象, 用于保存随机产生的验证码, 以便用户登录后进行验证  
        StringBuffer randomCode;  
        randomCode = new StringBuffer();  
          
        for(int i = 0; i < codeCount; i++){  
            //得到随机产生的验证码数字  
            String strRand = null;  
            strRand = String.valueOf(codeSequence[random.nextInt(36)]);  
            
            //用随机产生的颜色将验证码绘制到图像中  
            graphics.setColor(Color.GRAY);  
            graphics.drawString(strRand, (i + 1)* codeX, codeY);  
            randomCode.append(strRand);  
        }  
          
        request.getSession().setAttribute("randomCode", randomCode.toString());  
          
        //禁止图像缓存  
        response.setHeader("Pragma", "no-cache");  
        response.setHeader("Cache-Control", "no-cache");  
        response.setDateHeader("Expires", 0);  
          
        //将图像输出到输出流中  
        ServletOutputStream sos = null;  
        sos = response.getOutputStream();  
        ImageIO.write(buffImg, "jpeg", sos);   
        sos.close();  
    }  
      
    
    //随机获取颜色
    public Color getRandColor(int s,int e){  
        Random random=new Random ();  
        if(s>255) s=255;  
        if(e>255) e=255;  
        int r,g,b;  
        r=s+random.nextInt(e-s);    //随机生成RGB颜色中的r值  
        g=s+random.nextInt(e-s);    //随机生成RGB颜色中的g值  
        b=s+random.nextInt(e-s);    //随机生成RGB颜色中的b值  
        return new Color(r,g,b);  
    }  
}  



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值