4

I wrote a java program to render an emoji and create a local image file with it. To do that I downloaded Google's "Noto Color Emoji" font from google website.

This is mi code:

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();
    }
}

}

I tried to run it in both mac and linux (ubuntu) and the output in both cases is a black square.

Am I missing anything obvious? I also tried these commands mentioned at the end of this post (https://github.com/notwaldorf/ama/issues/53) but there is no difference on the output image.

I would appreciate any help on this as I'm quite new on emoji display.

Thanks in advance,

Sandra

1 Answers1

0

Change:

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

to:

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

It needs TYPE_INT_ARGB.

And add this before drawString():

g2.setPaint(Color.WHITE)
ouflak
  • 2,458
  • 10
  • 44
  • 49