java验证码代码_javaWeb实现验证码--代码超简单

importjava.awt.Color;importjava.awt.Font;importjava.awt.Graphics;importjava.awt.image.BufferedImage;importjava.io.ByteArrayOutputStream;importjava.io.IOException;importjava.io.OutputStream;importjava.util.Random;importjavax.imageio.ImageIO;importorg.slf4j.Logger;importorg.slf4j.LoggerFactory;importorg.springframework.stereotype.Service;importcom.hongyang.webglasses.model.VerifyCode;importcom.hongyang.webglasses.service.IVerifyCodeGen;importcom.hongyang.webglasses.util.RandomUtils;

@Servicepublic class SimpleCharVerifyCodeGenImpl implementsIVerifyCodeGen {private static final Logger logger = LoggerFactory.getLogger(SimpleCharVerifyCodeGenImpl.class);private static final String[] FONT_TYPES = { "\u5b8b\u4f53", "\u65b0\u5b8b\u4f53", "\u9ed1\u4f53", "\u6977\u4f53", "\u96b6\u4e66"};private static final int VALICATE_CODE_LENGTH = 4;/*** 生成随机字符

*

*@paramwidth

*@paramheight

*@paramos

*@return*@throwsIOException*/@Overridepublic String generate(int width, int height, OutputStream os) throwsIOException {

BufferedImage image= newBufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

Graphics graphics=image.getGraphics();

fillBackground(graphics, width, height);

String randomStr=RandomUtils.randomString(VALICATE_CODE_LENGTH);

createCharacter(graphics, randomStr);

graphics.dispose();//设置JPEG格式

ImageIO.write(image, "JPEG", os);returnrandomStr;

}/*** 验证码生成

*

*@paramwidth

*@paramheight

*@return

*/@Overridepublic VerifyCode generate(int width, intheight) {

VerifyCode verifyCode= null;try(//将流的初始化放到这里就不需要手动关闭流

ByteArrayOutputStream baos = newByteArrayOutputStream();

) {

String code=generate(width, height, baos);

verifyCode= newVerifyCode();

verifyCode.setCode(code);

verifyCode.setImgBytes(baos.toByteArray());

}catch(IOException e) {

logger.error(e.getMessage(), e);

verifyCode= null;

}returnverifyCode;

}/*** 设置字符颜色大小

*

*@paramg

*@paramrandomStr*/

private voidcreateCharacter(Graphics g, String randomStr) {char[] charArray =randomStr.toCharArray();for (int i = 0; i < charArray.length; i++) {//设置RGB颜色算法参数

g.setColor(new Color(50 + RandomUtils.nextInt(100),50 + RandomUtils.nextInt(100), 50 + RandomUtils.nextInt(100)));//设置字体大小,类型

g.setFont(new Font(FONT_TYPES[RandomUtils.nextInt(FONT_TYPES.length)], Font.BOLD, 26));//设置x y 坐标

g.drawString(String.valueOf(charArray[i]), 15 * i + 5, 19 + RandomUtils.nextInt(8));

}

}/*** 设置背景颜色及大小,干扰线

*

*@paramgraphics

*@paramwidth

*@paramheight*/

private static void fillBackground(Graphics graphics, int width, intheight) {//填充背景

System.setProperty("myColor", "#BBDAEE");

graphics.setColor(Color.getColor("myColor"));//设置矩形坐标x y 为0

graphics.fillRect(0, 0, width, height);//加入干扰线条

for (int i = 0; i < 8; i++) {//设置随机颜色算法参数

graphics.setColor(RandomUtils.randomColor(40, 150));

Random random= newRandom();int x =random.nextInt(width);int y =random.nextInt(height);int x1 =random.nextInt(width);int y1 =random.nextInt(height);

graphics.drawLine(x, y, x1, y1);

}

}

}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值