在java中将字符串写入图像 [英] Writing String into a image in java

查看:30
本文介绍了在java中将字符串写入图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 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中将字符串写入图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