我正在创建一个 servlet,它使用给定的文本呈现 jpg/png。我希望文本以渲染图像为中心。我可以得到宽度,但我得到的高度似乎是错误的
Font myfont = new Font(Font.SANS_SERIF, Font.BOLD, 400);
BufferedImage image = new BufferedImage(500, 500, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = image.createGraphics();
g.setFont(myfont);
g.setColor(Color.BLACK);
FontMetrics fm = g.getFontMetrics();
Integer textwidth = fm.stringWidth(imagetext);
Integer textheight = fm.getHeight();
FontRenderContext fr = g.getFontRenderContext();
LineMetrics lm = myfont.getLineMetrics("5", fr );
float ascent = lm.getAscent();
float descent = lm.getDescent();
float height = lm.getHeight();
g.drawString("5", ((imagewidth - textwidth) / 2) , y?);
g.dispose();
ImageIO.write(image, "png", outputstream);
这些是我得到的值: textwidth = 222 textheight = 504 ascent = 402 descent = 87 height = 503
有人知道如何获得“5”的确切高度吗?估计高度应该在250左右