java-html2image创建图像

6yoyoihd  于 2021-08-25  发布在  Java
关注(0)|答案(0)|浏览(205)

在local中,我有一段代码将text/html转换为.jpg图像。
如果文本包含图像的数据(即数据:x),则数据部分将转换为字节,并在磁盘上创建这些字节的文件,文本/html将更改为引用该文件。
在此之后,我在磁盘上创建最后一个图像(文本和图像)。
最后一步使用以下代码完成:

//the below creates a HtmlImageGenerator with the <img src=""/> tag
HtmlImageGenerator htmlImageGenerator = new HtmlImageGenerator();
DOMSource domSource = new DOMSource(doc); //doc is a Document containing text and image
StringWriter stringWriter = new StringWriter();
StreamResult streamResult = new StreamResult(stringWriter);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.transform(domSource, streamResult);
htmlImageGenerator.loadHtml(stringWriter.toString().replace("<?xml version=\"1.0\" encoding=\"UTF-8\"?>", ""));

//sets the dimension and type of the new image
BufferedImage newBufferedImage = new BufferedImage(hig.getBufferedImage().getWidth(), hig.getBufferedImage().getHeight(), BufferedImage.TYPE_INT_RGB);

//create the graphics for the new image
newBufferedImage.createGraphics().drawImage(hig.getBufferedImage(), 0, 0, Color.WHITE, null);

//write the new image on disk
File outputfile = new File(myFilePath + ".jpg");
ImageIO.write(newBufferedImage, "jpg", outputfile);

在我的windows机器上,这很好,但是当我在linux机器上使用相同的代码时,最后一个映像被破坏了。


你知道为什么会这样吗?谢谢

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题