验证码增加颜色配置(字体,干扰线)

This commit is contained in:
晓雷 2025-07-09 12:53:37 +08:00
parent b43899c6fb
commit 2085ce860d
5 changed files with 53 additions and 11 deletions

View File

@ -3,21 +3,22 @@ package cn.hutool.captcha;
import cn.hutool.captcha.generator.CodeGenerator;
import cn.hutool.captcha.generator.RandomGenerator;
import cn.hutool.core.codec.Base64;
import cn.hutool.core.img.GraphicsUtil;
import cn.hutool.core.img.ImgUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IORuntimeException;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.URLUtil;
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Font;
import java.awt.Image;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.concurrent.ThreadLocalRandom;
/**
* 抽象验证码<br>
@ -28,7 +29,7 @@ import java.io.OutputStream;
*/
public abstract class AbstractCaptcha implements ICaptcha {
private static final long serialVersionUID = 3180820918087507254L;
final ThreadLocalRandom random = RandomUtil.getRandom();
/**
* 图片的宽度
*/
@ -61,6 +62,16 @@ public abstract class AbstractCaptcha implements ICaptcha {
* 背景色
*/
protected Color background = Color.WHITE;
/**
* 字体颜色
*/
protected Color color;
/**
* 干扰线颜色
*/
protected Color interfereColor;
/**
* 文字透明度
*/
@ -270,5 +281,21 @@ public abstract class AbstractCaptcha implements ICaptcha {
public void setTextAlpha(float textAlpha) {
this.textAlpha = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, textAlpha);
}
@Override
public Color getInterfereColor() {
return ObjUtil.defaultIfNull(this.interfereColor,ImgUtil.randomColor(this.random));
}
public void setInterfereColor(Color interfereColor) {
this.interfereColor = interfereColor;
}
@Override
public Color getColor() {
return ObjUtil.defaultIfNull(this.color,ImgUtil.randomColor(this.random));
}
public void setColor(Color color) {
this.color = color;
}
}

View File

@ -109,7 +109,7 @@ public class CircleCaptcha extends AbstractCaptcha {
if (null != this.textAlpha) {
g.setComposite(this.textAlpha);
}
GraphicsUtil.drawStringColourful(g, code, this.font, this.width, this.height);
GraphicsUtil.drawString(g, code, this.font, this.getColor(), this.width, this.height, null, 0);
}
/**
@ -121,7 +121,7 @@ public class CircleCaptcha extends AbstractCaptcha {
final ThreadLocalRandom random = RandomUtil.getRandom();
for (int i = 0; i < this.interfereCount; i++) {
g.setColor(ImgUtil.randomColor(random));
g.setColor(this.getColor());
g.drawOval(random.nextInt(width), random.nextInt(height), random.nextInt(height >> 1), random.nextInt(height >> 1));
}
}

View File

@ -1,5 +1,6 @@
package cn.hutool.captcha;
import java.awt.*;
import java.io.OutputStream;
import java.io.Serializable;
@ -37,4 +38,18 @@ public interface ICaptcha extends Serializable{
* @param out 目标流
*/
void write(OutputStream out);
/**
* 获取验证码的背景颜色不配置为随机
*
* @return 颜色
*/
Color getColor();
/**
* 获取干扰线的颜色不配置为随机
*
* @return 颜色
*/
Color getInterfereColor();
}

View File

@ -101,7 +101,7 @@ public class LineCaptcha extends AbstractCaptcha {
if (null != this.textAlpha) {
g.setComposite(this.textAlpha);
}
GraphicsUtil.drawStringColourful(g, code, this.font, this.width, this.height);
GraphicsUtil.drawString(g, code, this.font, this.getColor(), this.width, this.height, null, 0);
}
/**
@ -117,7 +117,7 @@ public class LineCaptcha extends AbstractCaptcha {
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.setColor(this.getInterfereColor());
g.drawLine(xs, ys, xe, ye);
}
}

View File

@ -94,7 +94,7 @@ public class ShearCaptcha extends AbstractCaptcha {
// 扭曲
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());
drawInterfere(g, 0, RandomUtil.randomInt(this.height) + 1, this.width, RandomUtil.randomInt(this.height) + 1, this.interfereCount, this.getColor());
} finally {
g.dispose();
}
@ -114,7 +114,7 @@ public class ShearCaptcha extends AbstractCaptcha {
if (null != this.textAlpha) {
g.setComposite(this.textAlpha);
}
GraphicsUtil.drawStringColourful(g, code, this.font, this.width, this.height);
GraphicsUtil.drawString(g, code, this.font, this.getColor(), this.width, this.height, null, 0);
}
/**