add constructor

This commit is contained in:
Looly 2024-01-05 09:06:31 +08:00
parent 4ccf795ee0
commit efc4ca63a2
7 changed files with 94 additions and 28 deletions

View File

@ -27,7 +27,7 @@ public class CaptchaUtil {
* @param height 图片高 * @param height 图片高
* @return {@link LineCaptcha} * @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); return new LineCaptcha(width, height);
} }
@ -40,7 +40,7 @@ public class CaptchaUtil {
* @param lineCount 干扰线条数 * @param lineCount 干扰线条数
* @return {@link LineCaptcha} * @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); return new LineCaptcha(width, height, codeCount, lineCount);
} }
@ -52,7 +52,7 @@ public class CaptchaUtil {
* @return {@link CircleCaptcha} * @return {@link CircleCaptcha}
* @since 3.2.3 * @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); return new CircleCaptcha(width, height);
} }
@ -66,7 +66,7 @@ public class CaptchaUtil {
* @return {@link CircleCaptcha} * @return {@link CircleCaptcha}
* @since 3.2.3 * @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); return new CircleCaptcha(width, height, codeCount, circleCount);
} }
@ -78,7 +78,7 @@ public class CaptchaUtil {
* @return {@link ShearCaptcha} * @return {@link ShearCaptcha}
* @since 3.2.3 * @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); return new ShearCaptcha(width, height);
} }
@ -92,7 +92,7 @@ public class CaptchaUtil {
* @return {@link ShearCaptcha} * @return {@link ShearCaptcha}
* @since 3.3.0 * @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); return new ShearCaptcha(width, height, codeCount, thickness);
} }
@ -103,7 +103,7 @@ public class CaptchaUtil {
* @param height * @param height
* @return {@link GifCaptcha} * @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); return new GifCaptcha(width, height);
} }
@ -115,7 +115,7 @@ public class CaptchaUtil {
* @param codeCount 字符个数 * @param codeCount 字符个数
* @return {@link GifCaptcha} * @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); return new GifCaptcha(width, height, codeCount);
} }
} }

View File

