如何使用graphics2d和google noto颜色字体创建表情符号图像

时间:2018-01-11 11:55:12

标签: graphics2d emoji google-noto

我编写了一个java程序来渲染表情符号并用它创建一个本地图像文件。为此,我下载了Google" Noto Color Emoji"来自谷歌网站的字体。

这是mi代码:

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),但输出图像没有区别。

我很感激任何帮助,因为我在表情符号显示方面很新。

提前致谢,

桑德拉

0 个答案:

没有答案