本文介绍了用Java将String写入图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用ImageIo
将字符串写入图像.但是,在写入大字符串时,不会在该图像中写入完整字符串.
I am trying to write a string into a image using ImageIo
. But while writing a large string ,full string is not written into that image.
这是我的代码:
File url=new File(imgUrl);
BufferedImage image = ImageIO.read(url);
Graphics g = image.getGraphics();
g.setPaintMode();
g.setFont(g.getFont().deriveFont(30f));
g.drawString(text, 100, 100);
g.dispose();
此代码适用于小字符串.但是,当字符串的宽度超过图像的宽度时,完整的字符串将不会显示在该图像上.
This code works fine for small strings.but when the width of the string exceeds the width of the image,then full string is not displayed on that image.
有什么建议吗?
推荐答案
未经测试,但可以这样做:
Not tested, but it could be done as this:
JLabel label = new JLabel("<html><h2>Title</h2><p>large text ...</p>");
int w = image.getWidth();
int h = image.getHeigth();
label.setBounds(0, 0, w, h);
SwingUtilities.paintComponent(g, label, null, 0, 0, w, h);
这篇关于用Java将String写入图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!