随机数验证码:

@Data
public class ImageCode {//图形中的内容private String code;//图片private ByteArrayInputStream image;private int width = 400;private int height = 100;public static ImageCode getInstance() throws IOException {return new ImageCode();}private ImageCode() throws IOException {//图像缓冲区 (黑板)BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_BGR);//画笔Graphics graphics = image.getGraphics();//拿笔涂色画图形graphics.setColor(new Color(255,255,255));//画矩形graphics.fillRect(0,0,width,height);//字体graphics.setFont(new Font("宋体",Font.PLAIN,30));Random random = new Random();this.code="";for(int i=0;i<6;i++){String s = String.valueOf(random.nextInt(10));this.code += s;graphics.setColor(new Color(45,31,37));graphics.drawString(s,(width/6)*i,40);//画线graphics.setColor(new Color(100,100,100));graphics.drawLine((width/6)*i,40,(width/6)*i+25,40-30);}graphics.setColor(new Color(100,100,100));for (int i=0;i<100;i++){int x = random.nextInt(width);int y = random.nextInt(height);int x1 = random.nextInt(20);int y1 = random.nextInt(20);graphics.drawLine(x,y,x+x1,y+y1);}//收笔graphics.dispose();ByteArrayInputStream inputStream = null;ByteOutputStream outputStream = new ByteOutputStream();try {//赋值给byteArrayInputStreamImageOutputStream imageOutputStream = ImageIO.createImageOutputStream(outputStream);ImageIO.write(image,"jpeg",imageOutputStream);inputStream = new ByteArrayInputStream(outputStream.toByteArray());}catch (Exception e){System.out.println("生成验证码失败");}this.image = inputStream;}
}

加法验证码(1+8=?):

@Data
public class ImageCode {//图形中的内容private String code;//图片private ByteArrayInputStream image;private int width = 400;private int height = 100;public static ImageCode getInstance() throws IOException {return new ImageCode();}private ImageCode() throws IOException {//图像缓冲区 (黑板)BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_BGR);//画笔Graphics graphics = image.getGraphics();//拿笔涂色画图形graphics.setColor(new Color(255,255,255));//画矩形graphics.fillRect(0,0,width,height);//字体graphics.setFont(new Font("宋体",Font.PLAIN,30));Random random = new Random();int num1 = random.nextInt(20);int num2 = random.nextInt(20);graphics.setColor(new Color(0,200,100));graphics.drawString(num1+"", (width/6)*0+2,60);graphics.drawString("+", (width/6)*1+2,60);graphics.drawString(num2+"", (width/6)*2+2,60);graphics.drawString("=", (width/6)*3+2,60);graphics.drawString("?", (width/6)*4+2,60);int result = num1 + num2;this.code = result + "";//画100段干扰线段graphics.setColor(new Color(100,100,100));for (int i=0;i<100;i++){int x = random.nextInt(width);int y = random.nextInt(height);int x1 = random.nextInt(20);int y1 = random.nextInt(20);graphics.drawLine(x,y,x+x1,y+y1);}//收笔graphics.dispose();ByteArrayInputStream inputStream = null;ByteOutputStream outputStream = new ByteOutputStream();try {//赋值给byteArrayInputStreamImageOutputStream imageOutputStream = ImageIO.createImageOutputStream(outputStream);ImageIO.write(image,"jpeg",imageOutputStream);inputStream = new ByteArrayInputStream(outputStream.toByteArray());}catch (Exception e){System.out.println("生成验证码失败");}this.image = inputStream;}
}

保存验证码图片:

@RestController
@RequestMapping("/code")
public class VerifyCodeController {String attrName = "verifyCode";@GetMapping("/generator")@TokenCheck(required = false)public void  generatorCode(HttpServletRequest request,HttpServletResponse response){try {ImageCode imageCode = ImageCode.getInstance();//验证码的值String code = imageCode.getCode();request.getSession().setAttribute(attrName,code);//验证码图片ByteArrayInputStream image = imageCode.getImage();response.setContentType("image/jpeg");byte[] bytes = new byte[1024];try(ServletOutputStream out = response.getOutputStream()){while (image.read(bytes)!=-1){out.write(bytes);}}}catch (Exception e){System.out.println("异常");}}@GetMapping("/verity")@TokenCheck(required = false)public String verity(String verityCode, HttpServletRequest request){String s = request.getSession().getAttribute(attrName).toString();if(verityCode.equals(s)){return "效验码验证通过";}return "效验码验证不通过";}
}

base64字符串验证码:

