mirror of
https://gitee.com/dromara/hutool.git
synced 2025-04-30 04:45:55 +08:00
add constructor
This commit is contained in:
parent
4ccf795ee0
commit
efc4ca63a2
@ -27,7 +27,7 @@ public class CaptchaUtil {
|
||||
* @param height 图片高
|
||||
* @return {@link LineCaptcha}
|
||||
*/
|
||||
public static LineCaptcha createLineCaptcha(final int width, final int height) {
|
||||
public static LineCaptcha ofLineCaptcha(final int width, final int height) {
|
||||
return new LineCaptcha(width, height);
|
||||
}
|
||||
|
||||
@ -40,7 +40,7 @@ public class CaptchaUtil {
|
||||
* @param lineCount 干扰线条数
|
||||
* @return {@link LineCaptcha}
|
||||
*/
|
||||
public static LineCaptcha createLineCaptcha(final int width, final int height, final int codeCount, final int lineCount) {
|
||||
public static LineCaptcha ofLineCaptcha(final int width, final int height, final int codeCount, final int lineCount) {
|
||||
return new LineCaptcha(width, height, codeCount, lineCount);
|
||||
}
|
||||
|
||||
@ -52,7 +52,7 @@ public class CaptchaUtil {
|
||||
* @return {@link CircleCaptcha}
|
||||
* @since 3.2.3
|
||||
*/
|
||||
public static CircleCaptcha createCircleCaptcha(final int width, final int height) {
|
||||
public static CircleCaptcha ofCircleCaptcha(final int width, final int height) {
|
||||
return new CircleCaptcha(width, height);
|
||||
}
|
||||
|
||||
@ -66,7 +66,7 @@ public class CaptchaUtil {
|
||||
* @return {@link CircleCaptcha}
|
||||
* @since 3.2.3
|
||||
*/
|
||||
public static CircleCaptcha createCircleCaptcha(final int width, final int height, final int codeCount, final int circleCount) {
|
||||
public static CircleCaptcha ofCircleCaptcha(final int width, final int height, final int codeCount, final int circleCount) {
|
||||
return new CircleCaptcha(width, height, codeCount, circleCount);
|
||||
}
|
||||
|
||||
@ -78,7 +78,7 @@ public class CaptchaUtil {
|
||||
* @return {@link ShearCaptcha}
|
||||
* @since 3.2.3
|
||||
*/
|
||||
public static ShearCaptcha createShearCaptcha(final int width, final int height) {
|
||||
public static ShearCaptcha ofShearCaptcha(final int width, final int height) {
|
||||
return new ShearCaptcha(width, height);
|
||||
}
|
||||
|
||||
@ -92,7 +92,7 @@ public class CaptchaUtil {
|
||||
* @return {@link ShearCaptcha}
|
||||
* @since 3.3.0
|
||||
*/
|
||||
public static ShearCaptcha createShearCaptcha(final int width, final int height, final int codeCount, final int thickness) {
|
||||
public static ShearCaptcha ofShearCaptcha(final int width, final int height, final int codeCount, final int thickness) {
|
||||
return new ShearCaptcha(width, height, codeCount, thickness);
|
||||
}
|
||||
|
||||
@ -103,7 +103,7 @@ public class CaptchaUtil {
|
||||
* @param height 高
|
||||
* @return {@link GifCaptcha}
|
||||
*/
|
||||
public static GifCaptcha createGifCaptcha(final int width, final int height) {
|
||||
public static GifCaptcha ofGifCaptcha(final int width, final int height) {
|
||||
return new GifCaptcha(width, height);
|
||||
}
|
||||
|
||||
@ -115,7 +115,7 @@ public class CaptchaUtil {
|
||||
* @param codeCount 字符个数
|
||||
* @return {@link GifCaptcha}
|
||||
*/
|
||||
public static GifCaptcha createGifCaptcha(final int width, final int height, final int codeCount) {
|
||||
public static GifCaptcha ofGifCaptcha(final int width, final int height, final int codeCount) {
|
||||
return new GifCaptcha(width, height, codeCount);
|
||||
}
|
||||
}
|
||||
|
@ -14,6 +14,8 @@ package org.dromara.hutool.swing.captcha;
|
||||
|
||||
import org.dromara.hutool.core.util.ObjUtil;
|
||||
import org.dromara.hutool.core.util.RandomUtil;
|
||||
import org.dromara.hutool.swing.captcha.generator.CodeGenerator;
|
||||
import org.dromara.hutool.swing.captcha.generator.RandomGenerator;
|
||||
import org.dromara.hutool.swing.img.color.ColorUtil;
|
||||
import org.dromara.hutool.swing.img.GraphicsUtil;
|
||||
|
||||
@ -63,7 +65,19 @@ public class CircleCaptcha extends AbstractCaptcha {
|
||||
* @param interfereCount 验证码干扰元素个数
|
||||
*/
|
||||
public CircleCaptcha(final int width, final int height, final int codeCount, final int interfereCount) {
|
||||
super(width, height, codeCount, interfereCount);
|
||||
this(width, height, new RandomGenerator(codeCount), interfereCount);
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*
|
||||
* @param width 图片宽
|
||||
* @param height 图片高
|
||||
* @param generator 验证码生成器
|
||||
* @param interfereCount 验证码干扰元素个数
|
||||
*/
|
||||
public CircleCaptcha(final int width, final int height, final CodeGenerator generator, final int interfereCount) {
|
||||
super(width, height, generator, interfereCount);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -16,6 +16,8 @@ package org.dromara.hutool.swing.captcha;
|
||||
import org.dromara.hutool.core.util.ObjUtil;
|
||||
import org.dromara.hutool.core.util.RandomUtil;
|
||||
import com.madgag.gif.fmsware.AnimatedGifEncoder;
|
||||
import org.dromara.hutool.swing.captcha.generator.CodeGenerator;
|
||||
import org.dromara.hutool.swing.captcha.generator.RandomGenerator;
|
||||
|
||||
import java.awt.AlphaComposite;
|
||||
import java.awt.Color;
|
||||
@ -59,7 +61,29 @@ public class GifCaptcha extends AbstractCaptcha {
|
||||
* @param codeCount 验证码个数
|
||||
*/
|
||||
public GifCaptcha(final int width, final int height, final int codeCount) {
|
||||
super(width, height, codeCount, 10);
|
||||
this(width, height, codeCount, 10);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param width 验证码宽度
|
||||
* @param height 验证码高度
|
||||
* @param codeCount 验证码个数
|
||||
* @param interfereCount 干扰个数
|
||||
*/
|
||||
public GifCaptcha(final int width, final int height, final int codeCount, final int interfereCount) {
|
||||
this(width, height, new RandomGenerator(codeCount), interfereCount);
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*
|
||||
* @param width 图片宽
|
||||
* @param height 图片高
|
||||
* @param generator 验证码生成器
|
||||
* @param interfereCount 验证码干扰元素个数
|
||||
*/
|
||||
public GifCaptcha(final int width, final int height, final CodeGenerator generator, final int interfereCount) {
|
||||
super(width, height, generator, interfereCount);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -178,9 +202,9 @@ public class GifCaptcha extends AbstractCaptcha {
|
||||
g2d.setComposite(ac);
|
||||
g2d.setColor(fontColor[i]);
|
||||
g2d.drawOval(
|
||||
RandomUtil.randomInt(width),
|
||||
RandomUtil.randomInt(height),
|
||||
RandomUtil.randomInt(5, 30), 5 + RandomUtil.randomInt(5, 30)
|
||||
RandomUtil.randomInt(width),
|
||||
RandomUtil.randomInt(height),
|
||||
RandomUtil.randomInt(5, 30), 5 + RandomUtil.randomInt(5, 30)
|
||||
);//绘制椭圆边框
|
||||
g2d.drawString(words[i] + "", x + (font.getSize() + m) * i, y);
|
||||
}
|
||||
@ -223,8 +247,8 @@ public class GifCaptcha extends AbstractCaptcha {
|
||||
max = 255;
|
||||
}
|
||||
return new Color(
|
||||
RandomUtil.randomInt(min, max),
|
||||
RandomUtil.randomInt(min, max),
|
||||
RandomUtil.randomInt(min, max));
|
||||
RandomUtil.randomInt(min, max),
|
||||
RandomUtil.randomInt(min, max),
|
||||
RandomUtil.randomInt(min, max));
|
||||
}
|
||||
}
|
||||
|
@ -14,6 +14,8 @@ package org.dromara.hutool.swing.captcha;
|
||||
|
||||
import org.dromara.hutool.core.util.ObjUtil;
|
||||
import org.dromara.hutool.core.util.RandomUtil;
|
||||
import org.dromara.hutool.swing.captcha.generator.CodeGenerator;
|
||||
import org.dromara.hutool.swing.captcha.generator.RandomGenerator;
|
||||
import org.dromara.hutool.swing.img.color.ColorUtil;
|
||||
import org.dromara.hutool.swing.img.GraphicsUtil;
|
||||
|
||||
@ -53,7 +55,19 @@ public class LineCaptcha extends AbstractCaptcha {
|
||||
* @param lineCount 干扰线条数
|
||||
*/
|
||||
public LineCaptcha(final int width, final int height, final int codeCount, final int lineCount) {
|
||||
super(width, height, codeCount, lineCount);
|
||||
this(width, height, new RandomGenerator(codeCount), lineCount);
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*
|
||||
* @param width 图片宽
|
||||
* @param height 图片高
|
||||
* @param generator 验证码生成器
|
||||
* @param interfereCount 验证码干扰元素个数
|
||||
*/
|
||||
public LineCaptcha(final int width, final int height, final CodeGenerator generator, final int interfereCount) {
|
||||
super(width, height, generator, interfereCount);
|
||||
}
|
||||
// -------------------------------------------------------------------- Constructor end
|
||||
|
||||
|
@ -14,6 +14,8 @@ package org.dromara.hutool.swing.captcha;
|
||||
|
||||
import org.dromara.hutool.core.util.ObjUtil;
|
||||
import org.dromara.hutool.core.util.RandomUtil;
|
||||
import org.dromara.hutool.swing.captcha.generator.CodeGenerator;
|
||||
import org.dromara.hutool.swing.captcha.generator.RandomGenerator;
|
||||
import org.dromara.hutool.swing.img.color.ColorUtil;
|
||||
import org.dromara.hutool.swing.img.GraphicsUtil;
|
||||
|
||||
@ -63,7 +65,19 @@ public class ShearCaptcha extends AbstractCaptcha {
|
||||
* @param thickness 干扰线宽度
|
||||
*/
|
||||
public ShearCaptcha(final int width, final int height, final int codeCount, final int thickness) {
|
||||
super(width, height, codeCount, thickness);
|
||||
this(width, height, new RandomGenerator(codeCount), thickness);
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*
|
||||
* @param width 图片宽
|
||||
* @param height 图片高
|
||||
* @param generator 验证码生成器
|
||||
* @param interfereCount 验证码干扰元素个数
|
||||
*/
|
||||
public ShearCaptcha(final int width, final int height, final CodeGenerator generator, final int interfereCount) {
|
||||
super(width, height, generator, interfereCount);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -30,7 +30,7 @@ public class CaptchaTest {
|
||||
@Test
|
||||
public void lineCaptchaTest1() {
|
||||
// 定义图形验证码的长和宽
|
||||
final LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(200, 100);
|
||||
final LineCaptcha lineCaptcha = CaptchaUtil.ofLineCaptcha(200, 100);
|
||||
Assertions.assertNotNull(lineCaptcha.getCode());
|
||||
Assertions.assertTrue(lineCaptcha.verify(lineCaptcha.getCode()));
|
||||
}
|
||||
@ -39,7 +39,7 @@ public class CaptchaTest {
|
||||
@Disabled
|
||||
public void lineCaptchaTest3() {
|
||||
// 定义图形验证码的长和宽
|
||||
final LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(200, 70, 4, 15);
|
||||
final LineCaptcha lineCaptcha = CaptchaUtil.ofLineCaptcha(200, 70, 4, 15);
|
||||
lineCaptcha.setBackground(Color.yellow);
|
||||
lineCaptcha.write("f:/test/captcha/tellow.png");
|
||||
}
|
||||
@ -48,7 +48,7 @@ public class CaptchaTest {
|
||||
@Disabled
|
||||
public void lineCaptchaWithMathTest() {
|
||||
// 定义图形验证码的长和宽
|
||||
final LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(200, 80);
|
||||
final LineCaptcha lineCaptcha = CaptchaUtil.ofLineCaptcha(200, 80);
|
||||
lineCaptcha.setGenerator(new MathGenerator());
|
||||
lineCaptcha.setTextAlpha(0.8f);
|
||||
lineCaptcha.write("f:/captcha/math.png");
|
||||
@ -59,7 +59,7 @@ public class CaptchaTest {
|
||||
public void lineCaptchaTest2() {
|
||||
|
||||
// 定义图形验证码的长和宽
|
||||
final LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(200, 100);
|
||||
final LineCaptcha lineCaptcha = CaptchaUtil.ofLineCaptcha(200, 100);
|
||||
// LineCaptcha lineCaptcha = new LineCaptcha(200, 100, 4, 150);
|
||||
// 图形验证码写出,可以写出到文件,也可以写出到流
|
||||
lineCaptcha.write("f:/captcha/line.png");
|
||||
@ -79,7 +79,7 @@ public class CaptchaTest {
|
||||
public void circleCaptchaTest() {
|
||||
|
||||
// 定义图形验证码的长和宽
|
||||
final CircleCaptcha captcha = CaptchaUtil.createCircleCaptcha(200, 100, 4, 20);
|
||||
final CircleCaptcha captcha = CaptchaUtil.ofCircleCaptcha(200, 100, 4, 20);
|
||||
// CircleCaptcha captcha = new CircleCaptcha(200, 100, 4, 20);
|
||||
// 图形验证码写出,可以写出到文件,也可以写出到流
|
||||
captcha.write("f:/captcha/circle.png");
|
||||
@ -92,7 +92,7 @@ public class CaptchaTest {
|
||||
public void shearCaptchaTest() {
|
||||
|
||||
// 定义图形验证码的长和宽
|
||||
final ShearCaptcha captcha = CaptchaUtil.createShearCaptcha(203, 100, 4, 4);
|
||||
final ShearCaptcha captcha = CaptchaUtil.ofShearCaptcha(203, 100, 4, 4);
|
||||
// ShearCaptcha captcha = new ShearCaptcha(200, 100, 4, 4);
|
||||
// 图形验证码写出,可以写出到文件,也可以写出到流
|
||||
captcha.write("f:/captcha/shear.png");
|
||||
@ -116,7 +116,7 @@ public class CaptchaTest {
|
||||
@Disabled
|
||||
public void ShearCaptchaWithMathTest() {
|
||||
// 定义图形验证码的长和宽
|
||||
final ShearCaptcha captcha = CaptchaUtil.createShearCaptcha(200, 45, 4, 4);
|
||||
final ShearCaptcha captcha = CaptchaUtil.ofShearCaptcha(200, 45, 4, 4);
|
||||
captcha.setGenerator(new MathGenerator());
|
||||
// ShearCaptcha captcha = new ShearCaptcha(200, 100, 4, 4);
|
||||
// 图形验证码写出,可以写出到文件,也可以写出到流
|
||||
@ -128,7 +128,7 @@ public class CaptchaTest {
|
||||
@Test
|
||||
@Disabled
|
||||
public void GifCaptchaTest() {
|
||||
final GifCaptcha captcha = CaptchaUtil.createGifCaptcha(200, 100, 4);
|
||||
final GifCaptcha captcha = CaptchaUtil.ofGifCaptcha(200, 100, 4);
|
||||
captcha.write("d:/test/gif_captcha.gif");
|
||||
assert captcha.verify(captcha.getCode());
|
||||
}
|
||||
@ -136,7 +136,7 @@ public class CaptchaTest {
|
||||
@Test
|
||||
@Disabled
|
||||
public void bgTest(){
|
||||
final LineCaptcha captcha = CaptchaUtil.createLineCaptcha(200, 100, 4, 1);
|
||||
final LineCaptcha captcha = CaptchaUtil.ofLineCaptcha(200, 100, 4, 1);
|
||||
captcha.setBackground(Color.WHITE);
|
||||
captcha.write("d:/test/test.jpg");
|
||||
}
|
||||
|
@ -21,7 +21,7 @@ public class CaptchaUtilTest {
|
||||
@Disabled
|
||||
public void createTest() {
|
||||
for(int i = 0; i < 1; i++) {
|
||||
CaptchaUtil.createShearCaptcha(320, 240);
|
||||
CaptchaUtil.ofShearCaptcha(320, 240);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user