根据文字创建透明背景的PNG图片

This commit is contained in:
hiqiuyi 2022-07-25 12:01:26 +08:00
parent 630ed4775f
commit 0f2fc9f3ae
2 changed files with 44 additions and 14 deletions

View File

@ -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());// 画出字符串

View File

@ -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"))
);
}
}