1、生成验证码工具类

public class CheckCodeTool {
private Integer width = 80;
private Integer height = 38; public String getCheckCode(BaseForm baseForm) {
/*
* 绘图
*/
// step1,创建一个内存映像对象(画板)
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
// step2,获得画笔
Graphics g = image.getGraphics();
// step3,给笔上色
//Random r = new Random();
SecureRandom r = new SecureRandom();
// g.setColor(new Color(r.nextInt(255), r.nextInt(255),r.nextInt(255)));
// step4,给画板设置背景颜色
g.fillRect(0, 0, width, height);
// step5,绘制一个随机的字符串
String number = getNumber();
g.setColor(new Color(0, 0, 0)); //存储到redis(用于登录校验)
if (baseForm != null && StringUtils.isNotEmpty(baseForm.getSessionId())) {
String redisCheckCodeId = "CheckCodeId";
try {
if (RedisUtil.getInstance().isExists(redisCheckCodeId)) {
RedisUtil.getInstance().remove(redisCheckCodeId);
}
RedisUtil.getInstance().setStringWithSeconds(redisCheckCodeId,number,60);//1分钟时效
} catch (Exception e) {
System.out.println("getCheckCode:error:" + e.toString());
}
}
// new Font(字体,风格,大小)
g.setFont(new Font(null, Font.ITALIC, 20));
g.drawString(number, 5, 25);
// step6,加一些干扰线
for (int i = 0; i < 8; i++) {
g.setColor(new Color(r.nextInt(255), r.nextInt(255), r.nextInt(255)));
g.drawLine(r.nextInt(width), r.nextInt(height), r.nextInt(width), r.nextInt(height));
}
/*
* 压缩图片并输出到客户端(浏览器)
*/
ByteArrayOutputStream out = new ByteArrayOutputStream();
String base64String = "";
try {
ImageIO.write(image, "jpeg", out);
base64String = Base64Utils.encode(out.toByteArray());
} catch (Exception e) {
e.printStackTrace();
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} return base64String;
} // 生成随机数作为验证码
private String getNumber() {
String number = "";
String pool = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
SecureRandom r = new SecureRandom();
for (int i = 0; i < 4; i++) {
number += pool.charAt(r.nextInt(pool.length()));
}
return number;
}
}

2、测试验证类

public class CheckCodeTest {

    public static void main(String[] args) {
// TODO Auto-generated method stub
CheckCodeTool checkCodeTool = new CheckCodeTool();
String checkCode = checkCodeTool.getCheckCode(new BaseForm());
System.out.println(checkCode);
} }

输出:

/9j/4AAQSkZJRgABAgAAAQABAAD/2wBDAAgGB ......

将base64String字符串传递给前端,即可显示图片验证码。

Java使用imageio、awt生成图片验证码的更多相关文章

  1. (七)利用servlet生成图片验证码

    总结: 验证码就是一张图,然后往这张图上写入随机的字符(数字字母等). 1.1 编写html页面 <!DOCTYPE html> <html> <head> < ...

  2. SpringBoot使用谷歌方式生成图片验证码

    1.新建一个springboot的项目 2.导入坐标 <dependency> <groupId>com.github.penggle</groupId> < ...

  3. selenium+java破解极验滑动验证码的示例代码

    转自: https://www.jianshu.com/p/1466f1ba3275 selenium+java破解极验滑动验证码 卧颜沉默 关注 2017.08.15 20:07* 字数 3085  ...

  4. java实现随机字母数字验证码

    生成随街验证码 VerifyCode 工具类 package com.meeno.common.cerifycode; import javax.imageio.ImageIO; import jav ...

  5. 怎样用Java自制优秀的图片验证码?这样!

    Completely Automated Public Turing test to tell Computers and Humans Apart 全自动区分计算机和人类的图灵测试 简称CAPTCH ...

  6. PHP生成图片验证码demo【OOP面向对象版本】

    下面是我今天下午用PHP写的一个生成图片验证码demo,仅供参考. 这个demo总共分为4个文件,具体代码如下: 1.code.html中的代码: <!doctype html> < ...

  7. python 全栈开发,Day85(Git补充,随机生成图片验证码)

    昨日内容回顾 第一部分:django相关 1.django请求生命周期 1. 当用户在浏览器中输入url时,浏览器会生成请求头和请求体发给服务端 请求头和请求体中会包含浏览器的动作(action),这 ...

  8. JAVA 实现 QQ 邮箱发送验证码功能(不局限于框架)

    JAVA 实现 QQ 邮箱发送验证码功能(不局限于框架) 本来想实现 QQ 登录,有域名一直没用过,还得备案,好麻烦,只能过几天再更新啦. 先把实现的发送邮箱验证码更能更新了. 老规矩,更多内容在注释 ...

  9. net生成图片验证码--转自Lisliefor

    目前,机器识别验证码已经相当强大了,比较常见的避免被机器识别的方法,就是将验证码的字符串连到一起,这样就加大的识别的难度,毕竟机器没有人工智能.我找了很多的.net生成图片验证码的例子,后来经过一些修 ...

随机推荐

  1. 使用C#实现SSLSocket加密通讯 Https

    原文链接 http://blog.csdn.net/wuyb_2004/article/details/51393290 using System; using System.Collections; ...

  2. ABP中文网入门篇教程中的一个bug

    入门--从空项目开始--使用ASP.NET Core Web Application https://cn.abp.io/documents/abp/latest/Autofac-Integratio ...

  3. 去掉小程序button元素的边框

    button::after {     display:none }

  4. AtCoder Beginner Contest 113 B

    B - Palace Time limit : 2sec / Memory limit : 1024MB Score: 200 points Problem Statement A country d ...

  5. c# 实现无符号右移

    /// <summary> /// 无符号右移, 相当于java里的 value>>>pos /// </summary> /// <param nam ...

  6. UltraEdit 21.3 增加 mssql, json 高亮

    1.  %appdata%\IDMComp\UltraEdit 2.  将msql2k.uew,  json.uew 放到 wordfiles 目录即可 msql2k   json的uew 下载地址如 ...

  7. python_学生信息管理实例

    """提示:代码中的内容均被注释,请参考,切勿照搬""" """注意:代码切勿照搬,错误请留言指出" ...

  8. 路由器中带宽设置(Bandwidth) 20MHZ/40MHZ

    原文是英文的,在这里:http://routerguide.net/setting-up-20-mhz-or-40-mhz-bandwidth-how-to-improve-wifi-network- ...

  9. vue中添加util公共方法&&ES6之import、export

    vue中添加util公共方法&&ES6之import.export https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Re ...

  10. 基于setTimeout制作滚动广告板

    很多网站在其门户页面的上方正中央都会放置一个滚动广告板,用于显示一些推荐信息,用户点击即可进入浏览.比较常见的就是各个公司的官网,电商网站的首页等. 下面是天猫的滚动广告板截图. 其实,不需要借助于什 ...