【问题标题】:how to create an emoji image with graphics2d and google noto color font如何使用 graphics2d 和 google noto 彩色字体创建表情符号图像
【发布时间】:2018-06-20 17:56:50
【问题描述】:

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

如果我对表情符号显示很陌生,我将不胜感激。

提前致谢,

桑德拉

【问题讨论】:

    标签: graphics2d emoji google-noto


    【解决方案1】:

    变化:

    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-08-07
      • 2021-07-02
      • 1970-01-01
      • 2017-10-03
      • 2018-07-10
      • 2020-07-04
      • 2021-04-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多