clean history

This commit is contained in:
Looly
2019-08-14 10:02:32 +08:00
commit 6b011af032
1215 changed files with 159913 additions and 0 deletions

24
hutool-captcha/pom.xml Normal file
View File

@@ -0,0 +1,24 @@
<?xml version='1.0' encoding='utf-8'?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<packaging>jar</packaging>
<parent>
<groupId>cn.hutool</groupId>
<artifactId>hutool-parent</artifactId>
<version>4.6.2-SNAPSHOT</version>
</parent>
<artifactId>hutool-captcha</artifactId>
<name>${project.artifactId}</name>
<description>Hutool 验证码工具</description>
<dependencies>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-core</artifactId>
<version>${project.parent.version}</version>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,226 @@
package cn.hutool.captcha;
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Font;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import cn.hutool.captcha.generator.CodeGenerator;
import cn.hutool.captcha.generator.RandomGenerator;
import cn.hutool.core.codec.Base64;
import cn.hutool.core.img.ImgUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IORuntimeException;
import cn.hutool.core.io.IoUtil;
/**
* 抽象验证码<br>
* 抽象验证码实现了验证码字符串的生成、验证,验证码图片的写出<br>
* 实现类通过实现{@link #createImage(String)} 方法生成图片对象
*
* @author looly
*
*/
public abstract class AbstractCaptcha implements ICaptcha {
private static final long serialVersionUID = 3180820918087507254L;
/** 图片的宽度 */
protected int width = 100;
/** 图片的高度 */
protected int height = 37;
/** 验证码干扰元素个数 */
protected int interfereCount = 15;
/** 字体 */
protected Font font;
/** 验证码 */
protected String code;
/** 验证码图片 */
protected byte[] imageBytes;
/** 验证码生成器 */
protected CodeGenerator generator;
/** 背景色 */
protected Color background;
/** 文字透明度 */
protected AlphaComposite textAlpha;
/**
* 构造,使用随机验证码生成器生成验证码
*
* @param width 图片宽
* @param height 图片高
* @param codeCount 字符个数
* @param interfereCount 验证码干扰元素个数
*/
public AbstractCaptcha(int width, int height, int codeCount, int interfereCount) {
this(width, height, new RandomGenerator(codeCount), interfereCount);
}
/**
* 构造
*
* @param width 图片宽
* @param height 图片高
* @param generator 验证码生成器
* @param interfereCount 验证码干扰元素个数
*/
public AbstractCaptcha(int width, int height, CodeGenerator generator, int interfereCount) {
this.width = width;
this.height = height;
this.generator = generator;
this.interfereCount = interfereCount;
// 字体高度设为验证码高度-2留边距
this.font = new Font(Font.SANS_SERIF, Font.PLAIN, (int) (this.height * 0.75));
}
@Override
public void createCode() {
generateCode();
final ByteArrayOutputStream out = new ByteArrayOutputStream();
ImgUtil.writePng(createImage(this.code), out);
this.imageBytes = out.toByteArray();
}
/**
* 生成验证码字符串
*
* @since 3.3.0
*/
protected void generateCode() {
this.code = generator.generate();
}
/**
* 根据生成的code创建验证码图片
*
* @param code 验证码
*/
protected abstract Image createImage(String code);
@Override
public String getCode() {
if(null == this.code) {
createCode();
}
return this.code;
}
@Override
public boolean verify(String userInputCode) {
return this.generator.verify(getCode(), userInputCode);
}
/**
* 验证码写出到文件
*
* @param path 文件路径
* @throws IORuntimeException IO异常
*/
public void write(String path) throws IORuntimeException {
this.write(FileUtil.touch(path));
}
/**
* 验证码写出到文件
*
* @param file 文件
* @throws IORuntimeException IO异常
*/
public void write(File file) throws IORuntimeException {
try (OutputStream out = FileUtil.getOutputStream(file)) {
this.write(out);
} catch (IOException e) {
throw new IORuntimeException(e);
}
}
@Override
public void write(OutputStream out) {
IoUtil.write(out, false, getImageBytes());
}
/**
* 获取图形验证码图片bytes
*
* @return 图形验证码图片bytes
* @since 4.5.17
*/
public byte[] getImageBytes() {
if (null == this.imageBytes) {
createCode();
}
return this.imageBytes;
}
/**
* 获取验证码图
*
* @return 验证码图
*/
public BufferedImage getImage() {
return ImgUtil.read(IoUtil.toStream(getImageBytes()));
}
/**
* 获得图片的Base64形式
*
* @return 图片的Base64
* @since 3.3.0
*/
public String getImageBase64() {
return Base64.encode(getImageBytes());
}
/**
* 自定义字体
*
* @param font 字体
*/
public void setFont(Font font) {
this.font = font;
}
/**
* 获取验证码生成器
*
* @return 验证码生成器
*/
public CodeGenerator getGenerator() {
return generator;
}
/**
* 设置验证码生成器
*
* @param generator 验证码生成器
*/
public void setGenerator(CodeGenerator generator) {
this.generator = generator;
}
/**
* 设置背景色
*
* @param background 背景色
* @since 4.1.22
*/
public void setBackground(Color background) {
this.background = background;
}
/**
* 设置文字透明度
*
* @param textAlpha 文字透明度取值0~11表示不透明
* @since 4.5.17
*/
public void setTextAlpha(float textAlpha) {
this.textAlpha = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, textAlpha);
}
}

