一篇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     }

网上有类似的代码,但独独只有生成的文字没有干扰线。所以花了点时间完善了一下,发上来,如果有需要请拿走。

 

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