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

 
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random; import org.patchca.background.BackgroundFactory;
import org.patchca.color.ColorFactory;
import org.patchca.filter.predefined.CurvesRippleFilterFactory;
import org.patchca.filter.predefined.DiffuseRippleFilterFactory;
import org.patchca.filter.predefined.DoubleRippleFilterFactory;
import org.patchca.filter.predefined.MarbleRippleFilterFactory;
import org.patchca.filter.predefined.WobbleRippleFilterFactory;
import org.patchca.font.RandomFontFactory;
import org.patchca.service.ConfigurableCaptchaService;
import org.patchca.text.renderer.BestFitTextRenderer;
import org.patchca.utils.encoder.EncoderHelper;
import org.patchca.word.RandomWordFactory; /**
* 验证码工具
*/
public class CaptchaUtils { private static Random random = new Random();
private static ConfigurableCaptchaService ccs;
private static WobbleRippleFilterFactory wrff; // 摆波纹
private static DoubleRippleFilterFactory doff; // 双波纹
private static CurvesRippleFilterFactory crff; // 曲线波纹
private static DiffuseRippleFilterFactory drff; // 漫纹波
private static MarbleRippleFilterFactory mrff; // 大理石 private static void initialize(){
if (ccs == null){
synchronized (CaptchaUtils.class) {
if (ccs == null){
// 配置初始化
ccs = new ConfigurableCaptchaService(); // 设置图片大小
ccs.setWidth(100);
ccs.setHeight(28); // 设置文字数量
RandomWordFactory wf = new RandomWordFactory();
wf.setCharacters("ABDEFGHKMNRSWX2345689");
wf.setMinLength(4);
wf.setMaxLength(4);
ccs.setWordFactory(wf); // 设置字体大小
RandomFontFactory ff = new RandomFontFactory();
ff.setMinSize(28);
ff.setMaxSize(28);
ccs.setFontFactory(ff); // 设置文字渲染边距
BestFitTextRenderer tr = new BestFitTextRenderer();
tr.setTopMargin(3);
tr.setRightMargin(3);
tr.setBottomMargin(3);
tr.setLeftMargin(3);
ccs.setTextRenderer(tr); // 设置字体颜色
ccs.setColorFactory(new ColorFactory() {
@Override
public Color getColor(int x) {
int r = random.nextInt(90);
int g = random.nextInt(90);
int b = random.nextInt(90);
return new Color(r, g, b);
}
}); // 设置背景
ccs.setBackgroundFactory(new BackgroundFactory() {
@Override
public void fillBackground(BufferedImage image) {
Graphics graphics = image.getGraphics();
// 验证码图片的宽高
int imgWidth = image.getWidth();
int imgHeight = image.getHeight();
// 填充为白色背景
graphics.setColor(Color.WHITE);
graphics.fillRect(0, 0, imgWidth, imgHeight);
// 画 50 个噪点(颜色及位置随机)
for (int i = 0; i < 50; i++) {
// 随机颜色
int rInt = random.nextInt(100)+50;
int gInt = random.nextInt(100)+50;
int bInt = random.nextInt(100)+50;
graphics.setColor(new Color(rInt, gInt, bInt));
// 随机位置
int xInt = random.nextInt(imgWidth - 3);
int yInt = random.nextInt(imgHeight - 2);
// 随机旋转角度
int sAngleInt = random.nextInt(360);
int eAngleInt = random.nextInt(360);
// 随机大小
int wInt = random.nextInt(6);
int hInt = random.nextInt(6);
// 填充背景
graphics.fillArc(xInt, yInt, wInt, hInt, sAngleInt, eAngleInt);
// 画5条干扰线
if (i % 10 == 0) {
int xInt2 = random.nextInt(imgWidth);
int yInt2 = random.nextInt(imgHeight);
graphics.drawLine(xInt, yInt, xInt2, yInt2);
}
}
}
}); // 效果初始化
wrff = new WobbleRippleFilterFactory(); // 摆波纹
doff = new DoubleRippleFilterFactory(); // 双波纹
crff = new CurvesRippleFilterFactory(ccs.getColorFactory()); // 曲线波纹
drff = new DiffuseRippleFilterFactory(); // 漫纹波
mrff = new MarbleRippleFilterFactory(); // 大理石 }
}
}
} /**
* 生成验证码
* @param request
* @param response
* @throws IOException
* @return 验证码字符
*/
public static String generateCaptcha(OutputStream outputStream) throws IOException{ // 初始化设置
initialize(); // 随机选择一个样式
switch (random.nextInt(3)) {
case 0:
ccs.setFilterFactory(wrff); // 摆波纹
break;
case 1:
ccs.setFilterFactory(doff); // 双波纹
break;
case 2:
ccs.setFilterFactory(crff); // 曲线波纹
break;
case 3:
ccs.setFilterFactory(drff); // 漫纹波
break;
case 4:
ccs.setFilterFactory(mrff); // 大理石
break;
} // 生成验证码
String s = EncoderHelper.getChallangeAndWriteImage(ccs, "png", outputStream);
// System.out.println(s); return s;
} public static void main(String[] args) throws IOException { FileOutputStream fos = new FileOutputStream("d:\\captcha.png");
String s = generateCaptcha(fos);
System.out.println(s);
fos.close(); }
}

