期待更好更穩定的開源FrameWork的出現,讓我們一起努力吧!  
          日歷
          <2007年9月>
          2627282930311
          2345678
          9101112131415
          16171819202122
          23242526272829
          30123456
          統計
          • 隨筆 - 78
          • 文章 - 1
          • 評論 - 29
          • 引用 - 0

          導航

          常用鏈接

          留言簿(1)

          隨筆分類

          隨筆檔案(42)

          文章檔案(37)

          相冊

          搜索

          •  

          積分與排名

          • 積分 - 45228
          • 排名 - 1064

          最新隨筆

          最新評論

          閱讀排行榜

          評論排行榜

           


           


           

          有時候在我們的網絡應用中,防止程序自動登錄搞破壞,我們一般都會加上驗證碼,這些驗證碼一般來說都是由人來識別的,當然,如果驗證碼很有規律,或者說很清楚,漂亮,那么也是可能被程序識別的,我以前就識別過某網站的驗證碼,因為比較有規律,所以被識別了,并且識別率達到99%左右,其實我們可以制作很復雜一點的驗證碼,添加一些干擾的線條或者字體變形,使程序識別的難度加大,這樣,我們的目的也就達到了.

          下面是生成的圖片:

          package com.hadeslee;

          import java.awt.BasicStroke;
          import java.awt.Color;
          import java.awt.Font;
          import java.awt.GradientPaint;
          import java.awt.Graphics2D;
          import java.awt.GraphicsEnvironment;
          import java.awt.Paint;
          import java.awt.Point;
          import java.awt.Stroke;
          import java.awt.image.BufferedImage;
          import java.io.*;
          import java.util.ArrayList;
          import java.util.List;
          import java.util.Locale;
          import java.util.Random;
          import javax.imageio.ImageIO;

          import javax.servlet.*;
          import javax.servlet.http.*;

          /**
           *
           * 
          @author lbf
           * 
          @version
           
          */
          public class Code extends HttpServlet {
              
              
          /** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
               * 
          @param request servlet request
               * 
          @param response servlet response
               
          */
              
          private List<String> fonts=new ArrayList<String>();
              
          public Code(){
                  initFonts();
              }
              
          private void initFonts(){
                  GraphicsEnvironment.getLocalGraphicsEnvironment().preferLocaleFonts();
                  String[] names
          =GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames(Locale.CHINA);
                  
          for(String s:names){
                      
          char c=s.charAt(0);
                      
          if(Character.isLowerCase(c)||Character.isUpperCase(c)){
                          
                      }
          else{
                          fonts.add(s);
                      }
                  }
              }
              
          protected void processRequest(HttpServletRequest request, HttpServletResponse response)
              
          throws ServletException, IOException {
                  response.setContentType(
          "image/jpeg;charset=UTF-8");
                  OutputStream out
          =response.getOutputStream();
                  BufferedImage bi
          =new BufferedImage(200,50,BufferedImage.TYPE_INT_RGB);
                  Graphics2D g
          =bi.createGraphics();
                  
          char[] cs={'0','1','2','3','4','5','6','7','8','9'};
                  
          char[] use=new char[4];
                  g.setColor(
          new Color(240,240,240));
                  g.fillRect(
          0,0,200,50);
                  
          for(int i=0;i<4;i++){
                      Point p
          =getPoint(i);
                      
          int size=getSize();
                      use[i]
          =cs[(int)(Math.random()*cs.length)];
                     
          // g.setColor(new Color((int)(Math.random()*256),0,(int)(Math.random()*256)));
                      g.setPaint(getPaint(p,size));
                      g.setFont(
          new Font(fonts.get((int)(Math.random()*fonts.size())),getFace(),size));
                      g.drawString(
          ""+use[i],p.x,p.y);
                  }
                  g.setStroke(
          new BasicStroke(1.0f));
                  g.setPaint(
          null);
                  
          for(int i=0;i<4;i++){
                      g.setColor(
          new Color((int)(Math.random()*0x00FFFFFFF)));
                      g.drawLine((
          int)(Math.random()*200),(int)(Math.random()*50),(int)(Math.random()*200),(int)(Math.random()*50));
                  }
                  Random random 
          = new Random();
                  
          for (int i=0;i<88;i++) {
                      
          int x = random.nextInt(200);
                      
          int y = random.nextInt(50);
                      g.setColor(
          new Color((int)(Math.random()*0x00FFFFFFF)));
                      g.setStroke(getStroke());
                      g.drawLine(x,y,x,y);
                  }
                  ImageIO.write(bi,
          "JPEG",out);
                  out.close();
                  g.dispose();
              }
              
          private Stroke getStroke(){
                BasicStroke bs
          =new BasicStroke((float)(Math.random()*3));
                
          return bs;
              }
              
          private Point getPoint(int index){
                  
          return new Point(5+(index*((int)(Math.random()*10)+40)),40);
              }
              
          private Paint getPaint(Point p,int size){
                  GradientPaint gp
          =new GradientPaint(p.x,p.y,new Color((int)(Math.random()*256),0,(int)(Math.random()*256)),
                          p.x,p.y
          -size,new Color((int)(Math.random()*256),(int)(Math.random()*256),(int)(Math.random()*256)));
                  
          return gp;
              }
              
          private int getFace(){
                  
          if(Math.random()*10>5){
                      
          return Font.BOLD;
                  }
          else{
                      
          return Font.ITALIC;
                  }
              }
              
          private int getSize(){
                  
          int[] sizes=new int[20];
                  
          for(int i=0;i<20;i++){
                      sizes[i]
          =30+i;
                  }
                  
          return sizes[(int)(Math.random()*sizes.length)];
              }
              
              
          // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
              /** Handles the HTTP <code>GET</code> method.
               * 
          @param request servlet request
               * 
          @param response servlet response
               
          */
              
          protected void doGet(HttpServletRequest request, HttpServletResponse response)
              
          throws ServletException, IOException {
                  processRequest(request, response);
              }
              
              
          /** Handles the HTTP <code>POST</code> method.
               * 
          @param request servlet request
               * 
          @param response servlet response
               
          */
              
          protected void doPost(HttpServletRequest request, HttpServletResponse response)
              
          throws ServletException, IOException {
                  processRequest(request, response);
              }
              
              
          /** Returns a short description of the servlet.
               
          */
              
          public String getServletInfo() {
                  
          return "Short description";
              }
              
          // </editor-fold>
          }

           



          posted on 2007-09-21 20:01 BlueSky_itwangxinli 閱讀(267) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
           
          Copyright © BlueSky_itwangxinli Powered by: 博客園 模板提供:滬江博客
          主站蜘蛛池模板: 稻城县| 伊宁市| 喀喇沁旗| 兴山县| 曲阜市| 深泽县| 台前县| 黑水县| 府谷县| 新巴尔虎右旗| 宜丰县| 阜阳市| 高邑县| 南投市| 个旧市| 怀集县| 乌海市| 荔波县| 江山市| 子洲县| 青州市| 普定县| 丰县| 绵竹市| 沧州市| 马山县| 阳城县| 金坛市| 遂溪县| 夹江县| 道真| 出国| 繁峙县| 阜平县| 进贤县| 浦东新区| 长丰县| 奉化市| 邮箱| 东乡族自治县| 霍山县|