mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
图片验证码
This commit is contained in:
@@ -1,105 +1,111 @@
|
||||
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;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
/**
|
||||
* 直线干扰验证码单元测试
|
||||
*
|
||||
* @author looly
|
||||
*
|
||||
* @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
|
||||
public void lineCaptchaTest1() {
|
||||
// 定义图形验证码的长和宽
|
||||
LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(200, 100);
|
||||
Assert.assertNotNull(lineCaptcha.getCode());
|
||||
Assert.assertTrue(lineCaptcha.verify(lineCaptcha.getCode()));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void lineCaptchaTest3() {
|
||||
// 定义图形验证码的长和宽
|
||||
LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(200, 70, 4, 15);
|
||||
lineCaptcha.setBackground(Color.yellow);
|
||||
lineCaptcha.write("f:/test/captcha/tellow.png");
|
||||
}
|
||||
|
||||
@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 lineCaptchaTest3() {
|
||||
// 定义图形验证码的长和宽
|
||||
LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(200, 70, 4, 15);
|
||||
lineCaptcha.setBackground(Color.yellow);
|
||||
lineCaptcha.write("f:/test/captcha/tellow.png");
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void lineCaptchaTest2() {
|
||||
@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");
|
||||
}
|
||||
|
||||
// 定义图形验证码的长和宽
|
||||
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");
|
||||
@Test
|
||||
@Ignore
|
||||
public void lineCaptchaTest2() {
|
||||
|
||||
lineCaptcha.createCode();
|
||||
lineCaptcha.write("f:/captcha/line2.png");
|
||||
Console.log(lineCaptcha.getCode());
|
||||
// 验证图形验证码的有效性,返回boolean值
|
||||
lineCaptcha.verify("1234");
|
||||
}
|
||||
// 定义图形验证码的长和宽
|
||||
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");
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void circleCaptchaTest() {
|
||||
lineCaptcha.createCode();
|
||||
lineCaptcha.write("f:/captcha/line2.png");
|
||||
Console.log(lineCaptcha.getCode());
|
||||
// 验证图形验证码的有效性,返回boolean值
|
||||
lineCaptcha.verify("1234");
|
||||
}
|
||||
|
||||
// 定义图形验证码的长和宽
|
||||
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 circleCaptchaTest() {
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void ShearCaptchaTest() {
|
||||
// 定义图形验证码的长和宽
|
||||
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");
|
||||
}
|
||||
|
||||
// 定义图形验证码的长和宽
|
||||
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");
|
||||
}
|
||||
@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");
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void GifCaptchaTest() {
|
||||
GifCaptcha captcha = CaptchaUtil.createGifCaptcha(200, 100, 4);
|
||||
captcha.write("f:/gif_captcha.gif");
|
||||
assert captcha.verify(captcha.getCode());
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user