如何用java生成隨機驗證碼


 1.VerifyCode 類:

   1 package com.HRuinger.enity;

  2 
  3  import java.awt.BasicStroke;
  4  import java.awt.Color;
  5  import java.awt.Font;
  6  import java.awt.Graphics2D;
  7  import java.awt.image.BufferedImage;
  8  import java.io.IOException;
  9  import java.io.OutputStream;
 10  import java.util.Random;
 11 
 12  import javax.imageio.ImageIO;
 13 
 14  public  class VerifyCode {
 15      private  int w = 85;
 16      private  int h = 35;
 17       private Random r =  new Random();
 18       //  {"宋體", "華文楷體", "黑體", "華文新魏", "華文隸書", "微軟雅黑", "楷體_GB2312"}
 19       private String[] fontNames  = {"宋體", "華文楷體", "黑體", "微軟雅黑", "楷體_GB2312"};
 20      //  可選字符
 21       private String codes  = "23456789abcdefghjkmnopqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ";
 22      //  背景色
 23       private Color bgColor  =  new Color(255, 255, 255);
 24      //  驗證碼上的文本
 25       private String text ;
 26     
 27      //  生成隨機的顏色
 28       private Color randomColor () {
 29          int red = r.nextInt(150);
 30          int green = r.nextInt(150);
 31          int blue = r.nextInt(150);
 32          return  new Color(red, green, blue);
 33     }
 34     
 35      //  生成隨機的字體
 36       private Font randomFont () {
 37          int index = r.nextInt(fontNames.length);
 38         String fontName = fontNames[index]; // 生成隨機的字體名稱
 39           int style = r.nextInt(4); // 生成隨機的樣式, 0(無樣式), 1(粗體), 2(斜體), 3(粗體+斜體)
 40           int size = r.nextInt(5) + 26;  // 生成隨機字號, 24 ~ 28
 41           return  new Font(fontName, style, size);
 42     }
 43     
 44      //  畫干擾線
 45       private  void drawLine (BufferedImage image) {
 46          int num  = 3; // 一共畫3條
 47          Graphics2D g2 = (Graphics2D)image.getGraphics();
 48          for( int i = 0; i < num; i++) { // 生成兩個點的坐標,即4個值
 49               int x1 = r.nextInt(w);
 50              int y1 = r.nextInt(h);
 51              int x2 = r.nextInt(w);
 52              int y2 = r.nextInt(h); 
 53             g2.setStroke( new BasicStroke(1.5F)); 
 54             g2.setColor(Color.BLUE);  // 干擾線是藍色
 55              g2.drawLine(x1, y1, x2, y2); // 畫線
 56          }
 57     }
 58     
 59      //  隨機生成一個字符
 60       private  char randomChar () {
 61          int index = r.nextInt(codes.length());
 62          return codes.charAt(index);
 63     }
 64     
 65      //  創建BufferedImage
 66       private BufferedImage createImage () {
 67         BufferedImage image =  new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); 
 68         Graphics2D g2 = (Graphics2D)image.getGraphics(); 
 69         g2.setColor( this.bgColor);
 70         g2.fillRect(0, 0, w, h);
 71           return image;
 72     }
 73     
 74      //  調用這個方法得到驗證碼
 75       public BufferedImage getImage () {
 76         BufferedImage image = createImage(); // 創建圖片緩沖區 
 77          Graphics2D g2 = (Graphics2D)image.getGraphics(); // 得到繪制環境
 78          StringBuilder sb =  new StringBuilder(); // 用來裝載生成的驗證碼文本
 79           //  向圖片中畫4個字符
 80           for( int i = 0; i < 4; i++)  { // 循環四次,每次生成一個字符
 81              String s = randomChar() + ""; // 隨機生成一個字母 
 82              sb.append(s);  // 把字母添加到sb中
 83               float x = i * 1.0F * w / 4;  // 設置當前字符的x軸坐標
 84              g2.setFont(randomFont());  // 設置隨機字體
 85              g2.setColor(randomColor());  // 設置隨機顏色
 86              g2.drawString(s, x, h-5);  // 畫圖
 87          }
 88          this.text = sb.toString();  // 把生成的字符串賦給了this.text
 89          drawLine(image);  // 添加干擾線
 90           return image;        
 91     }
 92     
 93      //  返回驗證碼圖片上的文本
 94       public String getText () {
 95          return text;
 96     }
 97     
 98      //  保存圖片到指定的輸出流
 99       public  static  void output (BufferedImage image, OutputStream out) 
100                  throws IOException {
101         ImageIO.write(image, "jpg", out);
102     }
103 }

 

 2.ImageTest類

 

  1 package com.HRuinger.enity;

 2 
 3  import java.awt.Color;
 4  import java.awt.Graphics;
 5  import java.awt.Graphics2D;
 6  import java.awt.image.BufferedImage;
 7  import java.io.FileNotFoundException;
 8  import java.io.FileOutputStream;
 9  import java.io.IOException;
10 
11  import javax.imageio.ImageIO;
12 
13  public  class ImageTest {
14 
15    public  void fun1()  throws FileNotFoundException, IOException{
16          /*
17           * 1. 創建圖片緩沖區
18           * 2. 設置其寬高
19           * 3. 得到這個圖片的繪制環境(得到畫筆)
20           * 4. 保存起來
21            */
22         BufferedImage bi =  new BufferedImage(85, 35, BufferedImage.TYPE_INT_RGB);
23         Graphics2D g = (Graphics2D)bi.getGraphics(); // 得到繪制環境
24          g.setColor(Color.WHITE); // 把環境設置為白色
25          g.fillRect(0, 0, 70, 35); // 填充矩形!填充矩形,從0,0點開始,寬70,高35,即整個圖片,即為圖片設置背景色
26          g.setColor(Color.RED); // 把環境設置為紅色
27          g.drawString("Hello", 2,35-2); // 向圖片上寫入字符串,其中2,2表示x,y軸的坐標
28          
29   }
30      public  void fun2()  throws FileNotFoundException, IOException{
31             VerifyCode vc =  new VerifyCode(); // 創建VerifyCode類的對象
32              BufferedImage bi = vc.getImage(); // 調用getImge()方法獲得一個BufferedImage對象
33              VerifyCode.output(bi,  new FileOutputStream("WebRoot/img/yzm.jpg")); // 調用靜態方法output()方法將圖片保存在文件輸出流中
34              System.out.println(vc.getText()); // 在控制台上打印驗證碼的文本值
35      }
36 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM