使用 Graphics2D 在 BufferedImage 上覆盖文本并返回 BufferedImage
我已经检查了类似名称的问题,但他们没有回答这个用例。
基本上,我是在给定坐标(x,y)上覆盖一些文本(文本),我有以下函数一个包裹;
protected BufferedImage Process2(BufferedImage image){
Graphics2D gO = image.createGraphics();
gO.setColor(Color.red);
gO.setFont(new Font( "SansSerif", Font.BOLD, 12 ));
gO.drawString(this.text, this.x, this.y);
System.err.println(this.text+this.x+this.y);
return image;
}
我觉得我错过了一些显而易见的东西;我能找到的每个对 Graphics2D 的引用都是处理游戏或直接写入文件,但我只想返回 BufferedImage。 在当前代码中,覆盖层“渲染”后
,图像的末尾显示不变。
谢谢!
I have checked similarly named questions, but they don't answer this use case.
Basically, I was to overlay some text (text) at a given coordinate (x,y) I have the below function in a package;
protected BufferedImage Process2(BufferedImage image){
Graphics2D gO = image.createGraphics();
gO.setColor(Color.red);
gO.setFont(new Font( "SansSerif", Font.BOLD, 12 ));
gO.drawString(this.text, this.x, this.y);
System.err.println(this.text+this.x+this.y);
return image;
}
I feel like im missing something patently obvious; every reference to Graphics2D I can find is dealing with either games or writing directly to a file but I just want a BufferedImage returned. with the overlay 'rendered'
In the current code, the image appears out the end unchanged.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该方法
drawString()
使用 x 和 y 作为最左边字符的 基线。数字通常没有下行字母;如果text
也是如此,则在位置 (0,0) 处绘制的字符串将完全呈现在图像之外。请参阅此示例。附录:您可能遇到图像中不兼容的颜色模型的问题。一种简单的权宜之计是渲染图像,然后就地对其进行修改。
The method
drawString()
uses x and y for the leftmost character's baseline. Numbers typically have no descenders; if the same is true oftext
, a string drawn at position (0,0) will be rendered entirely outside the image. See this example.Addendum: You may be having trouble with an incompatible color model in your image. One simple expedient is to render the image and then modify it in situ.