需求:图片上渲染上文字,到测试有中文乱码,原因是 Linux 系统缺少字体库
涉及到的类
java.awt.Font 、 java.awt.Graphics2D
public void add(Graphics2D g, BufferedImage bimage, Map<String, Object> map) {
String markContent = (String) map.get("text");
g.setColor((Color) map.get("color"));
g.setBackground(Color.white);
AttributedString ats = new AttributedString(markContent);
Font f = new Font((String) map.get("fontType"), (Integer) map.get("style"),
(Integer) map.get("fontSize"));
ats.addAttribute(TextAttribute.FONT, f, 0, markContent.length());
AttributedCharacterIterator iter = ats.getIterator();
// 添加水印的文字和设置水印文字出现的内容
g.drawString(iter, (Integer) map.get("x"), (Integer) map.get("y"));
}
解决办法:
在 C:\Windows\Fonts 文件夹下找到使用的字体,复制出来后缀改成 .ttc, 复制到
/usr/share/fonts/dejavu/
之后重启服务即可。