Java使用特定字体编写文本图像 [英] Java Write Text Image with Specific Fonts

查看:158
本文介绍了Java使用特定字体编写文本图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在现有图像的顶部写了一些文字,字体不太清晰。是否有一些使用Graphics2D或Font类的设置可以帮助在图像顶部写文本时使字体看起来更漂亮? Dante字体在我编写时不会以Dante的形式出现。我试过使用抗锯齿,但它没有效果(请参阅setRenderingHint)。不管有没有RenderingHint设置,图像都是一样的。有什么建议么?

I'm writing some text on top of an existing image and the font isn't very sharp. Is there some settings either with the Graphics2D or Font classes that help make fonts look nicer when writing text on top of images? The Dante font doesn't come out as Dante when I write it. I've tried to use antialiasing but it had no effect (see setRenderingHint). The images came out the same with or without the RenderingHint set. Any suggestions?

public class ImageCreator{

public void createImage(String text){
     Graphics2D g = img.createGraphics(); //img is a BufferedImage read in from file system
     g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                    RenderingHints.VALUE_ANTIALIAS_ON);
     Font fnt=new Font("Dante",1,20);
     Color fntC = new Color(4, 4, 109);       
     g.setColor(fntC);
     g.setFont(fnt);
     Dimension d = new Dimension(200, 113);
     drawCenteredString(text, d.width, d.height, g);
}
public static void drawCenteredString(String s, int w, int h, Graphics g) {
      FontMetrics fm = g.getFontMetrics();
      int x = (w - fm.stringWidth(s)) / 2;
      int y = (fm.getAscent() + (h - (fm.getAscent() + fm.getDescent())) / 2);
      g.drawString(s, x, y);
}

}  


推荐答案

使用 TextLayout ,如图所示此处

附录: TextLayout RenderingHints 可应用于 FontRenderContext

这篇关于Java使用特定字体编写文本图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