用 Java 截取一个网页的截图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1504034/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 13:33:59  来源:igfitidea点击:

Take a screenshot of a web page in Java

javascreenshot

提问by mgamer

Is there a free tool that can read given webpage and take a screenshot of it?

有没有免费的工具可以阅读给定的网页并截取它的截图?

采纳答案by Michael Lloyd Lee mlk

To build on two of the answers above:

以上述两个答案为基础:

Rendering the HTML in Java then saving to an image - A few Java based HTML renders exist, all with different sets of drawbacks. The most common is the one built in. This is quite simple and can only render fairly basic HTML. The most intresting I know of is The Flying Saucer Project. This can render fairly complex XHTML but you will have to convert HTML before you can use it (JTindymay be able to help here). Taking a Swing component and creating a image is fairly simple, you just pass an BufferedImages graphicsobject and pass it to the Swing components paintmethod. Then splat that out with ImageIO.
A big advantage to this would be that the renderer would be headless. The disadvantage is that it would not be a perfect rendering and it would lack any plugins.

在 Java 中渲染 HTML,然后保存到图像 - 存在一些基于 Java 的 HTML 渲染,所有这些都有不同的缺点。最常见的是内置的。这很简单,只能呈现相当基本的 HTML。我所知道的最有趣的是飞碟计划。这可以呈现相当复杂的 XHTML,但您必须先转换 HTML,然后才能使用它(JTindy可能会在这里提供帮助)。获取 Swing 组件并创建图像相当简单,您只需传递一个BufferedImages图形对象并将其传递给 Swing 组件的绘制方法。然后用ImageIO 将其删除。
这样做的一个很大优势是渲染器将是无头的。缺点是它不是一个完美的渲染,它会缺少任何插件。

The second option requires you to start a web browser, work out where it is and then take a screen shot. Optionally you may also wish to strip out all the Firefox/IE/Opera/etc menus leaving you with just image. To get the dimensions of the web browser the simplest option would be to start it full screen. The other option would be to use something like JDICs browsercomponent to include it as part of the Java application. It would then be able to specify where the HTML is being rendered on screen and then simply use Robotto create a screen shot of that area.
The big advantage to this is that it will give a perfect rendering (for a given browser). The two disadvantages is that it would require native code (or at least using a native component) and it could not be headless1.

第二个选项要求您启动 Web 浏览器,找出它的位置,然后进行屏幕截图。或者,您可能还希望删除所有 Firefox/IE/Opera/etc 菜单,只留下图像。要获得网络浏览器的尺寸,最简单的选择是全屏启动。另一种选择是使用 JDIC浏览器组件之类的东西将其包含为 Java 应用程序的一部分。然后它就能够指定 HTML 在屏幕上呈现的位置,然后简单地使用Robot创建该区域的屏幕截图。
这样做的最大优点是它将提供完美的渲染(对于给定的浏览器)。两个缺点是它需要本机代码(或至少使用本机组件)并且它不能是无头的1。

1) You could use a virtual frame buffer. But that is outside Java.

1)您可以使用虚拟帧缓冲区。但那是在 Java 之外。

回答by flybywire

use selenium-rc

使用硒-rc

回答by JZeeb

You can use the createScreenCapture method in awt.Robot. This method allows you to specify which portion of the screen to capture. So, you would still need to determine the coordinates of the window that contains the web page that you want to capture.

您可以在 awt.Robot 中使用 createScreenCapture 方法。此方法允许您指定要捕获屏幕的哪个部分。因此,您仍然需要确定包含要捕获的网页的窗口的坐标。

回答by Sam Barnum

To render the HTML in pure java, you might take a look at Flying Saucer.

要在纯 Java 中呈现 HTML,您可以查看 Flying Saucer。

http://code.google.com/p/flying-saucer//

http://code.google.com/p/flying-saucer//

It renders XML/XHTML/CSS 2.1

它呈现 XML/XHTML/CSS 2.1

I believe it only works on valid XML or XHTML, so if you need to render non-valid HTML, use a tool like neko to clean it up before passing it to flying saucer.

我相信它只适用于有效的 XML 或 XHTML,因此如果您需要呈现无效的 HTML,请在将其传递给飞碟之前使用像 neko 这样的工具进行清理。

回答by Janning

I had best results with Selenium Webdriver using a VirtualFramebuffer and Firefox Binary. This is tested under ubuntu. You need to have xvfb and firefox installed

我在使用 VirtualFramebuffer 和 Firefox Binary 的 Selenium Webdriver 上获得了最好的结果。这是在 ubuntu 下测试的。你需要安装 xvfb 和 firefox

First install firefox and virtual framebuffer:

首先安装firefox和虚拟帧缓冲区:

aptitude install xvfb firefox

Compile and run this class, open /tmp/screenshot.png afterwards

编译运行这个类,之后打开/tmp/screenshot.png

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxBinary;
import org.openqa.selenium.firefox.FirefoxDriver;

public class CaptureScreenshotTest
{
    private static int      DISPLAY_NUMBER  = 99;
    private static String   XVFB            = "/usr/bin/Xvfb";
    private static String   XVFB_COMMAND    = XVFB + " :" + DISPLAY_NUMBER;
    private static String   URL             = "http://www.google.com/";
    private static String   RESULT_FILENAME = "/tmp/screenshot.png";

    public static void main ( String[] args ) throws IOException
    {
        Process p = Runtime.getRuntime().exec(XVFB_COMMAND);
        FirefoxBinary firefox = new FirefoxBinary();
        firefox.setEnvironmentProperty("DISPLAY", ":" + DISPLAY_NUMBER);
        WebDriver driver = new FirefoxDriver(firefox, null);
        driver.get(URL);
        File scrFile = ( (TakesScreenshot) driver ).getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(scrFile, new File(RESULT_FILENAME));
        driver.close();
        p.destroy();
    }
}

回答by chrismarx

It's not Java, but after coming here, it's what I ended up using, so I think it's worth a mention. With PhantomJsyou can run a headless version of webkit, and then access the functionality through a built in mongoose webserver, which can handle requests for screencaptures, and store them locally. You can use Java to the make the request, and the response can have the url to the image on the server, so you could grab that too-

不是Java,但是来到这里之后,我最终使用了它,所以我认为值得一提。使用PhantomJs,您可以运行无头版本的 webkit,然后通过内置的 mongoose 网络服务器访问该功能,该服务器可以处理对屏幕截图的请求,并将它们存储在本地。您可以使用 Java 来发出请求,并且响应可以包含服务器上图像的 url,因此您也可以获取它-