View File

@@ -0,0 +1,86 @@
package cn.hutool.captcha;
/**
* 图形验证码工具
*
* @author looly
* @since 3.1.2
*/
public class CaptchaUtil {
/**
* 创建线干扰的验证码默认5位验证码150条干扰线
*
* @param width 图片宽
* @param height 图片高
* @return {@link LineCaptcha}
*/
public static LineCaptcha createLineCaptcha(int width, int height) {
return new LineCaptcha(width, height);
}
/**
* 创建线干扰的验证码
*
* @param width 图片宽
* @param height 图片高
* @param codeCount 字符个数
* @param lineCount 干扰线条数
* @return {@link LineCaptcha}
*/
public static LineCaptcha createLineCaptcha(int width, int height, int codeCount, int lineCount) {
return new LineCaptcha(width, height, codeCount, lineCount);
}
/**
* 创建圆圈干扰的验证码默认5位验证码15个干扰圈
*
* @param width 图片宽
* @param height 图片高
* @return {@link CircleCaptcha}
* @since 3.2.3
*/
public static CircleCaptcha createCircleCaptcha(int width, int height) {
return new CircleCaptcha(width, height);
}
/**
* 创建圆圈干扰的验证码
*
* @param width 图片宽
* @param height 图片高
* @param codeCount 字符个数
* @param circleCount 干扰圆圈条数
* @return {@link CircleCaptcha}
* @since 3.2.3
*/
public static CircleCaptcha createCircleCaptcha(int width, int height, int codeCount, int circleCount) {
return new CircleCaptcha(width, height, codeCount, circleCount);
}
/**
* 创建扭曲干扰的验证码默认5位验证码
*
* @param width 图片宽
* @param height 图片高
* @return {@link ShearCaptcha}
* @since 3.2.3
*/
public static ShearCaptcha createShearCaptcha(int width, int height) {
return new ShearCaptcha(width, height);
}
/**
* 创建扭曲干扰的验证码默认5位验证码
*
* @param width 图片宽
* @param height 图片高
* @param codeCount 字符个数
* @param thickness 干扰线宽度
* @return {@link ShearCaptcha}
* @since 3.3.0
*/
public static ShearCaptcha createShearCaptcha(int width, int height, int codeCount, int thickness) {
return new ShearCaptcha(width, height, codeCount, thickness);
}
}

View File

