问题描述
如果我提供网页的URL作为输入,则我正在用Java构建一个Web应用程序,在该应用程序中我需要该网页的整个屏幕截图.
I am building a web application, in Java, where i want the whole screenshot of the webpage, if i give the URL of the webpage as input.
我的基本想法是捕获渲染组件的显示缓冲区.我不知道该怎么做.请帮助.
The basic idea i have is to capture the display buffer of the rendering component..I have no idea of how to do it..plz help..
推荐答案
对于可以扩展以支持并发呈现的纯Java解决方案,您可以使用Java HTML4/CSS2浏览器,例如眼镜蛇,它为GUI提供了一个Swing组件.实例化此组件时,可以调用它的paint(Graphics g)方法将其自身绘制到屏幕外的图像中
For a pure-java solution that can scale to support concurrent rendering, you could use a java HTML4/CSS2 browser, such as Cobra, that provides a Swing component for the GUI. When you instantiate this component, you can call it's paint(Graphics g) method to draw itself into an off-screen image
E.g.
Component c = ...; // the browser component
BufferedImage bi = new BufferedImage(c.getWidth(), c.getHeight(), TYPE_INT_RGB)
Graphics2d g = bi.createGraphics();
c.paint(g);
然后您可以使用Java图像API将其另存为JPG.
You can then use the java image API to save this as a JPG.
JPEGImageEncoder encoder = JPEGCodec.createEncoder(new FileOutputStream("screen.jpg"));
enncoder.encode(bi); // encode the buffered image
与已建立的本机浏览器相比,基于Java的浏览器通常显得苍白.但是,由于您的目标是静态图像而不是交互式浏览器,因此在这方面基于Java的浏览器可能绰绰有余.
Java-based browsers typically pale in comparison with the established native browsers. However, as your goal is static images, and not an interactive browser, a java-based browser may be more than adequate in this regard.
这篇关于使用Java将网页转换为jpeg图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!