图片验证码给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. Win10提示“您未连接到nvidia gpu的显示器”的解决方法

    显卡有Nvidia 和 ATI两个芯片,我们经常称他们为N卡和A卡,N卡更加注重于性能,而A卡则为颜色艳丽,画面更好.不过,最近一些windows10系统用户在使用N卡过程中,遇到了提示“您当前未使用 ...

  2. libxml2用xpath进行查找

    xml文档 <?xml version="1.0" encoding="UTF-8"?> <radios> <radio> ...

  3. (转)C++11使用emplace_back代替push_back (其中有关于右值引用)

    最近在写一段代码的时候,突然很好奇C++11中对push_back有没有什么改进以增加效率,上网搜了一些资料,发现果然新增了emplace_back方法,比push_back的效率要高很多. 首先,写 ...

  4. Python 用多线程上传和下载文件

    # -*- coding: utf-8 -*- __author__ = 'louis' from ftplib import FTP import multiprocessing import ti ...

  5. 2014-2015 ACM-ICPC East Central North America Regional Contest (ECNA 2014) A、Continued Fractions 【模拟连分数】

    任意门:http://codeforces.com/gym/100641/attachments Con + tin/(ued + Frac/tions) Time Limit: 3000/1000 ...

  6. Android学习笔记_69_android 支付宝之网页支付和快捷支付

    参考资料: https://b.alipay.com/order/productDetail.htm?productId=2013080604609654 https://b.alipay.com/o ...

  7. MFC通过URL下载并保存文件代码 转载

    http://blog.csdn.net/charlessimonyi/article/details/8666108?utm_source=tuicool&utm_medium=referr ...

  8. android(eclipse)编程中常见的java问题总结(四)

    0:java流:   流是具有方向的   在文件操作中java流分为字节流:Filereader和Filewriter字符流:FileOutputStream,FileInputSream   例如在 ...

  9. JS JavaScript中的文档碎片 DocumentFragement JS性能优化

    文档碎片是什么: 如果我们要在一个ul中添加100个li,如果不使用文档碎片,那么我们就需要使用append经常100次的追加,这会导致浏览器一直不停的渲染,是非常消耗资源的.但是如果我们使用文档碎片 ...

  10. 分享一个展示文章列表的CSS样式

    最近在帮朋友处理一个网站前端显示文章列表的时候,其中有个变通的思路,现整理出来留给有需要的朋友参考及自己备忘. 显示效果为:标题左对齐,日期右对齐. 标题和日期中间用常规的原点(“.”) 代替,显示效 ...