Java 生成驗證碼圖片


  1 import java.awt.image.BufferedImage;
  2 import java.io.BufferedReader;
  3 
  4 import java.awt.Color;
  5 import java.io.FileReader;
  6 import java.io.IOException;
  7 import java.util.ArrayList;
  8 import java.util.List;
  9 import java.util.Random;
 10 
 11 import java.awt.Font;
 12 import java.awt.Graphics;
 13 import java.awt.Graphics2D;
 14 
 15 import javax.imageio.ImageIO;
 16 import javax.servlet.ServletException;
 17 import javax.servlet.http.HttpServlet;
 18 import javax.servlet.http.HttpServletRequest;
 19 import javax.servlet.http.HttpServletResponse;
 20 
 21 /**
 22  * 驗證碼生成程序
 23  */
 24 public class CheckChengyu extends HttpServlet {
 25 
 26 // 集合中保存所有成語
 27     private List<String> words = new ArrayList<String>();
 28 
 29     @Override
 30     public void init() throws ServletException {
 31 // 初始化階段,讀取qy95.txt
 32 // web工程中讀取 文件,必須使用絕對磁盤路徑
 33         String path = getServletContext().getRealPath("/WEB-INF/chengyu.txt");
 34         try {
 35             BufferedReader reader = new BufferedReader(new FileReader(path));
 36             String line;
 37             while ((line = reader.readLine()) != null) {
 38                 words.add(line);
 39             }
 40             reader.close();
 41         } catch (IOException e) {
 42             e.printStackTrace();
 43         }
 44     }
 45 
 46     public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 47 // 禁止緩存
 48 // response.setHeader("Cache-Control", "no-cache");
 49 // response.setHeader("Pragma", "no-cache");
 50 // response.setDateHeader("Expires", -1);
 51 
 52         int width = 120;
 53         int height = 30;
 54 
 55 // 步驟一 繪制一張內存中圖片
 56         BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
 57 
 58 // 步驟二 圖片繪制背景顏色 ---通過繪圖對象
 59         Graphics graphics = bufferedImage.getGraphics();// 得到畫圖對象 --- 畫筆
 60 // 繪制任何圖形之前 都必須指定一個顏色
 61         graphics.setColor(getRandColor(200, 250));
 62         graphics.fillRect(0, 0, width, height);
 63 
 64 // 步驟三 繪制邊框
 65         graphics.setColor(Color.WHITE);
 66         graphics.drawRect(0, 0, width - 1, height - 1);
 67 
 68 // 步驟四 四個隨機數字
 69         Graphics2D graphics2d = (Graphics2D) graphics;
 70 // 設置輸出字體
 71         graphics2d.setFont(new Font("宋體", Font.BOLD, 18));
 72 
 73         Random random = new Random();// 生成隨機數
 74         int index = random.nextInt(words.size());
 75         String word = words.get(index);// 獲得成語
 76 
 77 // 定義x坐標
 78         int x = 10;
 79         for (int i = 0; i < word.length(); i++) {
 80 // 隨機顏色
 81         graphics2d.setColor(new Color(20 + random.nextInt(110), 20 + random.nextInt(110), 20 + random.nextInt(110)));
82
83 // 旋轉 -30 --- 30度 84 int jiaodu = random.nextInt(60) - 30; 85 // 換算弧度 86 double theta = jiaodu * Math.PI / 180; 87 88 // 獲得字母數字 89 char c = word.charAt(i); 90 91 // 將c 輸出到圖片 92 graphics2d.rotate(theta, x, 20); 93 graphics2d.drawString(String.valueOf(c), x, 20); 94 graphics2d.rotate(-theta, x, 20); 95 x += 30; 96 } 97 98 // 將驗證碼內容保存session 99 request.getSession().setAttribute("checkcode_session", word); 100 101 // 步驟五 繪制干擾線 102 graphics.setColor(getRandColor(160, 200)); 103 int x1; 104 int x2; 105 int y1; 106 int y2; 107 for (int i = 0; i < 30; i++) { 108 x1 = random.nextInt(width); 109 x2 = random.nextInt(12); 110 y1 = random.nextInt(height); 111 y2 = random.nextInt(12); 112 graphics.drawLine(x1, y1, x1 + x2, x2 + y2); 113 } 114 115 // 將上面圖片輸出到瀏覽器 ImageIO 116 graphics.dispose();// 釋放資源 117 118 //將圖片寫到response.getOutputStream()中 119 ImageIO.write(bufferedImage, "jpg", response.getOutputStream()); 120 121 } 122 123 public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 124 doGet(request, response); 125 } 126 127 /** 128 * 取其某一范圍的color 129 * 130 * @param fc int 范圍參數1 131 * @param bc int 范圍參數2 132 * @return Color 133 */ 134 private Color getRandColor(int fc, int bc) { 135 // 取其隨機顏色 136 Random random = new Random(); 137 if (fc > 255) { 138 fc = 255; 139 } 140 if (bc > 255) { 141 bc = 255; 142 } 143 int r = fc + random.nextInt(bc - fc); 144 int g = fc + random.nextInt(bc - fc); 145 int b = fc + random.nextInt(bc - fc); 146 return new Color(r, g, b); 147 } 148 149 }

 


免責聲明!

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



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