實現驗證碼

          Posted on 2008-12-16 16:17 李春生 閱讀(324) 評論(0)  編輯  收藏 所屬分類: web開發
          以下類產生4位驗證碼

          public class ImageTool{

            private static Random random = new Random();

            public ImageTool() {
            }

            public static Color getRandColor(int fc,int bc){//給定范圍獲得隨機顏色
              if(fc>255) fc=255;
              if(bc>255) bc=255;
              int r=fc+random.nextInt(bc-fc);
              int g=fc+random.nextInt(bc-fc);
              int b=fc+random.nextInt(bc-fc);
              return new Color(r,g,b);
            }

            public static void outputImage(HttpSession session, HttpServletResponse response) throws IOException{

              BufferedImage image = new BufferedImage(60, 20, BufferedImage.TYPE_INT_RGB);
              Graphics g = image.getGraphics();

              g.setColor(getRandColor(200, 250));
              g.fillRect(0, 0, 60, 20);

              g.setFont(new Font("Times New Roman",Font.PLAIN,18));

              g.setColor(getRandColor(160, 200));
              for (int i=0;i<155;i++) {
                int x = random.nextInt(60);
                int y = random.nextInt(20);
                int xl = random.nextInt(12);
                int yl = random.nextInt(12);
                g.drawLine(x,y,x+xl,y+yl);
              }

              StringBuffer randCode = new StringBuffer();
              for (int i=0;i<4;i++){
                String temp = String.valueOf(random.nextInt(10));
                randCode.append(temp);
                g.setColor(new Color(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110)));
                g.drawString(temp,13*i+6,16);
              }
              session.setAttribute("randCode", randCode.toString());

              g.dispose();
              OutputStream out=response.getOutputStream();

              response.reset();
              ImageIO.write(image, "jpg", out);
              out.close();
            }

            public static void imagePut(HttpSession session,OutputStream out,int pWidth,int pHeight,int pSize) throws IOException{

              BufferedImage image = new BufferedImage(pWidth, pHeight, BufferedImage.TYPE_INT_RGB);
              Graphics g = image.getGraphics();

              g.setColor(getRandColor(200, 250));
              g.fillRect(0, 0, pWidth, pHeight);

              g.setFont(new Font("Times New Roman",Font.PLAIN,pSize));

              g.setColor(getRandColor(160, 200));
              for (int i=0;i<18;i++) {
                int x = random.nextInt(60);
                int y = random.nextInt(20);
                int xl = random.nextInt(12);
                int yl = random.nextInt(12);
                g.drawLine(x,y,x+xl,y+yl);
              }

              StringBuffer randCode = new StringBuffer();
              for (int i=0;i<4;i++){
                String temp = String.valueOf(random.nextInt(10));
                randCode.append(temp);
                g.setColor(new Color(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110)));
                g.drawString(temp,13*i+6,16);
              }
              g.dispose();
              ImageIO.write(image, "jpg", out);
              session.setAttribute("randCode", randCode.toString());
          //    System.out.println("sessionID:"+session.toString()+"; 產生的驗證碼:"+session.getAttribute("randCode"));

            }

            public static void imagePutOneColor(HttpSession session,OutputStream out,int pWidth,int pHeight,int pSize) throws IOException{

              BufferedImage image = new BufferedImage(pWidth, pHeight, BufferedImage.TYPE_INT_RGB);
              Graphics g = image.getGraphics();

              g.setColor(new Color(255,255,255));
              g.fillRect(0, 0, 60, 20);

              g.setFont(new Font("Times New Roman",Font.TRUETYPE_FONT,pSize));

              g.setColor(getRandColor(200, 250));
              for (int i=0;i<18;i++) {
                int x = random.nextInt(60);
                int y = random.nextInt(20);
                int xl = random.nextInt(12);
                int yl = random.nextInt(12);
                g.drawLine(x,y,x+xl,y+yl);
              }

              StringBuffer randCode = new StringBuffer();
              for (int i=0;i<4;i++){
                String temp = String.valueOf(random.nextInt(10));
                randCode.append(temp);
                g.setColor(new Color(0,0,0));
                g.drawString(temp,13*i+6,16);
              }
              session.setAttribute("randCode", randCode.toString());

              g.dispose();
              ImageIO.write(image, "jpg", out);
            }


            public static void outputImage(String sessionName, HttpSession session, HttpServletResponse response) throws IOException{

              BufferedImage image = new BufferedImage(60, 20, BufferedImage.TYPE_INT_RGB);
              Graphics g = image.getGraphics();

              g.setColor(getRandColor(200, 250));
              g.fillRect(0, 0, 60, 20);

              g.setFont(new Font("Times New Roman",Font.PLAIN,18));

              g.setColor(getRandColor(160, 200));
              for (int i=0;i<155;i++) {
                int x = random.nextInt(60);
                int y = random.nextInt(20);
                int xl = random.nextInt(12);
                int yl = random.nextInt(12);
                g.drawLine(x,y,x+xl,y+yl);
              }

              StringBuffer randCode = new StringBuffer();
              for (int i=0;i<4;i++){
                String temp = String.valueOf(random.nextInt(10));
                randCode.append(temp);
                g.setColor(new Color(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110)));
                g.drawString(temp,13*i+6,16);
              }
              session.setAttribute(sessionName, randCode.toString());
              g.dispose();
              response.reset();
              OutputStream out=response.getOutputStream();
              ImageIO.write(image, "JPEG", out);

              out.flush();
              out.close();
            }
          }


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


          網站導航:
           

          posts - 5, comments - 10, trackbacks - 0, articles - 23

          Copyright © 李春生

          主站蜘蛛池模板: 卢氏县| 华阴市| 麟游县| 西盟| 三原县| 营口市| 云南省| 井冈山市| 北碚区| 龙州县| 岗巴县| 永宁县| 福贡县| 库车县| 蓬溪县| 兴化市| 阳泉市| 宁陕县| 马山县| 南康市| 宣城市| 顺平县| 江永县| 马鞍山市| 中方县| 景东| 丹东市| 广州市| 辽宁省| 江陵县| 牙克石市| 孝感市| 土默特右旗| 伽师县| 榆中县| 呼图壁县| 盘山县| 山丹县| 新疆| 卢氏县| 西林县|