Captcha.setBackground为null时背景透明

This commit is contained in:
Looly 2024-04-24 14:46:09 +08:00
parent c0807bfa49
commit 73e8c67d0b
7 changed files with 13 additions and 13 deletions

View File

@ -12,6 +12,7 @@
* 【json 】 JSONConfig增加setWriteLongAsString可选是否将Long写出为String类型issue#3541@Github * 【json 】 JSONConfig增加setWriteLongAsString可选是否将Long写出为String类型issue#3541@Github
* 【cache 】 CacheUtil.newTimedCache增加有schedulePruneDelay参数的重载方法issue#I9HO25@Gitee * 【cache 】 CacheUtil.newTimedCache增加有schedulePruneDelay参数的重载方法issue#I9HO25@Gitee
* 【core 】 NumberChineseFormatter提供阿拉伯转中文支持多位小数的方法pr#3552@Github * 【core 】 NumberChineseFormatter提供阿拉伯转中文支持多位小数的方法pr#3552@Github
* 【captcha】 Captcha.setBackground为null时背景透明issue#3558@Github
### 🐞Bug修复 ### 🐞Bug修复
* 【http 】 修复HttpUtil.urlWithFormUrlEncoded方法重复编码问题issue#3536@Github * 【http 】 修复HttpUtil.urlWithFormUrlEncoded方法重复编码问题issue#3536@Github

View File

@ -60,7 +60,7 @@ public abstract class AbstractCaptcha implements ICaptcha {
/** /**
* 背景色 * 背景色
*/ */
protected Color background; protected Color background = Color.WHITE;
/** /**
* 文字透明度 * 文字透明度
*/ */

View File

@ -82,8 +82,8 @@ public class CircleCaptcha extends AbstractCaptcha {
@Override @Override
public Image createImage(String code) { public Image createImage(String code) {
final BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); final BufferedImage image = new BufferedImage(width, height, (null == this.background) ? BufferedImage.TYPE_4BYTE_ABGR : BufferedImage.TYPE_INT_RGB);
final Graphics2D g = ImgUtil.createGraphics(image, ObjectUtil.defaultIfNull(this.background, Color.WHITE)); final Graphics2D g = ImgUtil.createGraphics(image, this.background);
try { try {
// 随机画干扰圈圈 // 随机画干扰圈圈

View File

@ -3,6 +3,7 @@ package cn.hutool.captcha;
import cn.hutool.captcha.generator.CodeGenerator; import cn.hutool.captcha.generator.CodeGenerator;
import cn.hutool.captcha.generator.RandomGenerator; import cn.hutool.captcha.generator.RandomGenerator;
import cn.hutool.core.img.ImgUtil;
import cn.hutool.core.img.gif.AnimatedGifEncoder; import cn.hutool.core.img.gif.AnimatedGifEncoder;
import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.RandomUtil; import cn.hutool.core.util.RandomUtil;
@ -181,13 +182,11 @@ public class GifCaptcha extends AbstractCaptcha {
* @return BufferedImage * @return BufferedImage
*/ */
private BufferedImage graphicsImage(char[] chars, Color[] fontColor, char[] words, int flag) { private BufferedImage graphicsImage(char[] chars, Color[] fontColor, char[] words, int flag) {
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); final BufferedImage image = new BufferedImage(width, height, (null == this.background) ? BufferedImage.TYPE_4BYTE_ABGR : BufferedImage.TYPE_INT_RGB);
//或得图形上下文 //或得图形上下文
Graphics2D g2d = image.createGraphics(); final Graphics2D g2d = ImgUtil.createGraphics(image, this.background);
try { try {
//利用指定颜色填充背景 //利用指定颜色填充背景
g2d.setColor(ObjectUtil.defaultIfNull(this.background, Color.WHITE));
g2d.fillRect(0, 0, width, height);
AlphaComposite ac; AlphaComposite ac;
// 字符的y坐标 // 字符的y坐标
float y = (height >> 1) + (font.getSize() >> 1); float y = (height >> 1) + (font.getSize() >> 1);

View File

@ -74,8 +74,8 @@ public class LineCaptcha extends AbstractCaptcha {
@Override @Override
public Image createImage(String code) { public Image createImage(String code) {
// 图像buffer // 图像buffer
final BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); final BufferedImage image = new BufferedImage(width, height, (null == this.background) ? BufferedImage.TYPE_4BYTE_ABGR : BufferedImage.TYPE_INT_RGB);
final Graphics2D g = GraphicsUtil.createGraphics(image, ObjectUtil.defaultIfNull(this.background, Color.WHITE)); final Graphics2D g = ImgUtil.createGraphics(image, this.background);
try { try {
// 干扰线 // 干扰线

View File

@ -84,8 +84,8 @@ public class ShearCaptcha extends AbstractCaptcha {
@Override @Override
public Image createImage(String code) { public Image createImage(String code) {
final BufferedImage image = new BufferedImage(this.width, this.height, BufferedImage.TYPE_INT_RGB); final BufferedImage image = new BufferedImage(width, height, (null == this.background) ? BufferedImage.TYPE_4BYTE_ABGR : BufferedImage.TYPE_INT_RGB);
final Graphics2D g = GraphicsUtil.createGraphics(image, ObjectUtil.defaultIfNull(this.background, Color.WHITE)); final Graphics2D g = ImgUtil.createGraphics(image, this.background);
try{ try{
// 画字符串 // 画字符串

View File

@ -28,8 +28,8 @@ public class CaptchaTest {
public void lineCaptchaTest3() { public void lineCaptchaTest3() {
// 定义图形验证码的长和宽 // 定义图形验证码的长和宽
LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(200, 70, 4, 15); LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(200, 70, 4, 15);
lineCaptcha.setBackground(Color.yellow); lineCaptcha.setBackground(null);
lineCaptcha.write("f:/test/captcha/tellow.png"); lineCaptcha.write("d:/test/captcha/tellow.png");
} }
@Test @Test