<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    實現驗證碼

    Posted on 2008-12-16 16:17 李春生 閱讀(331) 評論(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 © 李春生

    主站蜘蛛池模板: 亚洲午夜久久久久久尤物| 免费国产小视频在线观看| 在线观看亚洲av每日更新| 精品无码专区亚洲| 精品国产免费一区二区| 亚洲熟妇无码一区二区三区| 国产精品成人免费一区二区| 亚洲伊人色一综合网| 猫咪社区免费资源在线观看| 在线综合亚洲中文精品| 国产一精品一AV一免费孕妇 | 亚洲精彩视频在线观看| 中文字幕在线观看免费视频| 亚洲网站在线播放| 在线观看免费人成视频| 亚洲成人激情小说| 国产hs免费高清在线观看| 牛牛在线精品观看免费正 | 亚洲色大网站WWW永久网站| 在线观看免费a∨网站| 亚洲av成本人无码网站| 亚洲XX00视频| 久久美女网站免费| 亚洲日本乱码卡2卡3卡新区| 国产成人精品免费午夜app| 亚洲www在线观看| 国产成人精品免费视频软件| 一区二区三区免费视频观看| 亚洲国产精品无码久久SM| 在线观看永久免费| 亚洲中文字幕无码不卡电影| 免费国产成人18在线观看| 国产免费内射又粗又爽密桃视频 | 国产亚洲成人久久| 在线看片免费人成视久网| 涩涩色中文综合亚洲| 亚洲?V无码成人精品区日韩| 日韩免费高清播放器| 亚洲剧场午夜在线观看| vvvv99日韩精品亚洲| 99视频有精品视频免费观看|