根据未知行数创建图片 [英] Create image from text with unknown number of lines

查看:92
本文介绍了根据未知行数创建图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将一串文字转换成图像。问题是,如果文本比图像的长度更宽,并且图像的高度要动态调整以适合文本的大小,我希望文本能够被包裹,以便我知道文本占用了多少空间。 / p>

我在Java中工作,有几件事我尝试过:


  • 在JPanel中呈现HTML并保存为BufferedImage。这里的问题是,我使用的大多数CSS被JPanel忽略,图像无法使用。

  • 使用ImageMagick和img4Java。
  • 使用ImageMagick和img4Java。这个解决方案的两个重大失败是我需要安装命令行工具,这是我无法在我们的服务器上完成的。第二个原因是我无法轻松将图像转换为缓冲图像以供应用程序的其余部分使用。




有没有人知道在Java中这样做的方法?



谢谢!

解决方案

您可以使用包含HTML的标签作为换行符,如。




I would like to convert a string of text into an image. The issue is, I want the text to wrap if it is wider than the length of the image, and the height of the image to be dynamically sized to perfectly fit the text, so that I know how much space the text takes up.

I'm working in Java and there are several things I have tried:

  • Rendering HTML in a JPanel and saving as a BufferedImage. The problem here was that most of the css I used was ignored by the JPanel and the image was unusable.

  • Using ImageMagick and img4Java. The two big failures with this solution was that I needed the command-line tool installed, which I can't do on our server. The second was that I couldn't easily convert the image to buffered image for use in the rest of the app.

Does anyone know a way to do this in Java?

Thanks!

解决方案

You might use a label containing HTML for the line-wrap, as shown here.

To get an image of that, see LabelRenderTest.

这篇关于根据未知行数创建图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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