我编写了一个Java程序来渲染表情符号并创建本地图像文件。为此,我从Google网站下载了Google的“Noto Color Emoji”字体。
这是我的代码:
package com.test;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.font.FontRenderContext;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class Test {
public static void main(String[] args) throws IOException {
String string = "";
BufferedImage image;
Font font = null;
try {
font = Font.createFont(Font.TRUETYPE_FONT, new FileInputStream("/home/sandra/.fonts/NotoColorEmoji.ttf"));
} catch (FontFormatException e) {
e.printStackTrace();
}
image = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = image.createGraphics();
g2.setFont(font);
g2.drawString(string, 0, g2.getFontMetrics().getAscent());
g2.dispose();
File f = new File("image.png");
if (!f.exists()) {
if (!f.createNewFile()) {
System.err.println("Can't create image file.");
}
}
try {
ImageIO.write(image, "png", f);
} catch (IOException e) {
e.printStackTrace();
}
}
我在Mac和Linux(Ubuntu)上尝试运行它,但两种情况下的输出都是一个黑色方块。
我错过了什么明显的东西吗?我还尝试过这篇文章结尾提到的这些命令(https://github.com/notwaldorf/ama/issues/53),但输出图像没有任何区别。
由于我对表情符号的显示很新,请帮忙解决问题,谢谢。
先感谢您的帮助,
Sandra