@@ -0,0 +1,101 @@
package cn.hutool.captcha;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.util.concurrent.ThreadLocalRandom;
import cn.hutool.core.img.GraphicsUtil;
import cn.hutool.core.img.ImgUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.RandomUtil;
/**
* 圆圈干扰验证码
*
* @author looly
* @since 3.2.3
*
*/
public class CircleCaptcha extends AbstractCaptcha {
private static final long serialVersionUID = -7096627300356535494L;
/**
* 构造
*
* @param width 图片宽
* @param height 图片高
*/
public CircleCaptcha(int width, int height) {
this(width, height, 5);
}
/**
* 构造
*
* @param width 图片宽
* @param height 图片高
* @param codeCount 字符个数
*/
public CircleCaptcha(int width, int height, int codeCount) {
this(width, height, codeCount, 15);
}
/**
* 构造
*
* @param width 图片宽
* @param height 图片高
* @param codeCount 字符个数
* @param interfereCount 验证码干扰元素个数
*/
public CircleCaptcha(int width, int height, int codeCount, int interfereCount) {
super(width, height, codeCount, interfereCount);
}
@Override
public Image createImage(String code) {
final BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
final Graphics2D g = ImgUtil.createGraphics(image, ObjectUtil.defaultIfNull(this.background, Color.WHITE));
// 随机画干扰圈圈
drawInterfere(g);
// 画字符串
drawString(g, code);
return image;
}
// ----------------------------------------------------------------------------------------------------- Private method start
/**
* 绘制字符串
*
* @param g {@link Graphics}画笔
* @param code 验证码
*/
private void drawString(Graphics2D g, String code) {
// 指定透明度
if (null != this.textAlpha) {
g.setComposite(this.textAlpha);
}
GraphicsUtil.drawStringColourful(g, code, this.font, this.width, this.height);
}
/**
* 画随机干扰
*
* @param g {@link Graphics2D}
*/
private void drawInterfere(Graphics2D g) {
final ThreadLocalRandom random = RandomUtil.getRandom();
for (int i = 0; i < this.interfereCount; i++) {
g.setColor(ImgUtil.randomColor(random));
g.drawOval(random.nextInt(width), random.nextInt(height), random.nextInt(height >> 1), random.nextInt(height >> 1));
}
}
// ----------------------------------------------------------------------------------------------------- Private method end
}

View File

@@ -0,0 +1,40 @@
package cn.hutool.captcha;
import java.io.OutputStream;
import java.io.Serializable;
/**
* 验证码接口,提供验证码对象接口定义
*
* @author looly
*
*/
public interface ICaptcha extends Serializable{
/**
* 创建验证码,实现类需同时生成随机验证码字符串和验证码图片
*/
void createCode();
/**
* 获取验证码的文字内容
*
* @return 验证码文字内容
*/
String getCode();
/**
* 验证验证码是否正确,建议忽略大小写
*
* @param userInputCode 用户输入的验证码
* @return 是否与生成的一直
*/
boolean verify(String userInputCode);
/**
* 将验证码写出到目标流中
*
* @param out 目标流
*/
void write(OutputStream out);
}

View File

