java图片验证码生成代码_一篇Java图片验证码生成的代码

1 packageprojectUtil;2

3 /**

4 *@authortian5 * @date 2019/4/1015:586 */

7

8 importjavax.imageio.ImageIO;9 import java.awt.*;10 importjava.awt.geom.GeneralPath;11 importjava.awt.image.BufferedImage;12 importjava.io.FileOutputStream;13 importjava.io.IOException;14 importjava.io.OutputStream;15 importjava.util.Random;16

17 /**

18 * 验证码生成器19 *20 */

21 public classPicUtil {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 = newRandom();39

40 publicPicUtil() {41 this.createCode();42 }43

44 /**

45 *46 *@paramwidth 图片宽47 *@paramheight 图片高48 */

49 public PicUtil(int width, intheight) {50 this.width =width;51 this.height =height;52 this.createCode();53 }54

55 /**

56 *57 *@paramwidth 图片宽58 *@paramheight 图片高59 *@paramcodeCount 字符个数60 *@paramlineCount 干扰线条数61 */

62 public PicUtil(int width, int height, int codeCount, intlineCount) {63 this.width =width;64 this.height =height;65 this.codeCount =codeCount;66 this.lineCount =lineCount;67 this.createCode();68 }69

70 public voidcreateCode() {71 int codeX = 0;72 int fontHeight = 0;73 fontHeight = height - 5;//字体的高度

74 codeX = width / (codeCount + 3);//每个字符的宽度75

76 //图像buffer

77 buffImg = newBufferedImage(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 = newStringBuffer();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 = newGeneralPath();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 privateColor getRandomColor() {123 int r = getRandomNumber(255);124 int g = getRandomNumber(255);125 int b = getRandomNumber(255);126 return newColor(r, g, b);127 }128

129 /**获取随机数*/

130 private int getRandomNumber(intnumber) {131 returnrandom.nextInt(number);132 }133

134 public void write(String path) throwsIOException {135 OutputStream sos = newFileOutputStream(path);136 this.write(sos);137 }138

139 public void write(OutputStream sos) throwsIOException {140

141 ImageIO.write(buffImg, "png", sos);142 sos.flush();143 sos.close();144 }145

146 publicBufferedImage getBuffImg() {147 returnbuffImg;148 }149

150 publicString getCode() {151 returncode;152 }153

154

155 public static void main(String[] args) throwsIOException {156 for (int i = 0; i <5 ; i++) {157 newPicUtil(){{158 this.write("D:/yy/"+super.code+".png");159 }};160 }161

162 }163

164 private voiddrawTanx(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 voiddrawCosx(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 voiddrawSinx(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 voiddrawSinxDX(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 }

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值