這是返回AWT的字體字符爲BufferedImage的方法:不能得到正確的寬度字體燒焦
private BufferedImage getCharImage(char ch) {
BufferedImage sizeImage = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
Graphics2D sizeGraphics = (Graphics2D) sizeImage.getGraphics();
if (antiAlias) {
sizeGraphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
}
FontMetrics fontMetrics = sizeGraphics.getFontMetrics(font);
int charwidth = fontMetrics.charWidth(ch);
if (charwidth <= 0) {
charwidth = 1;
}
int charheight = fontMetrics.getHeight();
if (charheight <= 0) {
charheight = fontSize;
}
BufferedImage charImage = new BufferedImage(charwidth, charheight, BufferedImage.TYPE_INT_ARGB);
Graphics2D charGraphics = (Graphics2D) charImage.getGraphics();
if (antiAlias) {
charGraphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
}
charGraphics.setFont(font);
charGraphics.setColor(Color.WHITE);
charGraphics.drawString(String.valueOf(ch), 0, fontMetrics.getAscent());
return charImage;
}
的問題是,我得到不正確的寬度,和人物不適合裝箱緩衝圖像。如果我要創建像這樣的緩存圖像new BufferedImage(charwidth + 10, charheight, BufferedImage.TYPE_INT_ARGB);
這些字符將適合圖像(這意味着我得到錯誤的字體寬度)。 爲什麼我面對這個問題,我該如何修復它?我使用arialbi.ttf(粗體,斜體)。
這是字體的渲染
編輯:
我這是怎麼定義字體變量:
font = Font.createFont(Font.TRUETYPE_FONT, inputStream);
font = font.deriveFont(fontSize);
請讓我知道如果你要我澄清一些東西。 – Qualphey 2013-04-04 16:15:38
顯示你如何定義你的'font'變量。 – whiskeyspider 2013-04-04 17:21:43
字體大小爲124 – Qualphey 2013-04-04 18:34:23