0

我正在尝试生成bufferedImage给定文本大小的 a 。使用系统字体时,没有问题。我三次检查了位置,所以这不应该是我的错误。如果需要,我可以在某处上传字体。

font = Font.createFont(Font.TRUETYPE_FONT, ttfStream);

使用 .ttf 文件时出现错误,表明其中没有数据。

font = Font.createFont(Font.TRUETYPE_FONT, ttfStream);

错误说:

Exception in thread "main" java.lang.IllegalArgumentException: Width (0) and height (1) cannot be <= 0
at java.awt.image.DirectColorModel.createCompatibleWritableRaster(DirectColorModel.java:1016)
at java.awt.image.BufferedImage.<init>(BufferedImage.java:340)
at ErrorExample.stringToBufferedImage(Untitled.java:64)
at ErrorExample.main(Untitled.java:35)

示例代码:

class ErrorExample {
    static boolean dontwork = true;

    public static void main(String[] args) throws IOException, FontFormatException{
        InputStream ttfStream = new BufferedInputStream(new FileInputStream("/test/monofont.ttf"));
         Font font;
        if(dontwork == true){ //here the fun seems to be.
            font = Font.createFont(Font.TRUETYPE_FONT, ttfStream);
        }else{
            font = new Font( "Verdana", Font.BOLD, 20 );
        }
        BufferedImage img = stringToBufferedImage(font, "sdf");

    System.out.println("Done.");
    }


    /**
         * Modiefied from http://stackoverflow.com/a/17301696/3423324
         * @param font 
         */
        public static BufferedImage stringToBufferedImage(Font f, String s) {
            //First, we have to calculate the string's width and height

            BufferedImage img = new BufferedImage(1, 1, BufferedImage.TYPE_4BYTE_ABGR);
            Graphics g = img.getGraphics();

            //Set the font to be used when drawing the string
            //f = new Font("Tahoma", Font.PLAIN, 48);
            g.setFont(f);

            //Get the string visual bounds
            FontRenderContext frc = g.getFontMetrics().getFontRenderContext();
            Rectangle2D rect = f.getStringBounds(s, frc);
            //Release resources
            g.dispose();

            //Then, we have to draw the string on the final image

            //Create a new image where to print the character
            img = new BufferedImage((int) Math.ceil(rect.getWidth()), (int) Math.ceil(rect.getHeight()), BufferedImage.TYPE_INT_ARGB);
            //Graphics2D g2d = img.;
            //g2d.setColor(Color.black); // Otherwise the text would be white

            g = img.getGraphics();
            Graphics2D g2d = (Graphics2D) g;
            g.setColor(Color.black); //Otherwise the text would be white
            g2d.setColor(Color.black); //Otherwise the text would be white
            g2d.setFont(f);

            //Calculate x and y for that string
            FontMetrics fm = g.getFontMetrics();
            int x = 0;
            int y = fm.getAscent(); //getAscent() = baseline
            g2d.drawString(s, x, y);

            //Release resources
            g.dispose();

            //Return the image
            return img;
        }

}
4

1 回答 1

3

问题是:新加载的字体没有嵌入大小信息。来自 Javadoc:

这些字体以 Font 对象的形式返回,大小为 1、标识变换和默认字体特征。然后,这些基本字体可用于通过deriveFont此类中的方法派生具有不同大小、样式、转换和字体特征的新 Font 对象。

http://docs.oracle.com/javase/7/docs/api/java/awt/Font.html

使用系统字体时,大小已在给定参数中设置。

使用 ttf 但情况并非如此,必须手动设置大小:

font = font.deriveFont( 20f );

另请注意,它是一个浮点值,因为该函数deriveFont重载了一个int值,该值将设置样式,而不是大小。

于 2014-07-04T06:01:20.853 回答