一篇Java圖片驗證碼生成的代碼

  1 package projectUtil;
  2 
  3 /**
  4  * @author tian
  5  * @date 2019/4/1015:58
  6  */
  7 
  8 import javax.imageio.ImageIO;
  9 import java.awt.*;
 10 import java.awt.geom.GeneralPath;
 11 import java.awt.image.BufferedImage;
 12 import java.io.FileOutputStream;
 13 import java.io.IOException;
 14 import java.io.OutputStream;
 15 import java.util.Random;
 16 
 17 /**
 18  * 驗證碼生成器
 19  *
 20  */
 21 public class PicUtil {
 22     // 圖片的寬度。
 23     private int width = 120;
 24     // 圖片的高度。
 25     private int height = 40;
 26     // 驗證碼字符個數
 27     private int codeCount = 6;
 28     // 驗證碼干擾線數
 29     private int lineCount = 3;
 30     // 驗證碼
 31     private String code = null;
 32     // 驗證碼圖片Buffer
 33     private BufferedImage buffImg = null;
 34 
 35     private char[] codeSequence = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'M', 'N', 'P', 'Q', 'R',
 36             'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '2', '3', '4', '5', '6', '7', '8', '9' };
 37     // 生成隨機數
 38     private Random random = new Random();
 39 
 40     public PicUtil() {
 41         this.createCode();
 42     }
 43 
 44     /**
 45      *
 46      * @param width 圖片寬
 47      * @param height 圖片高
 48      */
 49     public PicUtil(int width, int height) {
 50         this.width = width;
 51         this.height = height;
 52         this.createCode();
 53     }
 54 
 55     /**
 56      *
 57      * @param width 圖片寬
 58      * @param height 圖片高
 59      * @param codeCount 字符個數
 60      * @param lineCount 干擾線條數
 61      */
 62     public PicUtil(int width, int height, int codeCount, int lineCount) {
 63         this.width = width;
 64         this.height = height;
 65         this.codeCount = codeCount;
 66         this.lineCount = lineCount;
 67         this.createCode();
 68     }
 69 
 70     public void createCode() {
 71         int codeX = 0;
 72         int fontHeight = 0;
 73         fontHeight = height - 5;// 字體的高度
 74         codeX = width / (codeCount + 3);// 每個字符的寬度
 75 
 76         // 圖像buffer
 77         buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
 78         Graphics2D g = buffImg.createGraphics();
 79 
 80         // 將圖像填充爲白色
 81         g.setColor(Color.WHITE);
 82         g.fillRect(0, 0, width, height);
 83 
 84         // 創建字體
 85 //        ImgFontByte imgFont = new ImgFontByte();
 86         Font font=new Font("宋體",Font.PLAIN,16);
 87         g.setFont(font);
 88 
 89         StringBuffer randomCode = new StringBuffer();
 90         // 隨機產生驗證碼字符
 91         for (int i = 0; i < codeCount; i++) {
 92             String strRand = String.valueOf(codeSequence[random.nextInt(codeSequence.length)]);
 93             // 設置字體顏色
 94             g.setColor(getRandomColor());
 95             // 設置字體位置
 96             g.drawString(strRand, (i + 1) * codeX, getRandomNumber(height / 2) + 20);
 97             randomCode.append(strRand);
 98         }
 99 // 利用GeneralPath類來畫曲線  
100 GeneralPath gp = new GeneralPath();
101         gp.moveTo(0,0);
102 
103 
104         for (int i = 0; i < lineCount; i++) {
105 // 繪製一個圓弧(弧線)
106 //            void drawArc(int x, int y, int width, int height, int startAngle, int arcAngle)
107 // 填充一個圓弧(扇形)
108 //            void fillArc(int x, int y, int width, int height, int startAngle, int arcAngle)
109 //            drawTanx(gp,g);
110 //            drawCosx(gp,g);
111             drawSinx(gp,g);
112             drawSinxDX(gp,g);
113             // 設置字體顏色
114             g.setColor(getRandomColor());
115             //  繪製一個圓弧(弧線)
116 //            g.drawArc(lineCount*getRandomNumber(10), lineCount*getRandomNumber(10), width, height,getRandomNumber(100),lineCount*getRandomNumber(100)%2==1?-lineCount*getRandomNumber(360):lineCount*getRandomNumber(1872));
117         }
118         code = randomCode.toString();
119     }
120 
121     /** 獲取隨機顏色 */
122     private Color getRandomColor() {
123         int r = getRandomNumber(255);
124         int g = getRandomNumber(255);
125         int b = getRandomNumber(255);
126         return new Color(r, g, b);
127     }
128 
129     /** 獲取隨機數 */
130     private int getRandomNumber(int number) {
131         return random.nextInt(number);
132     }
133 
134     public void write(String path) throws IOException {
135         OutputStream sos = new FileOutputStream(path);
136         this.write(sos);
137     }
138 
139     public void write(OutputStream sos) throws IOException {
140 
141         ImageIO.write(buffImg, "png", sos);
142         sos.flush();
143         sos.close();
144     }
145 
146     public BufferedImage getBuffImg() {
147         return buffImg;
148     }
149 
150     public String getCode() {
151         return code;
152     }
153 
154 
155     public static void main(String[] args) throws IOException {
156         for (int i = 0; i <5 ; i++) {
157             new PicUtil(){{
158                 this.write("D:/yy/"+super.code+".png");
159             }};
160         }
161 
162     }
163 
164         private void drawTanx(GeneralPath gp, Graphics2D g2d) {
165             for (double i = 0.000001; i <= 8*Math.PI; i+=0.0001*Math.PI) {
166                 gp.lineTo(20*i, 100*-Math.tan(i));
167             }
168             g2d.draw(gp);
169 
170             // 將當前畫筆以原點爲中心,旋轉180度,畫奇函數(關於原點對稱)
171             g2d.rotate(Math.PI);
172             g2d.draw(gp);
173         }
174 
175         private void drawCosx(GeneralPath gp, Graphics2D g2d) {
176             for (double i = 0.000001; i <= 8*Math.PI; i+=0.0001*Math.PI) {
177                 gp.lineTo(20*i, 100*-Math.cos(i));
178             }
179             g2d.draw(gp);
180 
181             // 將當前畫筆以Y中爲對稱軸,畫偶函數(關於Y軸對稱)
182             g2d.scale(-1, 1);
183             g2d.draw(gp);
184         }
185         private void drawSinx(GeneralPath gp, Graphics2D g2d) {
186             for (double i = 0.000001; i <= 8*Math.PI; i+=0.0001*Math.PI) {
187                 gp.lineTo(15*i, 100*-Math.sin(i));
188             }
189             g2d.draw(gp);
190             g2d.rotate(Math.PI*0.01*getRandomNumber(100));
191             g2d.draw(gp);
192         }
193 
194         private void drawSinxDX(GeneralPath gp, Graphics2D g) {
195             for (double i = 0.1; i <= 8*Math.PI; i+=0.0001*Math.PI) {
196                 gp.lineTo(20*i, -100*-Math.sin(i)/i);
197             }
198             g.draw(gp);
199             g.scale(-1, 1);
200             g.draw(gp);
201         }
202 
203     }

網上有類似的代碼,但獨獨只有生成的文字沒有干擾線。所以花了點時間完善了一下,發上來,如果有需要請拿走。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章