servlet生成驗證碼

          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);
               }

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

               
          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ī)類
                 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類型
                * 
          @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類型
                * 
          @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ī)類
                  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對象
                 * 
          @param img - java.awt.image.BufferedImage
                 * 
          */

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

                }

               
          /**
                * 返回一個4位的驗證碼
                * 
          */

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

                   
          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;
               }

               
          /**
                 測試
               
          */

          //     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 閱讀(238) 評論(0)  編輯  收藏 所屬分類: JAVA

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

          導(dǎo)航

          統(tǒng)計

          公告

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

          常用鏈接

          留言簿(2)

          隨筆分類

          隨筆檔案

          收藏夾

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 湖南省| 金寨县| 纳雍县| 桂林市| 凭祥市| 琼海市| 冀州市| 城口县| 灵石县| 靖江市| 仁化县| 永德县| 通海县| 鄢陵县| 调兵山市| 大埔县| 庆城县| 临猗县| 清水河县| 洪湖市| 玉林市| 诏安县| 农安县| 洛浦县| 隆安县| 巨鹿县| 本溪市| 息烽县| 东丰县| 册亨县| 台湾省| 旬阳县| 铜梁县| 手游| 潼南县| 綦江县| 兴文县| 青海省| 武安市| 新巴尔虎左旗| 建水县|