controller:

 /**
* 获取登录的验证码
* @param request
* @param response
*/
public void getLoginCode(HttpSession session,HttpServletRequest request,
HttpServletResponse response) throws Exception{
// 获取验证码以及图片
Map<String,Object> map = CodeUtil.generateCodeAndPic();
// 将验证码存入Session
session.setAttribute("imageCode",map.get("code"));
// 获取图片
BufferedImage image = (BufferedImage) map.get("codePic");
// 转成base64
BASE64Encoder encoder = new BASE64Encoder();
ByteArrayOutputStream baos = new ByteArrayOutputStream();//io流
ImageIO.write(image, "png", baos);//写入流中
byte[] bytes = baos.toByteArray();//转换成字节
String png_base64 = encoder.encodeBuffer(bytes).trim();//转换成base64串
//删除 \r\n
png_base64 = png_base64.replaceAll("\n", "").replaceAll("\r", "");
Map<String,String> maps = new HashMap<>();
maps.put("code","" + map.get("code"));
maps.put("address",png_base64);
JSONObject jsonObject = JSONObject.fromObject(maps);
PrintWriter writer = response.getWriter();
writer.print(jsonObject);
writer.flush();
writer.close();
}

private static int width = 180;
private static int height = 40;
private static int codeCount = 4;
private static int xx = 30;
private static int fontHeight = 36;
private static int codeY = 32;
private static char[] codeSequence = {
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R',
'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'
}; /**
* 生成验证码图片
* @return
*/
public static Map<String,Object> generateCodeAndPic(){
//定义图像buffer
BufferedImage bufferedImage = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
Graphics graphics = bufferedImage.getGraphics();
//创建一个随机数类
Random random = new Random();
// 将图像填充为白色
graphics.setColor(Color.WHITE);
// 填充图像
graphics.fillRect(0,0,width,height);
// 创建字体,字体的大小应该根据图片的高度来定
Font font = new Font("Fixedsys", Font.BOLD, fontHeight);
// 设置字体
graphics.setFont(font);
// 画边框
graphics.setColor(Color.black);
graphics.drawRect(0,0,width-1,height-1);
// 随机产生30条干扰线,让图形码不便于被其他程序检测到
graphics.setColor(Color.black);
for (int i=0;i<30;i++){
int x = random.nextInt(width);
int y = random.nextInt(height);
int x1 = random.nextInt(12);
int y1 = random.nextInt(12);
graphics.drawLine(x,y,x + x1,y + y1);
}
// randomCode用于保存随机生成的二维码,便于用户登录时验证
StringBuffer randomCode = new StringBuffer();
int red = 0, green=0,blue = 0; // 随机产生codeCount数字的验证码
for (int i =0;i<codeCount;i++){
// 得到随机的验证码数字
String code = String.valueOf(codeSequence[random.nextInt(36)]);
// 产生随机的颜色分量来构造颜色值,这样输出的每位数字的颜色都不一样
red = random.nextInt(255);
blue = random.nextInt(255);
green = random.nextInt(255); // 用随机产生的颜色将验证码放到图像中
graphics.setColor(new Color(red,green,blue));
graphics.drawString(code,(i + 1) * xx,codeY); // 将产生的四个随机数组合在一起
randomCode.append(code);
} Map<String,Object> map = new HashMap<>();
// 存放验证码
map.put("code",randomCode);
// 存放生成的验证码BufferedImage对象
map.put("codePic",bufferedImage);
return map;
}

最新文章

  1. perl 删除过期文件
  2. Horseman - 让你更轻松的使用 PhantomJS
  3. 离线安装redis集群
  4. SQL GROUP BY 后排序
  5. Spring基础——小的知识点
  6. clip to bounds 和mask to bounds的区别
  7. 使用Rxjava缓存请求
  8. SystemFile
  9. 网络流转换为Byte数组
  10. html 表单初步学习
  11. OCA读书笔记(8) - 管理用户安全
  12. react系列从零开始-react介绍
  13. 深入理解计算机系统(4.2)------逻辑设计和硬件控制语言HCL
  14. 如何在华为云软件开发云上搭建JavaWeb,Maven项目
  15. 缓存服务—Redis
  16. Navicat Premium 简体中文版 12.0.16 以上版本国外官网下载地址(非国内)
  17. PHP CLI模式下的多进程应用
  18. zookeeper集群迁移方案
  19. 【xsy2274】 平均值 线段树
  20. SpringMVC框架项目在编译运行是常见错误

热门文章

  1. 初见UDP_Client
  2. python 递归算阶乘 (转载)
  3. 2019-03-28 Python SQL 的注释
  4. android startservice无法启动服务
  5. 解析如何利用ElasticSearch和Redis检索和存储十亿信息
  6. ofbiz初级教程
  7. 关于thinkpadU盘系统盘启动不了解决方法
  8. 设计模式实例(Lua)笔记之七(Decorator模式)
  9. 数据共享之相互排斥量mutex
  10. 三种SVM的对偶问题