【问题标题】:Java Write Text Image with Specific FontsJava 编写具有特定字体的文本图像
【发布时间】:2011-05-04 23:56:03
【问题描述】:

我在现有图像上写了一些文字,字体不是很清晰。 Graphics2D 或 Font 类是否有一些设置可以帮助在图像上书写文本时字体看起来更好?当我写它时,Dante 字体并没有像 Dante 一样出现。我尝试使用抗锯齿,但没有效果(请参阅 setRenderingHint)。无论有没有 RenderingHint 集,图像的结果都是一样的。有什么建议么?

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

}  

【问题讨论】:

    标签: java graphics graphics2d antialiasing


    【解决方案1】:

    使用TextLayout,如图here

    附录:TextLayout 的优点是RenderingHints 可以应用于FontRenderContext

    【讨论】:

    • 感谢您的回复垃圾神,但这实际上让它看起来更糟
    • 这令人失望。你从我引用的例子中得到了什么结果?您是否将您的RenderingHints 应用到TextLayoutFontRenderContext?你能用新代码更新你的问题吗?
    • 问题是字体在 JVM 上不可用。
    • 有趣。系统是否用不同的指标替换了不同的字体?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-26
    • 2019-07-10
    • 2011-08-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多