Atiitt 图片图像验证码生成法原理 目录 1.1. 常见的最简单图片验证码是利用图像api把随机数文字转图像 1 1.2. 常见较为复杂图片验证码的方法 ( 镂空文字 打散 干扰线 文字扭曲

Atiitt 图片图像验证码生成法原理

 

目录

1.1. 常见的最简单图片验证码是利用图像api把随机数文字转图像 1

1.2. 常见较为复杂图片验证码的方法 ( 镂空文字  打散 干扰线 文字扭曲 粘连  膨胀,填充 ) 1

1.3. 实现 2

1.4. 参考 4

 

    1. 常见的最简单图片验证码是利用图像api把随机数文字转图像

 

    1. 常见较为复杂图片验证码的方法 ( 镂空文字  打散 干扰线 文字扭曲 粘连  膨胀,填充 旋转)

 

 

 

 

 

 

    1. 实现

/atiplat_cms/src/com/attilax/captcha/CapchGene.java

  public void geneCapch(OutputStream os) throws IOException

    {     

       //设置字母的大小,大小     

      int width=600, height=200;     

      int fontSiz = 50;

Font mFont = new Font("Times New Roman", Font.BOLD, fontSiz);  

//        response.setHeader("Pragma","No-cache");     

//        response.setHeader("Cache-Control","no-cache");     

//        response.setDateHeader("Expires", 0);     

        //表明生成的响应是图片     

   //     response.setContentType("image/jpeg");     

             // 100*18

       

        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);     

             

        Graphics g = image.getGraphics();     

        

       

        

        

        //set boder

        g.setColor(new Color(102,102,102));     

        g.drawRect(0, 0, width-1, height-1);    

        

        

        //set bg  

        g.setColor(getRandColor(200,250));     

     //   g.fillRect(1, 1, width-1, height-1);   

        g.fillRect(0,0, width, height);   

        

        g.setFont(mFont);        

        g.setColor(getRandColor(160,200));     

    

        

        Random random = new Random();   

        //画随机线     

//        for (int i=0;i<155;i++)     

//        {     

//            int x = random.nextInt(width - 1);     

//            int y = random.nextInt(height - 1);     

//            int xl = random.nextInt(6) + 1;     

//            int yl = random.nextInt(12) + 1;     

//            g.drawLine(x,y,x + xl,y + yl);     

//        }     

//    

//        //从另一方向画随机线     

//        for (int i = 0;i < 70;i++)     

//        {     

//            int x = random.nextInt(width - 1);     

//            int y = random.nextInt(height - 1);     

//            int xl = random.nextInt(12) + 1;     

//            int yl = random.nextInt(6) + 1;     

//            g.drawLine(x,y,x - xl,y - yl);     

//        }     

    

        

   

        //生成随机数,并将随机数字转换为字母     

        String sRand="";     

        for (int i=0;i<6;i++)     

        {     

            int itmp = random.nextInt(26) + 65;     

            char ctmp = (char)itmp;     

            sRand += String.valueOf(ctmp);     

            g.setColor(new Color(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110)));     

            g.drawString(String.valueOf(ctmp),fontSiz*i+10,fontSiz);     

        }     

    

//        HttpSession session = request.getSession(true);     

//        session.setAttribute("rand",sRand);     

//        g.dispose();     

        ImageIO.write(image, "JPEG", os);     

    }

 

    1. 参考

photoshop制作镂空文字

 

 

验证码的前世今生(前世篇) - 阿里聚安全 - 博客园.html

 

 atitit安全的验证码

Atitit 图片 验证码生成attilax总结

 

 

原文地址:https://www.cnblogs.com/attilax/p/15197509.html