4

我写了一个java程序来渲染一个表情符号并用它创建一个本地图像文件。为此,我从谷歌网站下载了谷歌的“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),但输出图像没有区别。

我将不胜感激任何帮助,因为我对表情符号显示很陌生。

提前致谢,

桑德拉

4

1 回答 1

0

改变:

image = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);

至:

 image = new BufferedImage(100, 100, BufferedImage.TYPE_INT_ARGB);

它需要 TYPE_INT_ARGB。

并在 drawString() 之前添加:

g2.setPaint(Color.WHITE)
于 2022-02-16T08:37:50.437 回答