@@ -0,0 +1,96 @@
package cn.hutool.captcha;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.util.concurrent.ThreadLocalRandom;
import cn.hutool.core.img.GraphicsUtil;
import cn.hutool.core.img.ImgUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.RandomUtil;
/**
* 使用干扰线方式生成的图形验证码
*
* @author looly
* @since 3.1.2
*/
public class LineCaptcha extends AbstractCaptcha {
private static final long serialVersionUID = 8691294460763091089L;
// -------------------------------------------------------------------- Constructor start
/**
* 构造默认5位验证码150条干扰线
*
* @param width 图片宽
* @param height 图片高
*/
public LineCaptcha(int width, int height) {
this(width, height, 5, 150);
}
/**
* 构造
*
* @param width 图片宽
* @param height 图片高
* @param codeCount 字符个数
* @param lineCount 干扰线条数
*/
public LineCaptcha(int width, int height, int codeCount, int lineCount) {
super(width, height, codeCount, lineCount);
}
// -------------------------------------------------------------------- Constructor end
@Override
public Image createImage(String code) {
// 图像buffer
final BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
final Graphics2D g = GraphicsUtil.createGraphics(image, ObjectUtil.defaultIfNull(this.background, Color.WHITE));
// 干扰线
drawInterfere(g);
// 字符串
drawString(g, code);
return image;
}
// ----------------------------------------------------------------------------------------------------- Private method start
/**
* 绘制字符串
*
* @param g {@link Graphics}画笔
* @param code 验证码
*/
private void drawString(Graphics2D g, String code) {
// 指定透明度
if (null != this.textAlpha) {
g.setComposite(this.textAlpha);
}
GraphicsUtil.drawStringColourful(g, code, this.font, this.width, this.height);
}
/**
* 绘制干扰线
*
* @param g {@link Graphics2D}画笔
*/
private void drawInterfere(Graphics2D g) {
final ThreadLocalRandom random = RandomUtil.getRandom();
// 干扰线
for (int i = 0; i < this.interfereCount; i++) {
int xs = random.nextInt(width);
int ys = random.nextInt(height);
int xe = xs + random.nextInt(width / 8);
int ye = ys + random.nextInt(height / 8);
g.setColor(ImgUtil.randomColor(random));
g.drawLine(xs, ys, xe, ye);
}
}
// ----------------------------------------------------------------------------------------------------- Private method start
}

View File

