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

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

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

    實現(xiàn)驗證碼

    Posted on 2008-12-16 16:17 李春生 閱讀(331) 評論(0)  編輯  收藏 所屬分類: web開發(fā)
    以下類產(chǎn)生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()+"; 產(chǎn)生的驗證碼:"+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();
      }
    }


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


    網(wǎng)站導(dǎo)航:
     

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

    Copyright © 李春生

    主站蜘蛛池模板: 免费人成视网站在线观看不卡| 中文字幕成人免费视频| 亚洲最新视频在线观看| 中文在线观看免费网站| 国产偷国产偷亚洲高清日韩| 日本免费久久久久久久网站| 亚洲天堂一区二区三区| 亚洲人成777在线播放| 美女视频黄频a免费观看| 黄桃AV无码免费一区二区三区| 免费国产黄网站在线观看| 四虎永久在线观看免费网站网址| 蜜桃成人无码区免费视频网站 | 亚洲va中文字幕无码久久| 好紧我太爽了视频免费国产| 在线观看H网址免费入口| 亚洲国产精品人久久电影| 四虎在线成人免费网站| 亚洲一级大黄大色毛片| 99re热免费精品视频观看| 亚洲综合av一区二区三区不卡 | 又粗又大又黑又长的免费视频 | 亚洲日本国产乱码va在线观看| 国产亚洲人成在线播放| 一级中文字幕乱码免费| 中文永久免费观看网站| 美女一级毛片免费观看| 亚洲AV日韩AV永久无码色欲| 一级毛片正片免费视频手机看 | 在线观看永久免费| 日本午夜免费福利视频| 亚洲国产一区视频| 亚洲高清免费在线观看| 亚洲AV无码一区二区三区鸳鸯影院| 又粗又长又爽又长黄免费视频| 16女性下面扒开无遮挡免费| 性短视频在线观看免费不卡流畅| 四虎影视在线永久免费看黄| 中文国产成人精品久久亚洲精品AⅤ无码精品| 精品久久洲久久久久护士免费 | 亚洲AV无码1区2区久久|