mirror of
https://gitee.com/dromara/hutool.git
synced 2025-06-28 13:34:09 +08:00
修复Graphics2D的资源没释放问题
This commit is contained in:
parent
3a70158d96
commit
03a92830a6
@ -2,7 +2,7 @@
|
|||||||
# 🚀Changelog
|
# 🚀Changelog
|
||||||
|
|
||||||
-------------------------------------------------------------------------------------------------------------
|
-------------------------------------------------------------------------------------------------------------
|
||||||
# 5.8.27(2024-03-12)
|
# 5.8.27(2024-03-15)
|
||||||
|
|
||||||
### 🐣新特性
|
### 🐣新特性
|
||||||
* 【extra 】 FreemarkerEngine修改默认版本参数
|
* 【extra 】 FreemarkerEngine修改默认版本参数
|
||||||
@ -16,6 +16,7 @@
|
|||||||
* 【json 】 修复JSONUtil序列化和反序列化预期的结果不一致问题(pr#3507@Github)
|
* 【json 】 修复JSONUtil序列化和反序列化预期的结果不一致问题(pr#3507@Github)
|
||||||
* 【http 】 修复CVE-2022-22885,HttpGlobalConfig可选关闭信任host(issue#2042@Github)
|
* 【http 】 修复CVE-2022-22885,HttpGlobalConfig可选关闭信任host(issue#2042@Github)
|
||||||
* 【core 】 修复DateUtil.betweenYear闰年2月问题(issue#I97U3J@Gitee)
|
* 【core 】 修复DateUtil.betweenYear闰年2月问题(issue#I97U3J@Gitee)
|
||||||
|
* 【captcha】 修复Graphics2D的资源没释放问题(issue#I98PYN@Gitee)
|
||||||
|
|
||||||
-------------------------------------------------------------------------------------------------------------
|
-------------------------------------------------------------------------------------------------------------
|
||||||
# 5.8.26(2024-02-10)
|
# 5.8.26(2024-02-10)
|
||||||
|
@ -71,11 +71,15 @@ public class CircleCaptcha extends AbstractCaptcha {
|
|||||||
final BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
|
final BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
|
||||||
final Graphics2D g = ImgUtil.createGraphics(image, ObjectUtil.defaultIfNull(this.background, Color.WHITE));
|
final Graphics2D g = ImgUtil.createGraphics(image, ObjectUtil.defaultIfNull(this.background, Color.WHITE));
|
||||||
|
|
||||||
// 随机画干扰圈圈
|
try{
|
||||||
drawInterfere(g);
|
// 随机画干扰圈圈
|
||||||
|
drawInterfere(g);
|
||||||
|
|
||||||
// 画字符串
|
// 画字符串
|
||||||
drawString(g, code);
|
drawString(g, code);
|
||||||
|
} finally {
|
||||||
|
g.dispose();
|
||||||
|
}
|
||||||
|
|
||||||
return image;
|
return image;
|
||||||
}
|
}
|
||||||
|
@ -171,32 +171,35 @@ public class GifCaptcha extends AbstractCaptcha {
|
|||||||
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
|
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
|
||||||
//或得图形上下文
|
//或得图形上下文
|
||||||
Graphics2D g2d = image.createGraphics();
|
Graphics2D g2d = image.createGraphics();
|
||||||
//利用指定颜色填充背景
|
try{
|
||||||
g2d.setColor(ObjectUtil.defaultIfNull(this.background, Color.WHITE));
|
//利用指定颜色填充背景
|
||||||
g2d.fillRect(0, 0, width, height);
|
g2d.setColor(ObjectUtil.defaultIfNull(this.background, Color.WHITE));
|
||||||
AlphaComposite ac;
|
g2d.fillRect(0, 0, width, height);
|
||||||
// 字符的y坐标
|
AlphaComposite ac;
|
||||||
float y = (height >> 1) + (font.getSize() >> 1);
|
// 字符的y坐标
|
||||||
float m = 1.0f * (width - (chars.length * font.getSize())) / chars.length;
|
float y = (height >> 1) + (font.getSize() >> 1);
|
||||||
//字符的x坐标
|
float m = 1.0f * (width - (chars.length * font.getSize())) / chars.length;
|
||||||
float x = Math.max(m / 2.0f, 2);
|
//字符的x坐标
|
||||||
g2d.setFont(font);
|
float x = Math.max(m / 2.0f, 2);
|
||||||
// 指定透明度
|
g2d.setFont(font);
|
||||||
if (null != this.textAlpha) {
|
// 指定透明度
|
||||||
g2d.setComposite(this.textAlpha);
|
if (null != this.textAlpha) {
|
||||||
}
|
g2d.setComposite(this.textAlpha);
|
||||||
for (int i = 0; i < chars.length; i++) {
|
}
|
||||||
ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, getAlpha(chars.length, flag, i));
|
for (int i = 0; i < chars.length; i++) {
|
||||||
g2d.setComposite(ac);
|
ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, getAlpha(chars.length, flag, i));
|
||||||
g2d.setColor(fontColor[i]);
|
g2d.setComposite(ac);
|
||||||
g2d.drawOval(
|
g2d.setColor(fontColor[i]);
|
||||||
|
g2d.drawOval(
|
||||||
RandomUtil.randomInt(width),
|
RandomUtil.randomInt(width),
|
||||||
RandomUtil.randomInt(height),
|
RandomUtil.randomInt(height),
|
||||||
RandomUtil.randomInt(5, 30), 5 + RandomUtil.randomInt(5, 30)
|
RandomUtil.randomInt(5, 30), 5 + RandomUtil.randomInt(5, 30)
|
||||||
);//绘制椭圆边框
|
);//绘制椭圆边框
|
||||||
g2d.drawString(words[i] + "", x + (font.getSize() + m) * i, y);
|
g2d.drawString(words[i] + "", x + (font.getSize() + m) * i, y);
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
g2d.dispose();
|
||||||
}
|
}
|
||||||
g2d.dispose();
|
|
||||||
return image;
|
return image;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -62,11 +62,15 @@ public class LineCaptcha extends AbstractCaptcha {
|
|||||||
final BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
|
final BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
|
||||||
final Graphics2D g = GraphicsUtil.createGraphics(image, ObjectUtil.defaultIfNull(this.background, Color.WHITE));
|
final Graphics2D g = GraphicsUtil.createGraphics(image, ObjectUtil.defaultIfNull(this.background, Color.WHITE));
|
||||||
|
|
||||||
// 干扰线
|
try {
|
||||||
drawInterfere(g);
|
// 干扰线
|
||||||
|
drawInterfere(g);
|
||||||
|
|
||||||
// 字符串
|
// 字符串
|
||||||
drawString(g, code);
|
drawString(g, code);
|
||||||
|
} finally {
|
||||||
|
g.dispose();
|
||||||
|
}
|
||||||
|
|
||||||
return image;
|
return image;
|
||||||
}
|
}
|
||||||
|
@ -73,13 +73,17 @@ public class ShearCaptcha extends AbstractCaptcha {
|
|||||||
final BufferedImage image = new BufferedImage(this.width, this.height, BufferedImage.TYPE_INT_RGB);
|
final BufferedImage image = new BufferedImage(this.width, this.height, BufferedImage.TYPE_INT_RGB);
|
||||||
final Graphics2D g = GraphicsUtil.createGraphics(image, ObjectUtil.defaultIfNull(this.background, Color.WHITE));
|
final Graphics2D g = GraphicsUtil.createGraphics(image, ObjectUtil.defaultIfNull(this.background, Color.WHITE));
|
||||||
|
|
||||||
// 画字符串
|
try{
|
||||||
drawString(g, code);
|
// 画字符串
|
||||||
|
drawString(g, code);
|
||||||
|
|
||||||
// 扭曲
|
// 扭曲
|
||||||
shear(g, this.width, this.height, ObjectUtil.defaultIfNull(this.background, Color.WHITE));
|
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, ImgUtil.randomColor());
|
||||||
|
} finally {
|
||||||
|
g.dispose();
|
||||||
|
}
|
||||||
|
|
||||||
return image;
|
return image;
|
||||||
}
|
}
|
||||||
|
@ -1338,8 +1338,11 @@ public class ImgUtil {
|
|||||||
final BufferedImage bimage = new BufferedImage(
|
final BufferedImage bimage = new BufferedImage(
|
||||||
img.getWidth(null), img.getHeight(null), imageType);
|
img.getWidth(null), img.getHeight(null), imageType);
|
||||||
final Graphics2D bGr = GraphicsUtil.createGraphics(bimage, backgroundColor);
|
final Graphics2D bGr = GraphicsUtil.createGraphics(bimage, backgroundColor);
|
||||||
bGr.drawImage(img, 0, 0, null);
|
try{
|
||||||
bGr.dispose();
|
bGr.drawImage(img, 0, 0, null);
|
||||||
|
} finally {
|
||||||
|
bGr.dispose();
|
||||||
|
}
|
||||||
|
|
||||||
return bimage;
|
return bimage;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user