@@ -0,0 +1,201 @@
package cn.hutool.captcha;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import cn.hutool.core.img.GraphicsUtil;
import cn.hutool.core.img.ImgUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.RandomUtil;
/**
* 扭曲干扰验证码
*
* @author looly
* @since 3.2.3
*
*/
public class ShearCaptcha extends AbstractCaptcha {
private static final long serialVersionUID = -7096627300356535494L;
/**
* 构造
*
* @param width 图片宽
* @param height 图片高
*/
public ShearCaptcha(int width, int height) {
this(width, height, 5);
}
/**
* 构造
*
* @param width 图片宽
* @param height 图片高
* @param codeCount 字符个数
*/
public ShearCaptcha(int width, int height, int codeCount) {
this(width, height, codeCount, 4);
}
/**
* 构造
*
* @param width 图片宽
* @param height 图片高
* @param codeCount 字符个数
* @param thickness 干扰线宽度
*/
public ShearCaptcha(int width, int height, int codeCount, int thickness) {
super(width, height, codeCount, thickness);
}
@Override
public Image createImage(String code) {
final BufferedImage image = new BufferedImage(this.width, this.height, BufferedImage.TYPE_INT_RGB);
final Graphics2D g = GraphicsUtil.createGraphics(image, ObjectUtil.defaultIfNull(this.background, Color.WHITE));
// 画字符串
drawString(g, code);
// 扭曲
shear(g, this.width, this.height, ObjectUtil.defaultIfNull(this.background, Color.WHITE));
// 画干扰线
drawInterfere(g, 0, RandomUtil.randomInt(this.height) + 1, this.width, RandomUtil.randomInt(this.height) + 1, this.interfereCount, ImgUtil.randomColor());
return image;
}
// ----------------------------------------------------------------------------------------------------- Private method start
/**
* 绘制字符串
*
* @param g {@link Graphics}画笔
* @param code 验证码
*/
private void drawString(Graphics2D g, String code) {
// 指定透明度
if (null != this.textAlpha) {
g.setComposite(this.textAlpha);
}
GraphicsUtil.drawStringColourful(g, code, this.font, this.width, this.height);
}
/**
* 扭曲
*
* @param g {@link Graphics}
* @param w1 w1
* @param h1 h1
* @param color 颜色
*/
private void shear(Graphics g, int w1, int h1, Color color) {
shearX(g, w1, h1, color);
shearY(g, w1, h1, color);
}
/**
* X坐标扭曲
*
* @param g {@link Graphics}
* @param w1 宽
* @param h1 高
* @param color 颜色
*/
private void shearX(Graphics g, int w1, int h1, Color color) {
int period = RandomUtil.randomInt(this.width);
boolean borderGap = true;
int frames = 1;
int phase = RandomUtil.randomInt(2);
for (int i = 0; i < h1; i++) {
double d = (double) (period >> 1) * Math.sin((double) i / (double) period + (6.2831853071795862D * (double) phase) / (double) frames);
g.copyArea(0, i, w1, 1, (int) d, 0);
if (borderGap) {
g.setColor(color);
g.drawLine((int) d, i, 0, i);
g.drawLine((int) d + w1, i, w1, i);
}
}
}
/**
* Y坐标扭曲
*
* @param g {@link Graphics}
* @param w1 宽
* @param h1 高
* @param color 颜色
*/
private void shearY(Graphics g, int w1, int h1, Color color) {
int period = RandomUtil.randomInt(this.height >> 1);
int frames = 20;
int phase = 7;
for (int i = 0; i < w1; i++) {
double d = (double) (period >> 1) * Math.sin((double) i / (double) period + (6.2831853071795862D * (double) phase) / (double) frames);
g.copyArea(i, 0, 1, h1, 0, (int) d);
g.setColor(color);
// 擦除原位置的痕迹
g.drawLine(i, (int) d, i, 0);
g.drawLine(i, (int) d + h1, i, h1);
}
}
/**
* 干扰线
*
* @param g {@link Graphics}
* @param x1x1
* @param y1 y1
* @param x2 x2
* @param y2 y2
* @param thickness 粗细
* @param c 颜色
*/
private void drawInterfere(Graphics g, int x1, int y1, int x2, int y2, int thickness, Color c) {
// The thick line is in fact a filled polygon
g.setColor(c);
int dX = x2 - x1;
int dY = y2 - y1;
// line length
double lineLength = Math.sqrt(dX * dX + dY * dY);
double scale = (double) (thickness) / (2 * lineLength);
// The x and y increments from an endpoint needed to create a
// rectangle...
double ddx = -scale * (double) dY;
double ddy = scale * (double) dX;
ddx += (ddx > 0) ? 0.5 : -0.5;
ddy += (ddy > 0) ? 0.5 : -0.5;
int dx = (int) ddx;
int dy = (int) ddy;
// Now we can compute the corner points...
int xPoints[] = new int[4];
int yPoints[] = new int[4];
xPoints[0] = x1 + dx;
yPoints[0] = y1 + dy;
xPoints[1] = x1 - dx;
yPoints[1] = y1 - dy;
xPoints[2] = x2 - dx;
yPoints[2] = y2 - dy;
xPoints[3] = x2 + dx;
yPoints[3] = y2 + dy;
g.fillPolygon(xPoints, yPoints, 4);
}
// ----------------------------------------------------------------------------------------------------- Private method end
}

View File

@@ -0,0 +1,48 @@
package cn.hutool.captcha.generator;
import cn.hutool.core.util.RandomUtil;
/**
* 随机字符验证码生成器<br>
* 可以通过传入的基础集合和长度随机生成验证码字符
*
* @author looly
* @since 4.1.2
*/
public abstract class AbstractGenerator implements CodeGenerator {
private static final long serialVersionUID = 8685744597154953479L;
/** 基础字符集合,用于随机获取字符串的字符集合 */
protected String baseStr;
/** 验证码长度 */
protected int length;
/**
* 构造,使用字母+数字做为基础
*
* @param count 生成验证码长度
*/
public AbstractGenerator(int count) {
this(RandomUtil.BASE_CHAR_NUMBER, count);
}
/**
* 构造
*
* @param baseStr 基础字符集合,用于随机获取字符串的字符集合
* @param length 生成验证码长度
*/
public AbstractGenerator(String baseStr, int length) {
this.baseStr = baseStr;
this.length = length;
}
/**
* 获取长度验证码
*
* @return 验证码长度
*/
public int getLength() {
return this.length;
}
}

