servlet生成驗(yàn)證碼

          import java.awt.Color;
          import java.awt.Font;
          import java.awt.Graphics2D;
          import java.awt.image.BufferedImage;
          import java.io.File;
          import java.util.LinkedList;
          import java.util.Random;
          import javax.imageio.ImageIO;
          import javax.servlet.http.HttpServletResponse;
          import com.sun.image.codec.jpeg.JPEGCodec;
          import com.sun.image.codec.jpeg.JPEGImageEncoder;
          //import java.awt.Graphics;
          public class testimage {
              
          public String sRand="";
              
          public testimage(){
                  
              }

              
          public Color getRandColor(int fc,int bc){//給定范圍獲得隨機(jī)顏色
                 Random random = new Random();
                 
          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);
               }

              
          /**
               * 通過(guò)文件創(chuàng)建圖像
               * 格式為jpg類(lèi)型
               * 
          */

               
          public void creatImage(String fileName,String content){
                   
          //在內(nèi)存中創(chuàng)建圖象
                 int width=60, height=20;
                 BufferedImage image 
          = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
                 
          // 獲取圖形上下文
                 Graphics2D g = image.createGraphics();
                 
          //Graphics g=image.getGraphics();
                 
          //生成隨機(jī)類(lèi)
                 Random random = new Random();
                 
          // 設(shè)定背景色
                 g.setColor(getRandColor(200,250));
                 g.fillRect(
          00, width, height);
                 
          //設(shè)定字體
                 g.setFont(new Font("Times New Roman",Font.PLAIN,18));
                 g.setColor(Color.black);
          //黑色文字
                 g.drawString(content,10,15);
                 g.dispose();
                 
          try{
                  File f
          =new File(fileName);
                  
          if(!f.exists()){
                      f.createNewFile();
                  }
          else{
                      Thread.sleep(
          200);
                      f.delete();
                      f.createNewFile();
                  }

                  ImageIO.write(image, 
          "jpg", f);
                 }
          catch(Exception e){
                     e.printStackTrace();
                 }

               }

               
          /**
                * 創(chuàng)建圖像
                * 格式為jpg類(lèi)型
                * 
          @param content - String 圖片輸出內(nèi)容
                * 
          @return java.awt.image.BufferedImage
                * 
          */

               
          public BufferedImage getBufferedImage(String content){
                
          int width=60, height=20;
                
          return getBufferedImage(content,width,height);
               }

               
          /**
                * 創(chuàng)建圖像
                * 格式為jpg類(lèi)型
                * 
          @param content - String 圖片輸出內(nèi)容
                * 
          @param width - int 圖片寬度
                * 
          @param height - int 圖片高度
                * 
          @return java.awt.image.BufferedImage
                * 
          */

                
          public BufferedImage getBufferedImage(String content,int width,int height){
                    
          //在內(nèi)存中創(chuàng)建圖象
                  BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
                  
          // 獲取圖形上下文
                  Graphics2D g = image.createGraphics();
                  
          //Graphics g=image.getGraphics();
                  
          //生成隨機(jī)類(lèi)
                  Random random = new Random();
                  
          // 設(shè)定背景色
                  g.setColor(getRandColor(200,250));
                  g.fillRect(
          00, width, height);
                  
          //設(shè)定字體
                  g.setFont(new Font("Times New Roman",Font.PLAIN,18));
                  g.setColor(Color.black);
          //黑色文字
                  g.drawString(content,10,15);
                  g.dispose();
                  
          return image;
                }

                
          /**
                 * 將現(xiàn)有BufferedImage融合進(jìn)Response
                 * 
          @param response - javax.servlet.http.ServletResponse 將使用的response對(duì)象
                 * 
          @param img - java.awt.image.BufferedImage
                 * 
          */

                
          public void response(HttpServletResponse response,BufferedImage img){
                 
          try{
                  response.setContentType(
          "image/jpg;charset=GB2312");//設(shè)定輸出的類(lèi)型
                  JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(response.getOutputStream());
                   encoder.encode(img);
          //對(duì)圖片進(jìn)行輸出編碼
                 }
          catch(Exception e){
                  e.printStackTrace();
                 }

                }

               
          /**
                * 返回一個(gè)4位的驗(yàn)證碼
                * 
          */

               
          private String getContent() throws InterruptedException{
                   String content
          ="";
                   
          for(int i=0;i<4;i++){
                       content
          +=getChar();
                       Thread.sleep(
          new Random().nextInt(10)+10);//休眠以控制字符的重復(fù)問(wèn)題
                   }

                   
          return content;
               }

               
          /**
                * 獲取隨機(jī)字符
                * 
          */

               
          private char getChar(){
                   Random random
          =new Random();
                   
          char ch='0';
                   LinkedList ls
          =new LinkedList();
                   
          for(int i=0;i<10;i++){//0-9
                       ls.add(String.valueOf(48+i));
                   }

                   
          for(int i=0;i<26;i++){//A-Z
                       ls.add(String.valueOf(65+i));
                   }

                   
          for(int i=0;i<26;i++){//a-z
                       ls.add(String.valueOf(97+i));
                   }

                   
          int index=random.nextInt(ls.size());
                   
          if(index>(ls.size()-1)){
                       index
          =ls.size()-1;
                   }

                   ch
          =(char)Integer.parseInt(String.valueOf(ls.get(index)));
                   
          return ch;
               }

               
          /**
                 測(cè)試
               
          */

          //     public static void main(String []args) throws Exception{
          //         testimage c=new testimage();
          //         c.creatImage("WebRoot/WEB-INF/image/me.jpg",c.getContent());
          //     }
          }

          posted on 2007-07-21 22:39 hugh 閱讀(240) 評(píng)論(0)  編輯  收藏 所屬分類(lèi): JAVA

          <2007年7月>
          24252627282930
          1234567
          891011121314
          15161718192021
          22232425262728
          2930311234

          導(dǎo)航

          統(tǒng)計(jì)

          公告

          小弟打算把硬盤(pán)里的所有資料整理到博客里,但是其中收藏了不少網(wǎng)上的文章(個(gè)人也記不住作者),請(qǐng)作者見(jiàn)諒!請(qǐng)知道作者的朋友(或作者本人)看到了請(qǐng)告訴小弟,小弟好把作者加上!

          常用鏈接

          留言簿(2)

          隨筆分類(lèi)

          隨筆檔案

          收藏夾

          搜索

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 宜宾县| 郓城县| 饶河县| 重庆市| 遵义县| 平阳县| 夏河县| 长寿区| 道真| 武夷山市| 河西区| 木兰县| 茌平县| 治县。| 潼关县| 黄陵县| 弥渡县| 红原县| 封开县| 天峨县| 九江市| 天台县| 兴和县| 拜城县| 会同县| 通州市| 大埔区| 凌云县| 靖宇县| 宜宾市| 海兴县| 施秉县| 丽水市| 惠来县| 交口县| 宿松县| 玉山县| 沐川县| 合水县| 巴楚县| 永和县|