From 03a92830a6c61ce3eaa10cd93362586a6e0b6adf Mon Sep 17 00:00:00 2001 From: Looly Date: Fri, 15 Mar 2024 16:37:41 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8DGraphics2D=E7=9A=84=E8=B5=84?= =?UTF-8?q?=E6=BA=90=E6=B2=A1=E9=87=8A=E6=94=BE=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 3 +- .../java/cn/hutool/captcha/CircleCaptcha.java | 12 +++-- .../java/cn/hutool/captcha/GifCaptcha.java | 47 ++++++++++--------- .../java/cn/hutool/captcha/LineCaptcha.java | 12 +++-- .../java/cn/hutool/captcha/ShearCaptcha.java | 16 ++++--- .../main/java/cn/hutool/core/img/ImgUtil.java | 7 ++- 6 files changed, 58 insertions(+), 39 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 22cfad999..95d7f3255 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ # 🚀Changelog ------------------------------------------------------------------------------------------------------------- -# 5.8.27(2024-03-12) +# 5.8.27(2024-03-15) ### 🐣新特性 * 【extra 】 FreemarkerEngine修改默认版本参数 @@ -16,6 +16,7 @@ * 【json 】 修复JSONUtil序列化和反序列化预期的结果不一致问题(pr#3507@Github) * 【http 】 修复CVE-2022-22885,HttpGlobalConfig可选关闭信任host(issue#2042@Github) * 【core 】 修复DateUtil.betweenYear闰年2月问题(issue#I97U3J@Gitee) +* 【captcha】 修复Graphics2D的资源没释放问题(issue#I98PYN@Gitee) ------------------------------------------------------------------------------------------------------------- # 5.8.26(2024-02-10) diff --git a/hutool-captcha/src/main/java/cn/hutool/captcha/CircleCaptcha.java b/hutool-captcha/src/main/java/cn/hutool/captcha/CircleCaptcha.java index a3c191a40..795e05604 100755 --- a/hutool-captcha/src/main/java/cn/hutool/captcha/CircleCaptcha.java +++ b/hutool-captcha/src/main/java/cn/hutool/captcha/CircleCaptcha.java @@ -71,11 +71,15 @@ public class CircleCaptcha extends AbstractCaptcha { final BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); final Graphics2D g = ImgUtil.createGraphics(image, ObjectUtil.defaultIfNull(this.background, Color.WHITE)); - // 随机画干扰圈圈 - drawInterfere(g); + try{ + // 随机画干扰圈圈 + drawInterfere(g); - // 画字符串 - drawString(g, code); + // 画字符串 + drawString(g, code); + } finally { + g.dispose(); + } return image; } diff --git a/hutool-captcha/src/main/java/cn/hutool/captcha/GifCaptcha.java b/hutool-captcha/src/main/java/cn/hutool/captcha/GifCaptcha.java index c313e44b2..388ac5bc3 100755 --- a/hutool-captcha/src/main/java/cn/hutool/captcha/GifCaptcha.java +++ b/hutool-captcha/src/main/java/cn/hutool/captcha/GifCaptcha.java @@ -171,32 +171,35 @@ public class GifCaptcha extends AbstractCaptcha { BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); //或得图形上下文 Graphics2D g2d = image.createGraphics(); - //利用指定颜色填充背景 - g2d.setColor(ObjectUtil.defaultIfNull(this.background, Color.WHITE)); - g2d.fillRect(0, 0, width, height); - AlphaComposite ac; - // 字符的y坐标 - float y = (height >> 1) + (font.getSize() >> 1); - float m = 1.0f * (width - (chars.length * font.getSize())) / chars.length; - //字符的x坐标 - float x = Math.max(m / 2.0f, 2); - g2d.setFont(font); - // 指定透明度 - 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)); - g2d.setComposite(ac); - g2d.setColor(fontColor[i]); - g2d.drawOval( + try{ + //利用指定颜色填充背景 + g2d.setColor(ObjectUtil.defaultIfNull(this.background, Color.WHITE)); + g2d.fillRect(0, 0, width, height); + AlphaComposite ac; + // 字符的y坐标 + float y = (height >> 1) + (font.getSize() >> 1); + float m = 1.0f * (width - (chars.length * font.getSize())) / chars.length; + //字符的x坐标 + float x = Math.max(m / 2.0f, 2); + g2d.setFont(font); + // 指定透明度 + 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)); + g2d.setComposite(ac); + g2d.setColor(fontColor[i]); + g2d.drawOval( 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); + );//绘制椭圆边框 + g2d.drawString(words[i] + "", x + (font.getSize() + m) * i, y); + } + } finally { + g2d.dispose(); } - g2d.dispose(); return image; } diff --git a/hutool-captcha/src/main/java/cn/hutool/captcha/LineCaptcha.java b/hutool-captcha/src/main/java/cn/hutool/captcha/LineCaptcha.java index 3aa1b1e22..6302afab9 100755 --- a/hutool-captcha/src/main/java/cn/hutool/captcha/LineCaptcha.java +++ b/hutool-captcha/src/main/java/cn/hutool/captcha/LineCaptcha.java @@ -62,11 +62,15 @@ public class LineCaptcha extends AbstractCaptcha { final BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); final Graphics2D g = GraphicsUtil.createGraphics(image, ObjectUtil.defaultIfNull(this.background, Color.WHITE)); - // 干扰线 - drawInterfere(g); + try { + // 干扰线 + drawInterfere(g); - // 字符串 - drawString(g, code); + // 字符串 + drawString(g, code); + } finally { + g.dispose(); + } return image; } diff --git a/hutool-captcha/src/main/java/cn/hutool/captcha/ShearCaptcha.java b/hutool-captcha/src/main/java/cn/hutool/captcha/ShearCaptcha.java index be53ca109..fe9a51cb7 100755 --- a/hutool-captcha/src/main/java/cn/hutool/captcha/ShearCaptcha.java +++ b/hutool-captcha/src/main/java/cn/hutool/captcha/ShearCaptcha.java @@ -73,13 +73,17 @@ public class ShearCaptcha extends AbstractCaptcha { 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)); - // 画字符串 - drawString(g, code); + try{ + // 画字符串 + drawString(g, code); - // 扭曲 - 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()); + // 扭曲 + 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()); + } finally { + g.dispose(); + } return image; } diff --git a/hutool-core/src/main/java/cn/hutool/core/img/ImgUtil.java b/hutool-core/src/main/java/cn/hutool/core/img/ImgUtil.java index 6c9a5e0f8..0862bfb81 100755 --- a/hutool-core/src/main/java/cn/hutool/core/img/ImgUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/img/ImgUtil.java @@ -1338,8 +1338,11 @@ public class ImgUtil { final BufferedImage bimage = new BufferedImage( img.getWidth(null), img.getHeight(null), imageType); final Graphics2D bGr = GraphicsUtil.createGraphics(bimage, backgroundColor); - bGr.drawImage(img, 0, 0, null); - bGr.dispose(); + try{ + bGr.drawImage(img, 0, 0, null); + } finally { + bGr.dispose(); + } return bimage; }