View File

@@ -0,0 +1,28 @@
package cn.hutool.captcha.generator;
import java.io.Serializable;
/**
* 验证码文字生成器
*
* @author looly
* @since 4.1.2
*/
public interface CodeGenerator extends Serializable{
/**
* 生成验证码
*
* @return 验证码
*/
public String generate();
/**
* 验证用户输入的字符串是否与生成的验证码匹配<br>
* 用户通过实现此方法定义验证码匹配方式
*
* @param code 生成的随机验证码
* @param userInputCode 用户输入的验证码
* @return 是否验证通过
*/
public boolean verify(String code, String userInputCode);
}

View File

@@ -0,0 +1,96 @@
package cn.hutool.captcha.generator;
import cn.hutool.core.util.CharUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
/**
* 数字计算验证码生成器
*
* @author looly
* @since 4.1.2
*/
public class MathGenerator implements CodeGenerator {
private static final long serialVersionUID = -5514819971774091076L;
private static final String operators = "+-*";
/** 参与计算数字最大长度 */
private int numberLength;
/**
* 构造
*/
public MathGenerator() {
this(2);
}
/**
* 构造
*
* @param numberLength 参与计算最大数字位数
*/
public MathGenerator(int numberLength) {
this.numberLength = numberLength;
}
@Override
public String generate() {
final int limit = getLimit();
String number1 = Integer.toString(RandomUtil.randomInt(limit));
String number2 = Integer.toString(RandomUtil.randomInt(limit));
number1 = StrUtil.padAfter(number1, this.numberLength, CharUtil.SPACE);
number2 = StrUtil.padAfter(number2, this.numberLength, CharUtil.SPACE);
final String code = StrUtil.builder()//
.append(number1)//
.append(RandomUtil.randomChar(operators))//
.append(number2)//
.append('=').toString();
return code;
}
@Override
public boolean verify(String code, String userInputCode) {
int result;
try {
result = Integer.parseInt(userInputCode);
} catch (NumberFormatException e) {
// 用户输入非数字
return false;
}
final int a = Integer.parseInt(StrUtil.sub(code, 0, this.numberLength).trim());
final char operator = code.charAt(this.numberLength);
final int b = Integer.parseInt(StrUtil.sub(code, this.numberLength + 1, this.numberLength + 1 + this.numberLength).trim());
switch (operator) {
case '+':
return (a + b) == result;
case '-':
return (a - b) == result;
case '*':
return (a * b) == result;
default:
return false;
}
}
/**
* 获取长度验证码
*
* @return 验证码长度
*/
public int getLength() {
return this.numberLength * 2 + 2;
}
/**
* 根据长度获取参与计算数字最大值
*
* @return 最大值
*/
private int getLimit() {
return Integer.parseInt("1" + StrUtil.repeat('0', this.numberLength));
}
}

View File

@@ -0,0 +1,47 @@
package cn.hutool.captcha.generator;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
/**
* 随机字符验证码生成器<br>
* 可以通过传入的基础集合和长度随机生成验证码字符
*
* @author looly
* @since 4.1.2
*/
public class RandomGenerator extends AbstractGenerator {
private static final long serialVersionUID = -7802758587765561876L;
/**
* 构造,使用字母+数字做为基础
*
* @param count 生成验证码长度
*/
public RandomGenerator(int count) {
super(count);
}
/**
* 构造
*
* @param baseStr 基础字符集合,用于随机获取字符串的字符集合
* @param length 生成验证码长度
*/
public RandomGenerator(String baseStr, int length) {
super(baseStr, length);
}
@Override
public String generate() {
return RandomUtil.randomString(this.baseStr, this.length);
}
@Override
public boolean verify(String code, String userInputCode) {
if (StrUtil.isNotBlank(userInputCode)) {
return StrUtil.equalsIgnoreCase(code, userInputCode);
}
return false;
}
}