    String attrName = "verifyCode";@GetMapping("/generator-base64")@TokenCheck(required = false)public String  generatorCodeBase64(HttpServletRequest request,HttpServletResponse response){try {ImageCode imageCode = ImageCode.getInstance();//验证码的值String code = imageCode.getCode();request.getSession().setAttribute(attrName,code);//验证码图片ByteArrayInputStream image = imageCode.getImage();request.getSession().setAttribute(attrName,code);ByteArrayOutputStream swapStream = new ByteArrayOutputStream();byte[] buff = new byte[1024];int r = 0;while ((r=image.read(buff,0,1024))>0){swapStream.write(buff,0,r);}byte[] data = swapStream.toByteArray();return Base64.getEncoder().encodeToString(data);}catch (Exception e){System.out.println("异常");return "";}}@GetMapping("/verity")@TokenCheck(required = false)public String verity(String verityCode, HttpServletRequest request){String s = request.getSession().getAttribute(attrName).toString();if(verityCode.equals(s)){return "效验码验证通过";}return "效验码验证不通过";}
}

识别验证码:

<dependency><groupId>net.sourceforge.tess4j</groupId><artifactId>tess4j</artifactId><version>4.5.4</version>
</dependency>
public class TesseractTest {public static void main(String[] args) throws TesseractException {ITesseract iTesseract = new Tesseract();//语言包,加进来iTesseract.setDatapath("语言包具体路径");//iTesseract.setLanguage("chi_sim");iTesseract.setLanguage("eng");File fileDir = new File("文件具体路径");for(File file:fileDir.listFiles()){String s = iTesseract.doOCR(file);System.out.println(file.getName()+"识别后的数字:"+s);}}
}

代码画验证码图片(一)相关推荐

  1. android+canvas+图片,android 开发 View _12_ 用Canvas 绘制一张图片(博客中演示用Canvas画验证码图片)...

    packagenet.yt.yuncare.widgets;importandroid.graphics.Bitmap;importandroid.graphics.Canvas;importandr ...

  2. 用来向登录页面输出验证码图片的一般处理程序页面

    这是自己以前做的B/S项目中的一个输出验证码图片的页面,没什么技术含量,希望高手们不要嘲笑,只是希望为需要帮助的人尽一点绵薄之力罢了! 页面简介:该页面是一个以ashx 为后缀的一般处理程序页面,用于 ...

  3. php验证码有图片没数字,php验证码图片不显示

    php 动态验证码,PHP如何开发短信验证码功能?,php验证码代码,php验证码图片不显示 欢迎登录清源教育官网 www.tsingyuan.cn 查看更多视频教程 php 验证码linux下只显示 ...

  4. 生成验证码图片的Java代码

    文章目录 验证码演示代码 请求资源路径为什么要添加一个随机数的参数 验证码演示代码 package priv.lwx.javaex.servlet_demo.web.servlet.response; ...

  5. python 识别登陆验证码图片(完整代码)_python 识别登录验证码图片功能的实现代码(完整代码)...

    在编写自动化测试用例的时候,每次登录都需要输入验证码,后来想把让python自己识别图片里的验证码,不需要自己手动登陆,所以查了一下识别功能怎么实现,做一下笔记. 首选导入一些用到的库,re.Imag ...

  6. php mysql 验证码代码_PHP_PHP 验证码的实现代码,checkcode.php 生成验证码图片, - phpStudy...

    PHP 验证码的实现代码 checkcode.php 生成验证码图片,还有变量 $_SESSION[check_pic]. 复制代码 代码如下: session_start(); for($i=0; ...

  7. php验证码图片看不清更换代码,php如何实现验证码看不清换一张的效果

    php实现验证码看不清换一张的方法:首先打开PHP代码文件:然后添加js代码"function changing(){document.getElementById('checkpic'). ...

  8. python 识别登陆验证码图片(完整代码)

    在编写自动化测试用例的时候,每次登录都需要输入验证码,后来想把让python自己识别图片里的验证码,不需要自己手动登陆,所以查了一下识别功能怎么实现,做一下笔记. 首选导入一些用到的库,re.Imag ...

  9. c语言将图像转换成字符画,25行Java代码将普通图片转换为字符画图片和文本的实现...

    本文主要介绍了25行Java代码将普通图片转换为字符画图片和文本的实现,分享给大家,具体如下: 原图 生成字符画文本(像素转换字符显示后,打开字符画显示相当于原图的好几倍大,不要用记事本打开,建议用n ...

最新文章

  1. 70+Python项目,面向初学者、中级和经验丰富的开发人员
  2. Excel VBA开发中数字签名的管理
  3. 增强型的for循环linkedlist_Java: 增强for循环针对list的时候,是严格按照list的顺序依次遍历的吗?...
  4. rabbitmq接收不到消息 防火墙_用PHP+RabbitMQ实现消息的发送和接收
  5. linux内核那些事之 VMA Gap
  6. linux 打zip gz tar,linux把文件压缩成.tar.gz的命令 | PT Ubuntu Blog
  7. java 赋值md5_Hook Java API以获得MD5加密前数据
  8. html表单文本框作用,HTML表单的用法
  9. java 按拼音模糊搜索汉字_数据查询支持中文拼音首字母模糊检索
  10. 51单片机外围模块——DS1302时钟模块
  11. MATLAB dir函数文件名排序问题
  12. Docker 运行常用容器
  13. html在搜索按钮中加放大镜,用 CSS3 画心形和搜索放大镜图标
  14. App Tamer for Mac
  15. html5 sketchpad,Sketchpad:基于html5在线图像绘画板
  16. CSS3实现对话气泡效果
  17. olivettifaces人脸识别之思考
  18. C++-计算体质指数BMI值 信息学奥赛
  19. CocoStudio基础教程(3)在程序中处理cocoStudio导出动画
  20. 徐家骏的华为十年:从DBA到副总裁的辛酸与喜悦[转载]

热门文章

  1. 万字长文带你解读Linux
  2. 《滴滴重MVVM框架Chameleon》架构篇读后感
  3. 第一章 c语言概述程序逻辑,第一章 程序逻辑与C语言概述
  4. try {}里有一个return语句,那么紧跟在这个try后的finally {}里的code会不会被执行,什么时候被执行,在return前还是后?
  5. Redis学习笔记(B站狂神说)(自己总结方便复习)
  6. Android如何设置渐变色背景 渐变shape
  7. phpyun 电脑调试wap版
  8. 【Openai】介绍
  9. 大学计算机实践教程在线阅读,第一部分 实验免费阅读_大学计算机基础实践教程免费全文_百度阅读...
  10. 凯悦250家店数据外泄 多家高端酒店存安全漏洞