为了破解图形验证码,AI需要大量的图片数据。为了简单获取大量的图形来喂给Ai模型训练,索性自己写一把。代码来一发。。

 
  1. import java.awt.Color;
  2. import java.awt.Graphics;
  3. import java.awt.image.BufferedImage;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. import java.io.OutputStream;
  7. import java.util.Random;
  8.  
  9. import org.patchca.background.BackgroundFactory;
  10. import org.patchca.color.ColorFactory;
  11. import org.patchca.filter.predefined.CurvesRippleFilterFactory;
  12. import org.patchca.filter.predefined.DiffuseRippleFilterFactory;
  13. import org.patchca.filter.predefined.DoubleRippleFilterFactory;
  14. import org.patchca.filter.predefined.MarbleRippleFilterFactory;
  15. import org.patchca.filter.predefined.WobbleRippleFilterFactory;
  16. import org.patchca.font.RandomFontFactory;
  17. import org.patchca.service.ConfigurableCaptchaService;
  18. import org.patchca.text.renderer.BestFitTextRenderer;
  19. import org.patchca.utils.encoder.EncoderHelper;
  20. import org.patchca.word.RandomWordFactory;
  21.  
  22. /**
  23. * 验证码工具
  24. */
  25. public class CaptchaUtils {
  26.  
  27. private static Random random = new Random();
  28. private static ConfigurableCaptchaService ccs;
  29. private static WobbleRippleFilterFactory wrff; // 摆波纹
  30. private static DoubleRippleFilterFactory doff; // 双波纹
  31. private static CurvesRippleFilterFactory crff; // 曲线波纹
  32. private static DiffuseRippleFilterFactory drff; // 漫纹波
  33. private static MarbleRippleFilterFactory mrff; // 大理石
  34.  
  35. private static void initialize(){
  36. if (ccs == null){
  37. synchronized (CaptchaUtils.class) {
  38. if (ccs == null){
  39. // 配置初始化
  40. ccs = new ConfigurableCaptchaService();
  41.  
  42. // 设置图片大小
  43. ccs.setWidth(100);
  44. ccs.setHeight(28);
  45.  
  46. // 设置文字数量
  47. RandomWordFactory wf = new RandomWordFactory();
  48. wf.setCharacters("ABDEFGHKMNRSWX2345689");
  49. wf.setMinLength(4);
  50. wf.setMaxLength(4);
  51. ccs.setWordFactory(wf);
  52.  
  53. // 设置字体大小
  54. RandomFontFactory ff = new RandomFontFactory();
  55. ff.setMinSize(28);
  56. ff.setMaxSize(28);
  57. ccs.setFontFactory(ff);
  58.  
  59. // 设置文字渲染边距
  60. BestFitTextRenderer tr = new BestFitTextRenderer();
  61. tr.setTopMargin(3);
  62. tr.setRightMargin(3);
  63. tr.setBottomMargin(3);
  64. tr.setLeftMargin(3);
  65. ccs.setTextRenderer(tr);
  66.  
  67. // 设置字体颜色
  68. ccs.setColorFactory(new ColorFactory() {
  69. @Override
  70. public Color getColor(int x) {
  71. int r = random.nextInt(90);
  72. int g = random.nextInt(90);
  73. int b = random.nextInt(90);
  74. return new Color(r, g, b);
  75. }
  76. });
  77.  
  78. // 设置背景
  79. ccs.setBackgroundFactory(new BackgroundFactory() {
  80. @Override
  81. public void fillBackground(BufferedImage image) {
  82. Graphics graphics = image.getGraphics();
  83. // 验证码图片的宽高
  84. int imgWidth = image.getWidth();
  85. int imgHeight = image.getHeight();
  86. // 填充为白色背景
  87. graphics.setColor(Color.WHITE);
  88. graphics.fillRect(0, 0, imgWidth, imgHeight);
  89. // 画 50 个噪点(颜色及位置随机)
  90. for (int i = 0; i < 50; i++) {
  91. // 随机颜色
  92. int rInt = random.nextInt(100)+50;
  93. int gInt = random.nextInt(100)+50;
  94. int bInt = random.nextInt(100)+50;
  95. graphics.setColor(new Color(rInt, gInt, bInt));
  96. // 随机位置
  97. int xInt = random.nextInt(imgWidth - 3);
  98. int yInt = random.nextInt(imgHeight - 2);
  99. // 随机旋转角度
  100. int sAngleInt = random.nextInt(360);
  101. int eAngleInt = random.nextInt(360);
  102. // 随机大小
  103. int wInt = random.nextInt(6);
  104. int hInt = random.nextInt(6);
  105. // 填充背景
  106. graphics.fillArc(xInt, yInt, wInt, hInt, sAngleInt, eAngleInt);
  107. // 画5条干扰线
  108. if (i % 10 == 0) {
  109. int xInt2 = random.nextInt(imgWidth);
  110. int yInt2 = random.nextInt(imgHeight);
  111. graphics.drawLine(xInt, yInt, xInt2, yInt2);
  112. }
  113. }
  114. }
  115. });
  116.  
  117. // 效果初始化
  118. wrff = new WobbleRippleFilterFactory(); // 摆波纹
  119. doff = new DoubleRippleFilterFactory(); // 双波纹
  120. crff = new CurvesRippleFilterFactory(ccs.getColorFactory()); // 曲线波纹
  121. drff = new DiffuseRippleFilterFactory(); // 漫纹波
  122. mrff = new MarbleRippleFilterFactory(); // 大理石
  123.  
  124. }
  125. }
  126. }
  127. }
  128.  
  129. /**
  130. * 生成验证码
  131. * @param request
  132. * @param response
  133. * @throws IOException
  134. * @return 验证码字符
  135. */
  136. public static String generateCaptcha(OutputStream outputStream) throws IOException{
  137.  
  138. // 初始化设置
  139. initialize();
  140.  
  141. // 随机选择一个样式
  142. switch (random.nextInt(3)) {
  143. case 0:
  144. ccs.setFilterFactory(wrff); // 摆波纹
  145. break;
  146. case 1:
  147. ccs.setFilterFactory(doff); // 双波纹
  148. break;
  149. case 2:
  150. ccs.setFilterFactory(crff); // 曲线波纹
  151. break;
  152. case 3:
  153. ccs.setFilterFactory(drff); // 漫纹波
  154. break;
  155. case 4:
  156. ccs.setFilterFactory(mrff); // 大理石
  157. break;
  158. }
  159.  
  160. // 生成验证码
  161. String s = EncoderHelper.getChallangeAndWriteImage(ccs, "png", outputStream);
  162. // System.out.println(s);
  163.  
  164. return s;
  165. }
  166.  
  167. public static void main(String[] args) throws IOException {
  168.  
  169. FileOutputStream fos = new FileOutputStream("d:\\captcha.png");
  170. String s = generateCaptcha(fos);
  171. System.out.println(s);
  172. fos.close();
  173.  
  174. }
  175. }

