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){//給定范圍獲得隨機顏色
                 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();
                 
          //生成隨機類
                 Random random = new Random();
                 
          // 設定背景色
                 g.setColor(getRandColor(200,250));
                 g.fillRect(
          00, width, height);
                 
          //設定字體
                 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();
                  
          //生成隨機類
                  Random random = new Random();
                  
          // 設定背景色
                  g.setColor(getRandColor(200,250));
                  g.fillRect(
          00, width, height);
                  
          //設定字體
                  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融合進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");//設定輸出的類型
                  JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(response.getOutputStream());
                   encoder.encode(img);
          //對圖片進行輸出編碼
                 }
          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);//休眠以控制字符的重復問題
                   }

                   
          return content;
               }

               
          /**
                * 獲取隨機字符
                * 
          */

               
          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

          導航

          統(tǒng)計

          公告

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

          常用鏈接

          留言簿(2)

          隨筆分類

          隨筆檔案

          收藏夾

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 洛扎县| 新乡市| 华亭县| 松溪县| 蓝田县| 新乡县| 泽库县| 布尔津县| 丰顺县| 密云县| 清流县| 平武县| 澄江县| 定结县| 民勤县| 铜川市| 鹤庆县| 镇平县| 邻水| 仪征市| 靖安县| 大埔区| 博客| 新龙县| 武胜县| 克什克腾旗| 广安市| 乌审旗| 崇左市| 西贡区| 天长市| 宁南县| 新源县| 阳谷县| 时尚| 商都县| 锡林郭勒盟| 德清县| 龙山县| 腾冲县| 筠连县|