Java(机器人)屏幕截图超出可见范围

标签 java screenshot awtrobot

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

  • 启动浏览器,进入所需网站
  • 启动我的程序
  • 程序将对可见区域进行截图
  • 程序将向下滚动以使页面的后半部分可见并截屏
  • 两张截图会合并

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

    编辑 1:这有点像我在原始帖子中所设想的。流程是(原型(prototype)):
  • 打开浏览器,在监视器 1 中转到所需的网站
  • 在监视器 2 中,运行程序(在我的例子中来自 Net beans)
  • 程序捕获了第一个屏幕截图,但如果调用 Robot.mouseScroll,则滚动的是 NetBeans 窗口,而不是浏览器的
  • 要滚动浏览器屏幕,我移动鼠标 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 中工作。我将开始另一个关于“虚拟监视器”想法的问题。感谢两位提供想法。

    最佳答案

    页面会在 JEditorPane 中呈现吗? ?如果是这样,请参阅 ComponentImageCapture 类(class)。

    关于Java(机器人)屏幕截图超出可见范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11919175/

    相关文章:

    java - 比较 Java 中可比较的泛型

    java - 如何统计文件中某个字符的出现次数?

    python - Python中flash元素的截图

    JAVA 机器人输入挪威字符

    java - char 到 KeyEvent int

    java - 从示例 : java calculate 10x20 获取输入

    java - JAR中有多个可运行类,如何运行它们?

    ios - 在我的 IOS 应用程序启动前截图

    ios - 在我们的应用程序外部使用 "Open Image with our app"和 "sleep/wake"按钮进行屏幕截图时,如何获取我们应用程序的本地通知 "home"