图片验证码给AI使用的更多相关文章

  1. 【python】带图片验证码的登录自动化实战

    近期在跟进新项目的时候,整体的业务线非常之长,会一直重复登录退出不同账号的这个流程,所以想从登录开始实现部分的自动化.因为是B/S的架构,所以采用的是selenium的框架来实现.大致实现步骤如下: ...

  2. 字符型图片验证码识别完整过程及Python实现

    字符型图片验证码识别完整过程及Python实现 1   摘要 验证码是目前互联网上非常常见也是非常重要的一个事物,充当着很多系统的 防火墙 功能,但是随时OCR技术的发展,验证码暴露出来的安全问题也越 ...

  3. android图片验证码--自绘控件

    自绘控件的内容都是自己绘制出来的 大致流程如下: 1.定义一个类继承view 使用TypedArray初始化属性集合 在view的构造方法中 有一个AttributeSet的参数 很明显是用来保存控件 ...

  4. webform(十)——图片水印和图片验证码

    两者都需要引入命名空间:using System.Drawing; 一.图片水印 前台Photoshuiyin.aspx代码: <div> <asp:FileUpload ID=&q ...

  5. Android-简单的图片验证码

    Android-图片验证码生成1.为啥要验证码?图片验证码在网络中使用的是比较普遍的.一般都是用来防止恶意破解密码.刷票.论坛灌水.刷页等.2.怎样的验证码比较好?验证码的获取方式无非就两种,一种是后 ...

  6. 在mvc中实现图片验证码的刷新

    首先,在项目模型(Model)层中建立一个生成图片验证码的类ValidationCodeHelper,代码如下: public class ValidationCodeHelper { //用户存取验 ...

  7. Webform 文件上传、 C#加图片水印 、 图片验证码

    文件上传:要使用控件 - FileUpload 1.如何判断是否选中文件? FileUpload.FileName - 选中文件的文件名,如果长度不大于0,那么说明没选中任何文件 js - f.val ...

  8. php 图片验证码生成 前后台验证

    自己从前一段时间做了个php小项目,关于生成图片验证码生成和后台的验证,把自己用到的东西总结一下,希望大家在用到相关问题的时候可以有一定的参考性. 首先,php验证码生成. 代码如下: 1.生成图像代 ...

  9. python 识别图片验证码报IOError

    说一下困扰了我一周的问题:识别图片验证码 本来我按照安装步骤(http://www.cnblogs.com/yeayee/p/4955506.html?utm_source=tuicool&u ...

随机推荐

  1. Gym - 101334E 多叉树遍历

    题意:给定一个字符串,求有多少种树与之对应,对应方式是,每次遍历左节点,没有了,就回溯: 分析:d[i,j] = sum(d[i+1,k-1],d[k,j]) (str[i]==str[k]); 坑点 ...

  2. POJ 1830 开关问题 【01矩阵 高斯消元】

    任意门:http://poj.org/problem?id=1830 开关问题 Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 1 ...

  3. 剑指offer23 从上往下打印二叉树

    没有把队列的头部弹出,出现内存错误:

  4. 【Linux-CentOS】【转-更正】使用CentOS DVD1 和DVD2做本地yum源

    原文在此.此文写的非常好,怕网络丢失,特转来,并做了更正. CentOS6以上版本一般都会提供一个DVD1和一个DVD2镜像,使用DVD1即可安装使用CentOS了,DVD2中存放了一些额外的软件包, ...

  5. 创建VS工程使用神经网络库——FANN

    编译: sourceforge上的FANN库带VS2010的工程,我机器上装的VS2005,用不了,愁人,只能手动创建工程了,编译不过,度娘不管用,FQ麻烦,用雅虎搜到一个工程的创建配置,调整配置试一 ...

  6. gcc扩展语法一:在表达式中的语句和声明

    在GNU C中包含在括号中的复合语句可以作为一个表达式.这就允许你在表达式中使用循环,switch和局部变量. 以前复合语句是包含在大括号中语句序列.在这种构造中,圆括号包围在大括号中.如下面的例子: ...

  7. toad for sql server

    数据库连接工具 toad for sql  sever

  8. WKWebView进度及title

    WKWebView进度及title WKWebView进度及title WKWebView 的estimatedProgress和title 都是KVO模式,所以可以添加监控: [webView ad ...

  9. excel导入到java/导出到excel

    package com.test.order.config; import com.test.order.domain.HavalDO; import org.apache.poi.ss.usermo ...

  10. 节约内存:Instagram的Redis实践

    Instagram可以说是网拍App的始祖级应用,也是当前最火热的拍照App之一,Instagram的照片数量已经达到3亿,而在Instagram里,我们需要知道每一张照片的作者是谁,下面就是Inst ...