Java 如何将网站的 HTML 转换为图像?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3960881/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert HTML of a website to an image?
提问by hernangarcia
Does anyone know how to do this? I've tried with JEditorPane but it does not work? Any other idea?
有谁知道如何做到这一点?我试过 JEditorPane 但它不起作用?还有其他想法吗?
Thanks in advance.
提前致谢。
This is the code I'm using:
这是我正在使用的代码:
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class HtmlToImage
{
protected static File generateOutput() throws Exception
{
// Create a temporary output file for the PNG image.
File outputFile = new File("Reporte.png");
outputFile.deleteOnExit();
JEditorPane pane = new JEditorPane();
pane.setContentType("text/html");
pane.setPage("http://www.google.com");
final JFrame frame = new JFrame();
frame.pack();
// Time Delay for the correct loading of the file.
try
{
Thread.sleep(5000);
}
catch(NumberFormatException nfe)
{
}
frame.add(pane);
frame.pack();
Dimension prefSize = pane.getPreferredSize();
pane.setSize(prefSize);
BufferedImage img = new BufferedImage( prefSize.width, prefSize.height,
BufferedImage.TYPE_INT_RGB);
Graphics2D g = (Graphics2D) img.getGraphics();
SwingUtilities.paintComponent(g, pane, frame, 0, 0, prefSize.width, prefSize.height);
ImageIO.write(img, "png", outputFile);
return outputFile;
}
public static void main(String[] args)
{
try
{
generateOutput();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
回答by Saul
You need to render the HTML and output the result as a picture file. Currently there is no full-fledged HTML renderer in core Java so you'll need a separate library or an application, WebRendererfor example. Simply invoke it from a servlet filter and override the response with rendering results.
您需要呈现 HTML 并将结果输出为图片文件。目前,核心 Java 中没有成熟的 HTML 渲染器,因此您需要一个单独的库或应用程序,例如WebRenderer。只需从 servlet 过滤器调用它并用呈现结果覆盖响应。
EditOpen source alternative to WebRenderer is Cobra
编辑WebRenderer 的开源替代品是Cobra
回答by dogbane
You could try using a JEditorPane as follows:
您可以尝试使用 JEditorPane 如下:
//load the webpage into the editor
JEditorPane ed = new JEditorPane(new URL("http://www.google.com"));
ed.setSize(200,200);
//create a new image
BufferedImage image = new BufferedImage(ed.getWidth(), ed.getHeight(),
BufferedImage.TYPE_INT_ARGB);
//paint the editor onto the image
SwingUtilities.paintComponent(image.createGraphics(),
ed,
new JPanel(),
0, 0, image.getWidth(), image.getHeight());
//save the image to file
ImageIO.write((RenderedImage)image, "png", new File("google.png"));
回答by optimus0127
You can also use Html2Image java API from Google.
您还可以使用 Google 的 Html2Image java API。