View File

@@ -0,0 +1,7 @@
/**
* 验证码生成策略实现
*
* @author looly
* @since 4.1.2
*/
package cn.hutool.captcha.generator;

View File

@@ -0,0 +1,7 @@
/**
* 图片验证码实现
*
* @author looly
*
*/
package cn.hutool.captcha;

View File

@@ -0,0 +1,95 @@
package cn.hutool.captcha;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
import cn.hutool.captcha.generator.MathGenerator;
import cn.hutool.core.lang.Console;
/**
* 直线干扰验证码单元测试
*
* @author looly
*
*/
public class CaptchaTest {
@Test
public void lineCaptchaTest1() {
// 定义图形验证码的长和宽
LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(200, 100);
Assert.assertNotNull(lineCaptcha.getCode());
Assert.assertTrue(lineCaptcha.verify(lineCaptcha.getCode()));
}
@Test
@Ignore
public void lineCaptchaWithMathTest() {
// 定义图形验证码的长和宽
LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(200, 80);
lineCaptcha.setGenerator(new MathGenerator());
lineCaptcha.setTextAlpha(0.8f);
lineCaptcha.write("f:/captcha/math.png");
}
@Test
@Ignore
public void lineCaptchaTest2() {
// 定义图形验证码的长和宽
LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(200, 100);
// LineCaptcha lineCaptcha = new LineCaptcha(200, 100, 4, 150);
// 图形验证码写出,可以写出到文件,也可以写出到流
lineCaptcha.write("f:/captcha/line.png");
Console.log(lineCaptcha.getCode());
// 验证图形验证码的有效性返回boolean值
lineCaptcha.verify("1234");
lineCaptcha.createCode();
lineCaptcha.write("f:/captcha/line2.png");
Console.log(lineCaptcha.getCode());
// 验证图形验证码的有效性返回boolean值
lineCaptcha.verify("1234");
}
@Test
@Ignore
public void circleCaptchaTest() {
// 定义图形验证码的长和宽
CircleCaptcha captcha = CaptchaUtil.createCircleCaptcha(200, 100, 4, 20);
// CircleCaptcha captcha = new CircleCaptcha(200, 100, 4, 20);
// 图形验证码写出,可以写出到文件,也可以写出到流
captcha.write("f:/captcha/circle.png");
// 验证图形验证码的有效性返回boolean值
captcha.verify("1234");
}
@Test
@Ignore
public void ShearCaptchaTest() {
// 定义图形验证码的长和宽
ShearCaptcha captcha = CaptchaUtil.createShearCaptcha(203, 100, 4, 4);
// ShearCaptcha captcha = new ShearCaptcha(200, 100, 4, 4);
// 图形验证码写出,可以写出到文件,也可以写出到流
captcha.write("f:/captcha/shear.png");
// 验证图形验证码的有效性返回boolean值
captcha.verify("1234");
}
@Test
@Ignore
public void ShearCaptchaWithMathTest() {
// 定义图形验证码的长和宽
ShearCaptcha captcha = CaptchaUtil.createShearCaptcha(200, 45, 4, 4);
captcha.setGenerator(new MathGenerator());
// ShearCaptcha captcha = new ShearCaptcha(200, 100, 4, 4);
// 图形验证码写出,可以写出到文件,也可以写出到流
captcha.write("f:/captcha/shear_math.png");
// 验证图形验证码的有效性返回boolean值
captcha.verify("1234");
}
}

View File

@@ -0,0 +1,15 @@
package cn.hutool.captcha;
import org.junit.Ignore;
import org.junit.Test;
public class CaptchaUtilTest {
@Test
@Ignore
public void createTest() {
for(int i = 0; i < 1; i++) {
CaptchaUtil.createShearCaptcha(320, 240);
}
}
}