<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 李春生 閱讀(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 © 李春生

    主站蜘蛛池模板: 校园亚洲春色另类小说合集 | a级毛片毛片免费观看久潮喷| 日本一道一区二区免费看 | 无人在线直播免费观看| 亚洲成色999久久网站| 一级做a爰全过程免费视频| 亚洲成人中文字幕| 免费观看无遮挡www的视频| 亚洲乱码卡三乱码新区| 67194成是人免费无码| 久久综合久久综合亚洲| 白白国产永久免费视频| 豆国产96在线|亚洲| 亚洲精品成人区在线观看| 国产在线精品观看免费观看| 亚洲精品国产品国语在线| 一级毛片免费毛片一级毛片免费 | 久久久亚洲AV波多野结衣 | 一区二区三区免费在线观看| 丁香五月亚洲综合深深爱| 久久午夜夜伦鲁鲁片无码免费| 亚洲精品人成电影网| 成人免费毛片视频| 一级**爱片免费视频| 亚洲天堂一区二区| 永久免费AV无码网站在线观看| 黄页免费视频播放在线播放| 亚洲精品无码高潮喷水在线| 亚欧在线精品免费观看一区| 亚洲第一综合天堂另类专| 亚洲免费观看视频| 欧洲黑大粗无码免费| sss日本免费完整版在线观看| 精品无码一区二区三区亚洲桃色| 午夜dj在线观看免费视频| 美女网站在线观看视频免费的| 久久精品国产亚洲av麻豆小说 | a毛片全部免费播放| 亚洲不卡影院午夜在线观看| 亚洲精品WWW久久久久久| 亚洲性线免费观看视频成熟|