From 0f2fc9f3ae314c8c50b4a63c83bedce0f38de106 Mon Sep 17 00:00:00 2001 From: hiqiuyi <178673693@qq.com> Date: Mon, 25 Jul 2022 12:01:26 +0800 Subject: [PATCH] =?UTF-8?q?=E6=A0=B9=E6=8D=AE=E6=96=87=E5=AD=97=E5=88=9B?= =?UTF-8?q?=E5=BB=BA=E9=80=8F=E6=98=8E=E8=83=8C=E6=99=AF=E7=9A=84PNG?= =?UTF-8?q?=E5=9B=BE=E7=89=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/cn/hutool/core/img/ImgUtil.java | 36 +++++++++++-------- .../java/cn/hutool/core/img/ImgUtilTest.java | 22 ++++++++++++ 2 files changed, 44 insertions(+), 14 deletions(-) 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 a13290c26..c77e277c5 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 @@ -23,18 +23,7 @@ import javax.imageio.ImageWriter; import javax.imageio.stream.ImageInputStream; import javax.imageio.stream.ImageOutputStream; import javax.swing.ImageIcon; -import java.awt.Color; -import java.awt.Font; -import java.awt.Graphics; -import java.awt.Graphics2D; -import java.awt.GraphicsConfiguration; -import java.awt.GraphicsDevice; -import java.awt.GraphicsEnvironment; -import java.awt.HeadlessException; -import java.awt.Image; -import java.awt.Point; -import java.awt.Rectangle; -import java.awt.Toolkit; +import java.awt.*; import java.awt.color.ColorSpace; import java.awt.font.FontRenderContext; import java.awt.geom.AffineTransform; @@ -1370,6 +1359,19 @@ public class ImgUtil { writePng(createImage(str, font, backgroundColor, fontColor, BufferedImage.TYPE_INT_ARGB), out); } + /** + * 根据文字创建透明背景的PNG图片 + * + * @param str 文字 + * @param font 字体{@link Font} + * @param fontColor 字体颜色,默认黑色 + * @param out 图片输出地 + * @throws IORuntimeException IO异常 + */ + public static void createTransparentImage(String str, Font font, Color fontColor, ImageOutputStream out) throws IORuntimeException { + writePng(createImage(str, font, null, fontColor, BufferedImage.TYPE_INT_ARGB), out); + } + /** * 根据文字创建图片 * @@ -1392,13 +1394,19 @@ public class ImgUtil { int height = unitHeight + 3; // 创建图片 - final BufferedImage image = new BufferedImage(width, height, imageType); - final Graphics g = image.getGraphics(); + BufferedImage image = new BufferedImage(width, height, imageType); + Graphics g = image.getGraphics(); if (null != backgroundColor) { // 先用背景色填充整张图片,也就是背景 g.setColor(backgroundColor); g.fillRect(0, 0, width, height); + }else{ + // 如果没有设置背景色,则设置为透明背景 + g.dispose(); + image = image.createGraphics().getDeviceConfiguration().createCompatibleImage(width, height, Transparency.TRANSLUCENT); + g = image.getGraphics(); } + g.setColor(ObjectUtil.defaultIfNull(fontColor, Color.BLACK)); g.setFont(font);// 设置画笔字体 g.drawString(str, 0, font.getSize());// 画出字符串 diff --git a/hutool-core/src/test/java/cn/hutool/core/img/ImgUtilTest.java b/hutool-core/src/test/java/cn/hutool/core/img/ImgUtilTest.java index a91e3de6a..68794256a 100755 --- a/hutool-core/src/test/java/cn/hutool/core/img/ImgUtilTest.java +++ b/hutool-core/src/test/java/cn/hutool/core/img/ImgUtilTest.java @@ -1,6 +1,7 @@ package cn.hutool.core.img; import cn.hutool.core.io.FileUtil; +import cn.hutool.core.io.IORuntimeException; import org.junit.Assert; import org.junit.Ignore; import org.junit.Test; @@ -146,4 +147,25 @@ public class ImgUtilTest { System.out.println(mainColor); } + @Test + public void createImageTest() throws IORuntimeException, IOException { + ImgUtil.createImage( + "版权所有", + new Font("黑体", Font.BOLD, 50), + Color.WHITE, + Color.BLACK, + ImageIO.createImageOutputStream(new File("d:/test/createImageTest.png")) + ); + } + + @Test + public void createTransparentImageTest() throws IORuntimeException, IOException { + ImgUtil.createTransparentImage( + "版权所有", + new Font("黑体", Font.BOLD, 50), + Color.BLACK, + ImageIO.createImageOutputStream(new File("d:/test/createTransparentImageTest.png")) + ); + } + }