@ -14,6 +14,8 @@ package org.dromara.hutool.swing.captcha;
import org.dromara.hutool.core.util.ObjUtil; import org.dromara.hutool.core.util.ObjUtil;
import org.dromara.hutool.core.util.RandomUtil; 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.color.ColorUtil;
import org.dromara.hutool.swing.img.GraphicsUtil; import org.dromara.hutool.swing.img.GraphicsUtil;
@ -63,7 +65,19 @@ public class CircleCaptcha extends AbstractCaptcha {
* @param interfereCount 验证码干扰元素个数 * @param interfereCount 验证码干扰元素个数
*/ */
public CircleCaptcha(final int width, final int height, final int codeCount, final int 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 @Override

View File

@ -16,6 +16,8 @@ package org.dromara.hutool.swing.captcha;
import org.dromara.hutool.core.util.ObjUtil; import org.dromara.hutool.core.util.ObjUtil;
import org.dromara.hutool.core.util.RandomUtil; import org.dromara.hutool.core.util.RandomUtil;
import com.madgag.gif.fmsware.AnimatedGifEncoder; 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.AlphaComposite;
import java.awt.Color; import java.awt.Color;
@ -59,7 +61,29 @@ public class GifCaptcha extends AbstractCaptcha {
* @param codeCount 验证码个数 * @param codeCount 验证码个数
*/ */
public GifCaptcha(final int width, final int height, final int 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);
} }
/** /**

View File

@ -14,6 +14,8 @@ package org.dromara.hutool.swing.captcha;
import org.dromara.hutool.core.util.ObjUtil; import org.dromara.hutool.core.util.ObjUtil;
import org.dromara.hutool.core.util.RandomUtil; 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.color.ColorUtil;
import org.dromara.hutool.swing.img.GraphicsUtil; import org.dromara.hutool.swing.img.GraphicsUtil;
@ -53,7 +55,19 @@ public class LineCaptcha extends AbstractCaptcha {
* @param lineCount 干扰线条数 * @param lineCount 干扰线条数
*/ */
public LineCaptcha(final int width, final int height, final int codeCount, final int 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 // -------------------------------------------------------------------- Constructor end

View File

@ -14,6 +14,8 @@ package org.dromara.hutool.swing.captcha;
import org.dromara.hutool.core.util.ObjUtil; import org.dromara.hutool.core.util.ObjUtil;
import org.dromara.hutool.core.util.RandomUtil; 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.color.ColorUtil;
import org.dromara.hutool.swing.img.GraphicsUtil; import org.dromara.hutool.swing.img.GraphicsUtil;
@ -63,7 +65,19 @@ public class ShearCaptcha extends AbstractCaptcha {
* @param thickness 干扰线宽度 * @param thickness 干扰线宽度
*/ */
public ShearCaptcha(final int width, final int height, final int codeCount, final int 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 @Override

View File

@ -30,7 +30,7 @@ public class CaptchaTest {
@Test @Test
public void lineCaptchaTest1() { public void lineCaptchaTest1() {
// 定义图形验证码的长和宽 // 定义图形验证码的长和宽
final LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(200, 100); final LineCaptcha lineCaptcha = CaptchaUtil.ofLineCaptcha(200, 100);
Assertions.assertNotNull(lineCaptcha.getCode()); Assertions.assertNotNull(lineCaptcha.getCode());
Assertions.assertTrue(lineCaptcha.verify(lineCaptcha.getCode())); Assertions.assertTrue(lineCaptcha.verify(lineCaptcha.getCode()));
} }
@ -39,7 +39,7 @@ public class CaptchaTest {
@Disabled @Disabled
public void lineCaptchaTest3() { 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.setBackground(Color.yellow);
lineCaptcha.write("f:/test/captcha/tellow.png"); lineCaptcha.write("f:/test/captcha/tellow.png");
} }
@ -48,7 +48,7 @@ public class CaptchaTest {
@Disabled @Disabled
public void lineCaptchaWithMathTest() { public void lineCaptchaWithMathTest() {
// 定义图形验证码的长和宽 // 定义图形验证码的长和宽
final LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(200, 80); final LineCaptcha lineCaptcha = CaptchaUtil.ofLineCaptcha(200, 80);
lineCaptcha.setGenerator(new MathGenerator()); lineCaptcha.setGenerator(new MathGenerator());
lineCaptcha.setTextAlpha(0.8f); lineCaptcha.setTextAlpha(0.8f);
lineCaptcha.write("f:/captcha/math.png"); lineCaptcha.write("f:/captcha/math.png");
@ -59,7 +59,7 @@ public class CaptchaTest {
public void lineCaptchaTest2() { 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 lineCaptcha = new LineCaptcha(200, 100, 4, 150);
// 图形验证码写出可以写出到文件也可以写出到流 // 图形验证码写出可以写出到文件也可以写出到流
lineCaptcha.write("f:/captcha/line.png"); lineCaptcha.write("f:/captcha/line.png");
@ -79,7 +79,7 @@ public class CaptchaTest {
public void circleCaptchaTest() { 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); // CircleCaptcha captcha = new CircleCaptcha(200, 100, 4, 20);
// 图形验证码写出可以写出到文件也可以写出到流 // 图形验证码写出可以写出到文件也可以写出到流
captcha.write("f:/captcha/circle.png"); captcha.write("f:/captcha/circle.png");
@ -92,7 +92,7 @@ public class CaptchaTest {
public void shearCaptchaTest() { 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); // ShearCaptcha captcha = new ShearCaptcha(200, 100, 4, 4);
// 图形验证码写出可以写出到文件也可以写出到流 // 图形验证码写出可以写出到文件也可以写出到流
captcha.write("f:/captcha/shear.png"); captcha.write("f:/captcha/shear.png");
@ -116,7 +116,7 @@ public class CaptchaTest {
@Disabled @Disabled
public void ShearCaptchaWithMathTest() { 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()); captcha.setGenerator(new MathGenerator());
// ShearCaptcha captcha = new ShearCaptcha(200, 100, 4, 4); // ShearCaptcha captcha = new ShearCaptcha(200, 100, 4, 4);
// 图形验证码写出可以写出到文件也可以写出到流 // 图形验证码写出可以写出到文件也可以写出到流
@ -128,7 +128,7 @@ public class CaptchaTest {
@Test @Test
@Disabled @Disabled
public void GifCaptchaTest() { 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"); captcha.write("d:/test/gif_captcha.gif");
assert captcha.verify(captcha.getCode()); assert captcha.verify(captcha.getCode());
} }
@ -136,7 +136,7 @@ public class CaptchaTest {
@Test @Test
@Disabled @Disabled
public void bgTest(){ 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.setBackground(Color.WHITE);
captcha.write("d:/test/test.jpg"); captcha.write("d:/test/test.jpg");
} }

View File

@ -21,7 +21,7 @@ public class CaptchaUtilTest {
@Disabled @Disabled
public void createTest() { public void createTest() {
for(int i = 0; i < 1; i++) { for(int i = 0; i < 1; i++) {
CaptchaUtil.createShearCaptcha(320, 240); CaptchaUtil.ofShearCaptcha(320, 240);
} }
} }
} }