3

我想一次截取比我的屏幕大的整个网站的屏幕截图。我知道如何使用 Robot 类来截取可见区域的屏幕截图。我认为这样做的一种方法是:

  1. 启动浏览器,转到所需的网站
  2. 启动我的程序
  3. 程序将对可见区域进行截图
  4. 程序将向下滚动以使页面的后半部分可见并截取屏幕截图
  5. 两个屏幕截图将合并

这有点笨拙的解决方案,在这一点上我什至不确定它是否可能(滚动浏览器的窗口)。所以我正在寻找更好的解决方案的提示。

编辑 1:这有点像我在原始帖子中所设想的。流程是(原型):

  1. 打开浏览器,在显示器 1 中转到所需的网站
  2. 在监视器 2 中,运行程序(在我的例子中来自 Net beans)
  3. 程序捕获第一个屏幕截图,但如果调用 Robot.mouseScroll 它是 NetBeans 窗口滚动而不是 webbrowser
  4. 要滚动浏览器屏幕,我移动鼠标 Monitor 1,单击它以获得焦点,滚动然后再截取另一个屏幕截图。现在我有两个可以拼接的 png 图像。

问题是,虽然我在技术上可以做到这一点,但我无法同时在我的计算机上做任何其他事情,因为我正在通过程序移动鼠标。目标是让我的程序在 Excel 中工作时截取屏幕截图并同时分析图像(拼接和提取信息)。按照目前的设置,这是不可能的。

此外,Java 似乎根本无法做到这一点。有趣的是,足够多的 JavaScript 可能能够并且看起来像带有 Win Api 的 C++ 将能够如此。这是一种耻辱。有什么想法吗?

/** * @param args the command line arguments */ public static void main(String[] args) throws IOException { int width; int height;

    try{
        // Get screen devices.
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice[] gdevices = ge.getScreenDevices();

        // Find out widht and height of each, print line.
        for (int i = 0; i < gdevices.length; i++){
            System.out.println("Device " + i);
            System.out.println("Width:" + gdevices[i].getDisplayMode().getWidth());
            System.out.println("Height:" + gdevices[i].getDisplayMode().getHeight());
        }

        // Get width and height again for later. Don't worry about this now.
        width = gdevices[1].getDisplayMode().getWidth();
        height = gdevices[1].getDisplayMode().getHeight();

        // Initiate robot.
        Robot robot = new Robot(gdevices[1]);
        // Size of printscreen area. Temporary. Will be entire screen later.
        Rectangle captureSize = new Rectangle(0,0,500,500);

        // Take screenshot on gdevice[1]
        BufferedImage bufferedImage = robot.createScreenCapture(captureSize);
        File outputfile = new File("My Screenshot" + 1 + ".png");
        boolean write = ImageIO.write(bufferedImage, "png",outputfile);

        // Preparing to take another screenshot after.
        /* Need to move mouse to monitor where screenshot is taken. Mouse
         * is currently at a monitor that displays NetBeans, which is where 
         * I'm running this from, for now.
         */

        robot.mouseMove(200,200);
        /* Need to activate window by gaining focus, don't how to do with robot
         * do with mouse instead.
         */
        robot.mousePress(InputEvent.BUTTON1_MASK);
        robot.mouseRelease(InputEvent.BUTTON1_MASK);
        /* After the first screen shot, scroll screen that's now 
         * active (focused?)
         */
        robot.mouseWheel(3);

        // Take second screenshot.
        robot.delay(1000);
        bufferedImage = robot.createScreenCapture(captureSize);
        outputfile = new File("My Screenshot" + 2 + ".png");
        //write into second half
        write = ImageIO.write(bufferedImage, "png",outputfile);
    }
    catch (AWTException e){
        System.err.println("Somethingfishy is going on ...");
    }
}

}

编辑 2:新方法 好的,考虑到这一点,即使我找到了焦点(例如使用 alt+tab 而不是移动)它也无济于事,因为我需要同时在 excel 中工作。我将开始另一个关于“虚拟监视器”想法的问题。感谢两位提供想法。

4

4 回答 4

1

页面会呈现在JEditorPane? 如果是这样,请查看ComponentImageCapture课程。

于 2012-08-12T03:12:03.837 回答
0

机器人类可以完全按照您的概述使用,只需确保在单击滚动和第二个屏幕截图之间包含延迟。这是Robot的一个很好的应用。当你拥有它时,你应该在这里发布你的代码。我认为没有办法截取屏幕上没有的东西。您受到视频卡和显示分辨率的限制。

于 2012-08-12T02:28:46.900 回答
0

在问题本身中查看我的编辑。程序的流程必须重定向到虚拟监视器或类似的东西。谢谢。

于 2012-08-12T11:27:21.363 回答
0

尝试

      Dimension size = driver.manage().window().getSize();
      Point point = driver.manage().window().getPosition();
      Robot robot = new Robot();
      String format = "png";
      File tempFile = new File(System.getProperty("user.dir") + "//" + screenshotFolder + "//" + fileName);
      Rectangle captureRect = new Rectangle(point.getX(), point.getY(), size.getWidth(), size.getHeight());
      BufferedImage captureImage = robot.createScreenCapture(captureRect);
      ImageIO.write(captureImage, format, tempFile);
于 2015-06-11T11:15:30.583 回答