简体   繁体   English

以编程方式获取Java小程序的屏幕截图

[英]Programatically getting a screenshot of a Java applet

I have tried to devise a way to get a screenshot of a Java applet running in a browser, but I can't seem to get it working. 我试图设计一种方法来获取在浏览器中运行的Java applet的屏幕截图,但是我似乎无法使其正常运行。 I managed successfully to use cutycapt to get screenshots fine from "normal" websites, but I soon found out that qtwebkit which it seems to rely on for the rendering does not support java. 我设法成功地使用cutycapt从“正常”网站上获取了屏幕截图,但是我很快发现它似乎依赖于渲染的qtwebkit不支持Java。 I also tried IEcapt thinking that it would somehow inherit the Java rendering capabilities of IE on the system, but it does not work. 我还尝试了IEcapt以为它将以某种方式继承系统上IE的Java呈现功能,但是它不起作用。 Flash also does not seem to be working in IEcapt , and it has no flags for enabling plugins, so I am assuming the functionality is not there either. Flash似乎也不能在IEcaptIEcapt ,并且它没有启用插件的标志,因此我假设该功能也不存在。

Does anyone have any thoughts on how you could render something like an /index.jsp to an image from a Windows or Linux command line? 是否有人对您如何从Windows或Linux命令行将类似/index.jsp的图像渲染到图像有任何想法?

Selenium webdriver might be useful here: Selenium Webdriver在这里可能有用:

http://docs.seleniumhq.org/projects/webdriver/ http://docs.seleniumhq.org/projects/webdriver/

It is used primarily for test automation but it might be helpful. 它主要用于测试自动化,但可能会有所帮助。

It could be used, for example, like this: 例如,可以像这样使用它:

import org.openqa.selenium.*;

WebDriver driver = new FirefoxDriver();  // create a Firefox webdriver instance
driver.get("http://www.google.com/");  // navigate to page
File screenshotFile = ((Screenshot)driver).getScreenshotAs(file); // capture screenshot
                                                                  // and save to a file

// here you can trigger any necessary actions on the website:
Webelement element = driver.findElement(By.name("q")).sendKeys("xxxxx");
element.click();
new WebDriverWait(driver, 10)).until(ExpectedConditions.titleContains("xxxxx"));

// and capture a new screenshot once the content has changed
File xxxScreenshotFile = ((Screenshot)driver).getScreenshotAs(file);

Have you tried using java.awt.Robot? 您是否尝试过使用java.awt.Robot?

    Rectangle rect = yourGragphicsConfiguration.getBounds();
    BufferedImage image = new Robot().createScreenCapture(rect);

If you know the position of your applet you might be able to get it with 如果您知道小程序的位置,则可以使用

    BufferedImage dest = image.getSubimage(appletX, appletY, appletHeight, appletWidth);

You can take a screenshot of Swing/AWT component. 您可以截取Swing / AWT组件的屏幕截图。

This can be done in 2 ways. 这可以通过两种方式完成。 In both cases the component must be visible. 在这两种情况下,组件都必须可见。

Without the robot use: 在不使用机器人的情况下:

BufferedImage image = new BufferedImage(component.getWidth(), 
component.getHeight(), BufferedImage.TYPE_INT_ARGB);
    Graphics g = image.getGraphics();
    component.paint(g);

With the robot use: 与机器人一起使用:

In this case the screenshot of area in which this component is situated will be made. 在这种情况下,将制作此组件所在区域的屏幕截图。 That is, if the component overlaps another application window then the screenshot will contain an area of this another window. 也就是说,如果组件与另一个应用程序窗口重叠,则屏幕截图将包含该另一个窗口的区域。

Point point = new Point(0, 0);
    SwingUtilities.convertPointToScreen(point, component);
    Rectangle region = component.getBounds();
    region.x = point.x;
    region.y = point.y;
    BufferedImage image= new Robot().createScreenCapture(region);

This information is taken from the article: Frequently Asked Questions during Java applet development 此信息摘自以下文章: Java applet开发期间的常见问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM