java生成图片验证码二,加入透明颜色,各种干扰线,干扰点,干扰框,旋转,随机位置

本文转载自:https://blog.csdn.net/huitoukest/article/details/49781431

  1. public class VerificationCode {  
  2.   
  3.     Random rand = new Random();  
  4.     /** 
  5.      * 随机产生的加数和被加数 
  6.      */  
  7.     private int jiashu=0;  
  8.     private int beijiashu=0;  
  9.     /** 
  10.      * 随机产生的计算方式,0表示加,1表示减 
  11.      */  
  12.     private int js=0;    
  13.     private char[] aa={'零','壹','贰','叁','肆','伍','陆','柒','捌','玖'};  
  14.     private char[] action={'加','减'};  
  15.     private char[] jieguo={'等','是'};  
  16.     public static void main(String[] args) {  
  17.         VerificationCode code=new VerificationCode();  
  18.          JFrame jFrame=new JFrame();  
  19.           jFrame.setBounds(400400250250);  
  20.             
  21.           ImageIcon img = new ImageIcon(code.getVerificationCode2());   
  22.           JLabel background = new JLabel(img);  
  23.             
  24.           jFrame.add(background);  
  25.           jFrame.setVisible(true);  
  26.           jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   
  27.     }  
  28.     /** 
  29.      * 第二种验证码的计算方式,两位数的加减法 
  30.      * @return 一个新的验证码图片 
  31.      */  
  32.     public BufferedImage getVerificationCode2()  
  33.     {  
  34.         int width=200;  
  35.         int height=100;  
  36.         int degree=0;//继续一共旋转的角度,方便最后的时候旋转回来  
  37.        BufferedImage image = new BufferedImage(width, height,   
  38.                 BufferedImage.TYPE_4BYTE_ABGR);   
  39.         // 获取图形上下文   
  40.         Graphics g = image.getGraphics();   
  41.           
  42.         //画随机干扰框  
  43.         setSquareBackGround(g,width,height,5);   
  44.         //画干扰点  
  45.         //CreateRandomPoint(width, height,50,g,255);         
  46.         //随机画几条线  
  47.        // CreateRandomLine(width, height,2,g,255);           
  48.         // 设定背景色   
  49.         Color background=getColor(180);  
  50.         g.setColor(background);  
  51.         g.fillRect(00, width, height);                     
  52.         //画边框   
  53.         g.setColor(background);  
  54.         g.drawRect(0,0,width-1,height-1);                         
  55.         //   将认证码显示到图像中,如果要生成更多位的认证码  
  56.         char[] content=getDrawContent2();  
  57.         int[] xs=getRadonWidths(width,content.length);  
  58.         int[] ys=getRadomHeights(height,content.length);  
  59.         for(int i=0;i<content.length;i++)  
  60.         {   String s=content[i]+"";  
  61.                    if(content[i]=='!')  
  62.                        s="";  
  63.                    //如果在画字之前旋转图片  
  64.             if(i!=2){         
  65.                    int maxDegree=rand.nextInt(2);  
  66.                    if(maxDegree==0)  
  67.                     maxDegree=0;  
  68.                    else maxDegree=305;  
  69.            degree=rand.nextInt(45)+maxDegree;             
  70.             }   else degree=0;  
  71.             g.setColor(getColor(background,230));   
  72.             if(i==2)//运算符号显示大一些  
  73.                 g.setFont(new Font("Atlantic Inline",Font.PLAIN,getIntRandom(22,25)));  
  74.             else  
  75.             g.setFont(new Font("Atlantic Inline",Font.PLAIN,getIntRandom(20,23)));            
  76.             RotateString(s, xs[i], ys[i], g, degree);  
  77.         }         
  78.         //画随机干扰框  
  79.          setSquareBackGround(g,width,height,3);   
  80.         //画干扰点  
  81.         CreateRandomPoint(width, height,100,g,100);         
  82.         //随机画几条线  
  83.         CreateRandomLine(width, height,8,g,100);        
  84.         // 释放图形上下文  
  85.         g.dispose();  
  86.         System.out.println("计算的结果是="+getResult2());        
  87.         return image;  
  88.     }  
  89.     /** 
  90.      * 旋转并且画出指定字符串 
  91.      * @param s 需要旋转的字符串 
  92.      * @param x 字符串的x坐标 
  93.      * @param y 字符串的Y坐标 
  94.      * @param g 画笔g 
  95.      * @param degree 旋转的角度 
  96.      */  
  97.     private void RotateString(String s,int x,int y,Graphics g,int degree)  
  98.     {  
  99.         Graphics2D g2d = (Graphics2D) g.create();                                    
  100.         //   平移原点到图形环境的中心  ,这个方法的作用实际上就是将字符串移动到某一个位置  
  101.         //g2d.translate(x-1, y+3);               
  102.         g2d.translate(getIntRandom(x, x+2), getIntRandom(y,y+2));     
  103.         //   旋转文本    
  104.          g2d.rotate(degree* Math.PI / 180);  
  105.          //特别需要注意的是,这里的画笔已经具有了上次指定的一个位置,所以这里指定的其实是一个相对位置  
  106.          g2d.drawString(s,0 , 0);  
  107.     }  
  108.       
  109.     /** 
  110.      * 随机产生干扰点 
  111.      * @param width 
  112.      * @param height 
  113.      * @param many 
  114.      * @param g 
  115.      * @param alpha 透明度0~255 0表示全透 
  116.      */  
  117.     private void CreateRandomPoint(int width,int height,int many,Graphics g,int alpha)  
  118.     {  // 随机产生干扰点  
  119.         for (int i=0;i<many;i++) {  
  120.             int x = rand.nextInt(width);   
  121.             int y = rand.nextInt(height);   
  122.             g.setColor(getColor(alpha));  
  123.             g.drawOval(x,y,rand.nextInt(3),rand.nextInt(3));   
  124.         }   
  125.     }  
  126. /** 
  127.  * 随机产生干扰线条 
  128.  * @param width 
  129.  * @param height 
  130.  * @param minMany 最少产生的数量 
  131.  * @param g 
  132.  * @param alpha 透明度0~255 0表示全透 
  133.  */  
  134.     private void CreateRandomLine(int width,int height,int minMany,Graphics g,int alpha)  
  135.     {  // 随机产生干扰线条  
  136.         for (int i=0;i<getIntRandom(minMany, minMany+6);i++) {   
  137.             int x1 =getIntRandom(0,(int)(width*0.6));   
  138.             int y1 =getIntRandom(0,(int)(height*0.6));   
  139.             int x2 =getIntRandom((int)(width*0.4),width);   
  140.             int y2 =getIntRandom((int)(height*0.2),height);    
  141.             g.setColor(getColor(alpha));  
  142.             g.drawLine(x1, y1, x2, y2);  
  143.         }   
  144.     }  
  145.       
  146.     /** 
  147.      * 由随机产生的方块来作为干扰背景 
  148.      */  
  149.     private void setSquareBackGround(Graphics g,int width,int height,int count){  
  150.         // 随机产生干扰线条  
  151.         for (int i=0;i<getIntRandom(count, count+2);i++) {   
  152.             int x1 =getIntRandom(0,(int)(width*0.3));   
  153.             int y1 =getIntRandom(0,(int)(height*0.3));   
  154.             int x2 =getIntRandom((int)(width*0.5),width);   
  155.             int y2 =getIntRandom((int)(height*0.55),height);    
  156.             g.setColor(getColor(100));  
  157.             int w=x2-x1;  
  158.             int h=y2-y1;  
  159.             if(w<0) w=-w;  
  160.             if(h<0) h=-h;  
  161.             g.drawRect(x1, y1, w, h);  
  162.             g.setColor(getColor(25));  
  163.             g.fillRect(x1, y1, w, h);  
  164.         }   
  165.     }  
  166.       
  167.     /***  
  168.      * @return 随机返一个指定区间的数字 
  169.      */  
  170.     private int getIntRandom(int start,int end)  
  171.     {   if(end<start)  
  172.         {  
  173.           int t=end;  
  174.           end=start;  
  175.           start=t;  
  176.         }  
  177.         int i=start+(int) (Math.random()*(end-start));  
  178.         return i;  
  179.     }  
  180.       
  181.     @SuppressWarnings("unused")  
  182.     private int getIntRandom(double start,double end)  
  183.     {   if(end<start)  
  184.         {  
  185.           double t=end;  
  186.           end=start;  
  187.           start=t;  
  188.         }  
  189.         double i=start+(int) (Math.random()*(end-start));  
  190.         return (int)i;  
  191.     }  
  192.       
  193.     /*** 随机返回一种颜色,透明度0~255 0表示全透 
  194.      * @return 随机返回一种颜色 
  195.      * @param alpha 透明度0~255 0表示全透 
  196.      */  
  197.     private Color getColor(int alpha)  
  198.     {  
  199.         int R=(int) (Math.random()*255);  
  200.         int G=(int) (Math.random()*255);  
  201.         int B=(int) (Math.random()*255);  
  202.     return new Color(R,G,B,alpha);  
  203.     }  
  204.       
  205.     /***  
  206.      * @return 随机返回一种颜色,与给定颜色相类似 
  207.      * @param alpha 透明度0~255 0表示全透 
  208.      */  
  209.     private Color getColor(Color c,int alpha)  
  210.     {  
  211.         int R=getIntRandom(-140,140);  
  212.         int G=getIntRandom(-140,140);  
  213.         int B=getIntRandom(-140,140);  
  214.         R=getCloserRandom(c.getRed(),R);  
  215.         B=getCloserRandom(c.getBlue(),B);  
  216.         G=getCloserRandom(c.getGreen(),G);  
  217.     return new Color(R,G,B,alpha);  
  218.     }  
  219.     /** 
  220.      * 在颜色值和给定的随机数之间返回一个随机颜色值0~255 
  221.      * @param colorValue 
  222.      * @param randomValue 
  223.      * @param deep,默认为0 
  224.      * @return 
  225.      */  
  226.     @SuppressWarnings("unused")  
  227.     private int getCloserRandom(int colorValue,int randomValue){          
  228.         if(colorValue+randomValue>255)  
  229.         {   
  230.             return getCloserRandom(colorValue,randomValue-getIntRandom(1, randomValue+20));  
  231.         }else if(colorValue+randomValue<0){  
  232.             return getCloserRandom(colorValue,randomValue+getIntRandom(1, randomValue+20));  
  233.         }else if(Math.abs(randomValue)<60)  
  234.         {  
  235.             return getCloserRandom(colorValue,getIntRandom(-255,255));  
  236.         }else{  
  237.             return colorValue+randomValue;  
  238.         }  
  239.     }  
  240.       
  241.     /** 
  242.      *  
  243.      * @return 返回getVerificationCode2需要画出的内容:两位数加减法字符数组 
  244.      */  
  245.     private char[] getDrawContent2()  
  246.     { beijiashu=0;  
  247.       jiashu=0;  
  248.     char[] temp=new char[6];  
  249.     char[] w =aa;    
  250.     int k=0;      
  251.           /** 
  252.            * 产生被加数 
  253.            */  
  254.         //从aa\bb\cc中选择一个字符数组作为素材      
  255.         k=(int)(Math.random()*4);  
  256.           w=aa;  
  257.           k=(int)(Math.random()*10);  
  258.           temp[0]=w[k];  
  259.           if(k==0) temp[0]='!';  
  260.           beijiashu+=k*10;  
  261.           k=(int)(Math.random()*10);  
  262.           temp[1]=w[k];  
  263.           beijiashu+=k;  
  264.           /** 
  265.            * 产生加数 
  266.            */  
  267.         k=(int)(Math.random()*2);  
  268.           w=aa;  
  269.        k=(int)(Math.random()*10);  
  270.           temp[3]=w[k];  
  271.       if(k==0) temp[3]='!';    
  272.       jiashu=k*10+jiashu;           
  273.         k=(int)(Math.random()*10);  
  274.           temp[4]=w[k];  
  275.           jiashu+=k;  
  276.           //选择加减乘除  
  277.           w=action;  
  278.           k=(int)(Math.random()*2 );  
  279.           temp[2]=w[k];  
  280.           js=k%2;  
  281.           //结果  
  282.           w=jieguo;  
  283.           k=(int)(Math.random()*2);  
  284.           temp[5]=w[k];  
  285.           return temp;  
  286.     }     
  287.     /** 
  288.      * 对图片选择,这里保留以方便以后使用 
  289.      * @param bufferedimage 
  290.      * @param degree 
  291.      * @return 一张旋转后的图片 
  292.      */  
  293.     public BufferedImage rolateImage(BufferedImage bufferedimage,int degree,Color backGround)  
  294.     {                  
  295.         BufferedImage img;  
  296.         int w = bufferedimage.getWidth();  
  297.         int h = bufferedimage.getHeight();  
  298.         int type = BufferedImage.TYPE_INT_RGB;  
  299.         Graphics2D graphics2d;  
  300.         graphics2d = (img = new BufferedImage(w, h, type)).createGraphics();  
  301.         graphics2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION,  
  302.                     RenderingHints.VALUE_INTERPOLATION_BICUBIC);  
  303.         graphics2d.rotate(Math.toRadians(degree), w / 2, h / 2);  
  304.         graphics2d.drawImage(bufferedimage,nullnull);   
  305.         return img;  
  306.     }  
  307.       
  308.     /** 
  309.      * 得到验证码getVerificationCode2,计算出来的结果 
  310.      */  
  311.     public int getResult2()  
  312.     { if(js==0)  
  313.         return(beijiashu+jiashu);  
  314.       else if(js==1return (beijiashu-jiashu);  
  315.         return 0;  
  316.     }  
  317.     /** 
  318.      *  
  319.      * @param many 
  320.      * @return 画图的时候随机的高度的数组 
  321.      */  
  322.     private int[] getRadomHeights(int height,int many){  
  323.         int[] temp=new int[many];   
  324.         for(int i=0;i<many;i++){  
  325.             temp[i]=getRadomHeight(height);  
  326.         }  
  327.         return temp;  
  328.     }  
  329.     /** 
  330.      *  
  331.      * @param many 
  332.      * @return 画图的时候起始x坐标的数组 
  333.      */  
  334.     private int[] getRadonWidths(int width,int many)  
  335.     { int[] temp=new int[many];   
  336.       for(int i=0;i<many;i++){  
  337.           if(i==0)  
  338.           temp[i]=getRadonWidth(many,0,width);  
  339.           else temp[i]=getRadonWidth(many,temp[i-1],width);  
  340.       }  
  341.       return temp;        
  342.     }  
  343.       
  344.     private int getRadomHeight(int fullHeight)  
  345.     {   
  346.       return getIntRandom((int)(fullHeight*0.2), (int)(fullHeight*0.75));         
  347.     }  
  348.       
  349.     private int getRadonWidth(int many,int minWidth, int maxWidth)  
  350.     {  
  351.       int minJianju=maxWidth/(many+2);  
  352.       int maxJianju=maxWidth/(many);  
  353.       int temp=maxJianju-minJianju;  
  354.       //在的规定的范围内产生一个随机数  
  355.       return (int)(Math.random()*temp)+minWidth+minJianju;        
  356.     }  
  357.   
  358.   
  359. }  


说明,干扰信息,如干扰点,干扰框等待在文字和背景的前后都加入理论效果更高;

         通过设置图片大小,颜色透明度,干扰数量,文字大小,旋转角度等等,可以自行下载定制;


效果图:

干扰点:

          


干扰框/点/线图:



不加入干扰的效果:



干扰框的图:



调整验证码大小和字体大小之后的图:


猜你喜欢

转载自blog.csdn.net/qq_35868412/article/details/80484688