mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
修复Graphics2D的资源没释放问题
This commit is contained in:
parent
03891277e9
commit
9a3d6857db
@ -234,7 +234,7 @@ public class ULID implements Comparable<ULID>, Serializable {
|
|||||||
* @param timestamp 时间戳
|
* @param timestamp 时间戳
|
||||||
*/
|
*/
|
||||||
private static void checkTimestamp(final long timestamp) {
|
private static void checkTimestamp(final long timestamp) {
|
||||||
Assert.isTrue((timestamp & TIMESTAMP_MASK) == 0), "ULID does not support timestamps after +10889-08-02T05:31:50.655Z!")
|
Assert.isTrue((timestamp & TIMESTAMP_MASK) == 0,
|
||||||
;
|
"ULID does not support timestamps after +10889-08-02T05:31:50.655Z!");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -85,11 +85,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 = GraphicsUtil.createGraphics(image, ObjUtil.defaultIfNull(this.background, Color.WHITE));
|
final Graphics2D g = GraphicsUtil.createGraphics(image, ObjUtil.defaultIfNull(this.background, Color.WHITE));
|
||||||
|
|
||||||
// 随机画干扰圈圈
|
try{
|
||||||
drawInterfere(g);
|
// 随机画干扰圈圈
|
||||||
|
drawInterfere(g);
|
||||||
|
|
||||||
// 画字符串
|
// 画字符串
|
||||||
drawString(g, code);
|
drawString(g, code);
|
||||||
|
} finally {
|
||||||
|
g.dispose();
|
||||||
|
}
|
||||||
|
|
||||||
return image;
|
return image;
|
||||||
}
|
}
|
||||||
|
@ -13,9 +13,9 @@
|
|||||||
package org.dromara.hutool.swing.captcha;
|
package org.dromara.hutool.swing.captcha;
|
||||||
|
|
||||||
|
|
||||||
|
import com.madgag.gif.fmsware.AnimatedGifEncoder;
|
||||||
import org.dromara.hutool.core.util.ObjUtil;
|
import org.dromara.hutool.core.util.ObjUtil;
|
||||||
import org.dromara.hutool.core.util.RandomUtil;
|
import org.dromara.hutool.core.util.RandomUtil;
|
||||||
import com.madgag.gif.fmsware.AnimatedGifEncoder;
|
|
||||||
import org.dromara.hutool.swing.captcha.generator.CodeGenerator;
|
import org.dromara.hutool.swing.captcha.generator.CodeGenerator;
|
||||||
import org.dromara.hutool.swing.captcha.generator.RandomGenerator;
|
import org.dromara.hutool.swing.captcha.generator.RandomGenerator;
|
||||||
|
|
||||||
@ -183,32 +183,35 @@ public class GifCaptcha 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 g2d = image.createGraphics();
|
final Graphics2D g2d = image.createGraphics();
|
||||||
//利用指定颜色填充背景
|
try{
|
||||||
g2d.setColor(ObjUtil.defaultIfNull(this.background, Color.WHITE));
|
//利用指定颜色填充背景
|
||||||
g2d.fillRect(0, 0, width, height);
|
g2d.setColor(ObjUtil.defaultIfNull(this.background, Color.WHITE));
|
||||||
AlphaComposite ac;
|
g2d.fillRect(0, 0, width, height);
|
||||||
// 字符的y坐标
|
AlphaComposite ac;
|
||||||
final float y = (height >> 1) + (font.getSize() >> 1);
|
// 字符的y坐标
|
||||||
final float m = 1.0f * (width - (chars.length * font.getSize())) / chars.length;
|
final float y = (height >> 1) + (font.getSize() >> 1);
|
||||||
//字符的x坐标
|
final float m = 1.0f * (width - (chars.length * font.getSize())) / chars.length;
|
||||||
final float x = Math.max(m / 2.0f, 2);
|
//字符的x坐标
|
||||||
g2d.setFont(font);
|
final 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));
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
g2d.dispose();
|
||||||
}
|
}
|
||||||
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.dispose();
|
|
||||||
return image;
|
return image;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -77,11 +77,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, ObjUtil.defaultIfNull(this.background, Color.WHITE));
|
final Graphics2D g = GraphicsUtil.createGraphics(image, ObjUtil.defaultIfNull(this.background, Color.WHITE));
|
||||||
|
|
||||||
// 干扰线
|
try{
|
||||||
drawInterfere(g);
|
// 干扰线
|
||||||
|
drawInterfere(g);
|
||||||
|
|
||||||
// 字符串
|
// 字符串
|
||||||
drawString(g, code);
|
drawString(g, code);
|
||||||
|
} finally {
|
||||||
|
g.dispose();
|
||||||
|
}
|
||||||
|
|
||||||
return image;
|
return image;
|
||||||
}
|
}
|
||||||
|
@ -85,13 +85,16 @@ 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, ObjUtil.defaultIfNull(this.background, Color.WHITE));
|
final Graphics2D g = GraphicsUtil.createGraphics(image, ObjUtil.defaultIfNull(this.background, Color.WHITE));
|
||||||
|
|
||||||
// 画字符串
|
try{
|
||||||
drawString(g, code);
|
// 画字符串
|
||||||
|
drawString(g, code);
|
||||||
// 扭曲
|
// 扭曲
|
||||||
shear(g, this.width, this.height, ObjUtil.defaultIfNull(this.background, Color.WHITE));
|
shear(g, this.width, this.height, ObjUtil.defaultIfNull(this.background, Color.WHITE));
|
||||||
// 画干扰线
|
// 画干扰线
|
||||||
drawInterfere(g, 0, RandomUtil.randomInt(this.height) + 1, this.width, RandomUtil.randomInt(this.height) + 1, this.interfereCount, ColorUtil.randomColor());
|
drawInterfere(g, 0, RandomUtil.randomInt(this.height) + 1, this.width, RandomUtil.randomInt(this.height) + 1, this.interfereCount, ColorUtil.randomColor());
|
||||||
|
} finally {
|
||||||
|
g.dispose();
|
||||||
|
}
|
||||||
|
|
||||||
return image;
|
return image;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user