mirror of
https://gitee.com/dromara/hutool.git
synced 2025-04-05 17:37:59 +08:00
feature: add captcha font size
This commit is contained in:
parent
bf7738e6f9
commit
397bcd7341
@ -95,6 +95,25 @@ public abstract class AbstractCaptcha implements ICaptcha {
|
||||
this.font = new Font(Font.SANS_SERIF, Font.PLAIN, (int) (this.height * 0.75));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*
|
||||
* @param width 图片宽
|
||||
* @param height 图片高
|
||||
* @param generator 验证码生成器
|
||||
* @param interfereCount 验证码干扰元素个数
|
||||
* @param size 字体的大小 高度的倍数
|
||||
*/
|
||||
public AbstractCaptcha(int width, int height, CodeGenerator generator, int interfereCount, float size) {
|
||||
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 * size));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void createCode() {
|
||||
generateCode();
|
||||
@ -201,7 +220,7 @@ public abstract class AbstractCaptcha implements ICaptcha {
|
||||
* @return 图片带文件格式的 Base64
|
||||
* @since 5.3.11
|
||||
*/
|
||||
public String getImageBase64Data(){
|
||||
public String getImageBase64Data() {
|
||||
return URLUtil.getDataUriBase64("image/png", getImageBase64());
|
||||
}
|
||||
|
||||
|
@ -47,6 +47,21 @@ public class CaptchaUtil {
|
||||
return new LineCaptcha(width, height, generator, lineCount);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建线干扰的验证码
|
||||
*
|
||||
* @param width 图片宽
|
||||
* @param height 图片高
|
||||
* @param codeCount 字符个数
|
||||
* @param lineCount 干扰线条数
|
||||
* @param size 字体的大小 高度的倍数
|
||||
* @return {@link LineCaptcha}
|
||||
*/
|
||||
public static LineCaptcha createLineCaptcha(int width, int height, int codeCount, int lineCount, float size) {
|
||||
return new LineCaptcha(width, height, codeCount, lineCount, size);
|
||||
}
|
||||
// ------------------------- lineCaptcha end -------------------------
|
||||
|
||||
/**
|
||||
* 创建圆圈干扰的验证码,默认5位验证码,15个干扰圈
|
||||
*
|
||||
@ -86,6 +101,21 @@ public class CaptchaUtil {
|
||||
return new CircleCaptcha(width, height, generator, circleCount);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建圆圈干扰的验证码
|
||||
*
|
||||
* @param width 图片宽
|
||||
* @param height 图片高
|
||||
* @param codeCount 字符个数
|
||||
* @param circleCount 干扰圆圈条数
|
||||
* @param size 字体的大小 高度的倍数
|
||||
* @return {@link CircleCaptcha}
|
||||
*/
|
||||
public static CircleCaptcha createCircleCaptcha(int width, int height, int codeCount, int circleCount, float size) {
|
||||
return new CircleCaptcha(width, height, codeCount, circleCount, size);
|
||||
}
|
||||
// ------------------------- circleCaptcha end -------------------------
|
||||
|
||||
/**
|
||||
* 创建扭曲干扰的验证码,默认5位验证码
|
||||
*
|
||||
@ -125,10 +155,25 @@ public class CaptchaUtil {
|
||||
return new ShearCaptcha(width, height, generator, thickness);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建扭曲干扰的验证码,默认5位验证码
|
||||
*
|
||||
* @param width 图片宽
|
||||
* @param height 图片高
|
||||
* @param codeCount 字符个数
|
||||
* @param thickness 干扰线宽度
|
||||
* @param size 字体的大小 高度的倍数
|
||||
* @return {@link ShearCaptcha}
|
||||
*/
|
||||
public static ShearCaptcha createShearCaptcha(int width, int height, int codeCount, int thickness, float size) {
|
||||
return new ShearCaptcha(width, height, codeCount, thickness, size);
|
||||
}
|
||||
// ------------------------- shearCaptcha end -------------------------
|
||||
|
||||
/**
|
||||
* 创建GIF验证码
|
||||
*
|
||||
* @param width 宽
|
||||
* @param width 宽
|
||||
* @param height 高
|
||||
* @return {@link GifCaptcha}
|
||||
*/
|
||||
@ -139,8 +184,8 @@ public class CaptchaUtil {
|
||||
/**
|
||||
* 创建GIF验证码
|
||||
*
|
||||
* @param width 宽
|
||||
* @param height 高
|
||||
* @param width 宽
|
||||
* @param height 高
|
||||
* @param codeCount 字符个数
|
||||
* @return {@link GifCaptcha}
|
||||
*/
|
||||
@ -151,8 +196,8 @@ public class CaptchaUtil {
|
||||
/**
|
||||
* 创建GIF验证码
|
||||
*
|
||||
* @param width 宽
|
||||
* @param height 高
|
||||
* @param width 宽
|
||||
* @param height 高
|
||||
* @param generator 验证码生成器
|
||||
* @param thickness 验证码干扰元素个数
|
||||
* @return {@link GifCaptcha}
|
||||
@ -160,4 +205,20 @@ public class CaptchaUtil {
|
||||
public static GifCaptcha createGifCaptcha(int width, int height, CodeGenerator generator, int thickness) {
|
||||
return new GifCaptcha(width, height, generator, thickness);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建圆圈干扰的验证码
|
||||
*
|
||||
* @param width 图片宽
|
||||
* @param height 图片高
|
||||
* @param codeCount 字符个数
|
||||
* @param thickness 验证码干扰元素个数
|
||||
* @param size 字体的大小 高度的倍数
|
||||
* @return {@link GifCaptcha}
|
||||
*/
|
||||
public static GifCaptcha createGifCaptcha(int width, int height, int codeCount, int thickness, float size) {
|
||||
return new GifCaptcha(width, height, codeCount, thickness, size);
|
||||
}
|
||||
// ------------------------- gifCaptcha end -------------------------
|
||||
|
||||
}
|
||||
|
@ -24,7 +24,7 @@ public class CircleCaptcha extends AbstractCaptcha {
|
||||
/**
|
||||
* 构造
|
||||
*
|
||||
* @param width 图片宽
|
||||
* @param width 图片宽
|
||||
* @param height 图片高
|
||||
*/
|
||||
public CircleCaptcha(int width, int height) {
|
||||
@ -34,8 +34,8 @@ public class CircleCaptcha extends AbstractCaptcha {
|
||||
/**
|
||||
* 构造
|
||||
*
|
||||
* @param width 图片宽
|
||||
* @param height 图片高
|
||||
* @param width 图片宽
|
||||
* @param height 图片高
|
||||
* @param codeCount 字符个数
|
||||
*/
|
||||
public CircleCaptcha(int width, int height, int codeCount) {
|
||||
@ -45,9 +45,9 @@ public class CircleCaptcha extends AbstractCaptcha {
|
||||
/**
|
||||
* 构造
|
||||
*
|
||||
* @param width 图片宽
|
||||
* @param height 图片高
|
||||
* @param codeCount 字符个数
|
||||
* @param width 图片宽
|
||||
* @param height 图片高
|
||||
* @param codeCount 字符个数
|
||||
* @param interfereCount 验证码干扰元素个数
|
||||
*/
|
||||
public CircleCaptcha(int width, int height, int codeCount, int interfereCount) {
|
||||
@ -66,12 +66,26 @@ public class CircleCaptcha extends AbstractCaptcha {
|
||||
super(width, height, generator, interfereCount);
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*
|
||||
* @param width 图片宽
|
||||
* @param height 图片高
|
||||
* @param codeCount 字符个数
|
||||
* @param interfereCount 验证码干扰元素个数
|
||||
* @param size 字体的大小 高度的倍数
|
||||
*/
|
||||
public CircleCaptcha(int width, int height, int codeCount, int interfereCount, float size) {
|
||||
super(width, height, new RandomGenerator(codeCount), interfereCount, size);
|
||||
}
|
||||
|
||||
|
||||
@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));
|
||||
|
||||
try{
|
||||
try {
|
||||
// 随机画干扰圈圈
|
||||
drawInterfere(g);
|
||||
|
||||
@ -88,7 +102,7 @@ public class CircleCaptcha extends AbstractCaptcha {
|
||||
/**
|
||||
* 绘制字符串
|
||||
*
|
||||
* @param g {@link Graphics2D}画笔
|
||||
* @param g {@link Graphics2D}画笔
|
||||
* @param code 验证码
|
||||
*/
|
||||
private void drawString(Graphics2D g, String code) {
|
||||
|
@ -53,9 +53,9 @@ public class GifCaptcha extends AbstractCaptcha {
|
||||
}
|
||||
|
||||
/**
|
||||
* @param width 验证码宽度
|
||||
* @param height 验证码高度
|
||||
* @param codeCount 验证码个数
|
||||
* @param width 验证码宽度
|
||||
* @param height 验证码高度
|
||||
* @param codeCount 验证码个数
|
||||
* @param interfereCount 验证码干扰元素个数
|
||||
*/
|
||||
public GifCaptcha(int width, int height, int codeCount, int interfereCount) {
|
||||
@ -74,6 +74,19 @@ public class GifCaptcha extends AbstractCaptcha {
|
||||
super(width, height, generator, interfereCount);
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*
|
||||
* @param width 图片宽
|
||||
* @param height 图片高
|
||||
* @param codeCount 验证码个数
|
||||
* @param interfereCount 验证码干扰元素个数
|
||||
* @param size 字体的大小 高度的倍数
|
||||
*/
|
||||
public GifCaptcha(int width, int height, int codeCount, int interfereCount, float size) {
|
||||
super(width, height, new RandomGenerator(codeCount), interfereCount, size);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置图像的颜色量化(转换质量 由GIF规范允许的最大256种颜色)。
|
||||
* 低的值(最小值= 1)产生更好的颜色,但处理显著缓慢。
|
||||
@ -171,7 +184,7 @@ public class GifCaptcha extends AbstractCaptcha {
|
||||
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
|
||||
//或得图形上下文
|
||||
Graphics2D g2d = image.createGraphics();
|
||||
try{
|
||||
try {
|
||||
//利用指定颜色填充背景
|
||||
g2d.setColor(ObjectUtil.defaultIfNull(this.background, Color.WHITE));
|
||||
g2d.fillRect(0, 0, width, height);
|
||||
@ -191,9 +204,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);
|
||||
}
|
||||
|
@ -24,7 +24,7 @@ public class LineCaptcha extends AbstractCaptcha {
|
||||
/**
|
||||
* 构造,默认5位验证码,150条干扰线
|
||||
*
|
||||
* @param width 图片宽
|
||||
* @param width 图片宽
|
||||
* @param height 图片高
|
||||
*/
|
||||
public LineCaptcha(int width, int height) {
|
||||
@ -34,8 +34,8 @@ public class LineCaptcha extends AbstractCaptcha {
|
||||
/**
|
||||
* 构造
|
||||
*
|
||||
* @param width 图片宽
|
||||
* @param height 图片高
|
||||
* @param width 图片宽
|
||||
* @param height 图片高
|
||||
* @param codeCount 字符个数
|
||||
* @param lineCount 干扰线条数
|
||||
*/
|
||||
@ -54,6 +54,21 @@ public class LineCaptcha extends AbstractCaptcha {
|
||||
public LineCaptcha(int width, int height, CodeGenerator generator, int interfereCount) {
|
||||
super(width, height, generator, interfereCount);
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*
|
||||
* @param width 图片宽
|
||||
* @param height 图片高
|
||||
* @param codeCount 字符个数
|
||||
* @param interfereCount 验证码干扰元素个数
|
||||
* @param size 字体的大小 高度的倍数
|
||||
*/
|
||||
public LineCaptcha(int width, int height, int codeCount, int interfereCount, float size) {
|
||||
super(width, height, new RandomGenerator(codeCount), interfereCount, size);
|
||||
}
|
||||
|
||||
|
||||
// -------------------------------------------------------------------- Constructor end
|
||||
|
||||
@Override
|
||||
@ -79,7 +94,7 @@ public class LineCaptcha extends AbstractCaptcha {
|
||||
/**
|
||||
* 绘制字符串
|
||||
*
|
||||
* @param g {@link Graphics}画笔
|
||||
* @param g {@link Graphics}画笔
|
||||
* @param code 验证码
|
||||
*/
|
||||
private void drawString(Graphics2D g, String code) {
|
||||
|
@ -68,6 +68,20 @@ public class ShearCaptcha extends AbstractCaptcha {
|
||||
super(width, height, generator, interfereCount);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*
|
||||
* @param width 图片宽
|
||||
* @param height 图片高
|
||||
* @param codeCount 字符个数
|
||||
* @param interfereCount 验证码干扰元素个数
|
||||
* @param size 字体的大小 高度的倍数
|
||||
*/
|
||||
public ShearCaptcha(int width, int height, int codeCount, int interfereCount, float size) {
|
||||
super(width, height, new RandomGenerator(codeCount), interfereCount, size);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Image createImage(String code) {
|
||||
final BufferedImage image = new BufferedImage(this.width, this.height, BufferedImage.TYPE_INT_RGB);
|
||||
|
@ -32,6 +32,15 @@ public class CaptchaTest {
|
||||
lineCaptcha.write("f:/test/captcha/tellow.png");
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void lineCaptchaTestWithSize() {
|
||||
// 定义图形验证码的长和宽
|
||||
LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(200, 70, 4, 15, 0.65f);
|
||||
lineCaptcha.setBackground(Color.yellow);
|
||||
lineCaptcha.write("f:/test/captcha/tellow.png");
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void lineCaptchaWithMathTest() {
|
||||
@ -75,12 +84,22 @@ public class CaptchaTest {
|
||||
captcha.verify("1234");
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void circleCaptchaTestWithSize() {
|
||||
// 定义图形验证码的长和宽
|
||||
CircleCaptcha captcha = CaptchaUtil.createCircleCaptcha(200, 70, 4, 15, 0.65f);
|
||||
captcha.setBackground(Color.yellow);
|
||||
captcha.write("f:/test/captcha/circle.png");
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void shearCaptchaTest() {
|
||||
|
||||
// 定义图形验证码的长和宽
|
||||
ShearCaptcha captcha = CaptchaUtil.createShearCaptcha(203, 100, 4, 4);
|
||||
ShearCaptcha captcha = CaptchaUtil.createShearCaptcha(200, 100, 4, 4);
|
||||
// ShearCaptcha captcha = new ShearCaptcha(200, 100, 4, 4);
|
||||
// 图形验证码写出,可以写出到文件,也可以写出到流
|
||||
captcha.write("f:/captcha/shear.png");
|
||||
@ -113,6 +132,16 @@ public class CaptchaTest {
|
||||
captcha.verify("1234");
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void ShearCaptchaTestWithSize() {
|
||||
// 定义图形验证码的长和宽
|
||||
ShearCaptcha captcha = CaptchaUtil.createShearCaptcha(200, 70, 4, 15, 0.65f);
|
||||
captcha.setBackground(Color.yellow);
|
||||
captcha.write("f:/test/captcha/shear.png");
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void GifCaptchaTest() {
|
||||
@ -123,7 +152,16 @@ public class CaptchaTest {
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void bgTest(){
|
||||
public void GifCaptchaTestWithSize() {
|
||||
// 定义图形验证码的长和宽
|
||||
GifCaptcha captcha = CaptchaUtil.createGifCaptcha(200, 70, 4, 15, 0.65f);
|
||||
captcha.setBackground(Color.yellow);
|
||||
captcha.write("f:/test/captcha/gif.png");
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void bgTest() {
|
||||
LineCaptcha captcha = CaptchaUtil.createLineCaptcha(200, 100, 4, 1);
|
||||
captcha.setBackground(Color.WHITE);
|
||||
captcha.write("d:/test/test.jpg");
|
||||
|
Loading…
Reference in New Issue
Block a user