Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java (Robot) screenshot beyond visible

I want to take a screen shot at once of an entire website that is larger than my screen. I know how to use the Robot class to take a screenshot of the visible area. One way of doing this I thought was:

  1. Start browser, go to desired website
  2. Start my program
  3. Program will take a screenshot of visible area
  4. Program will scroll down to make the second half of the page visible and take a screenshot
  5. Both screenshots will be combined

That is somewhat of a clumsy solution and at this point I'm not even sure it's possible (to scroll a webrowser's window). So I am looking for hints of a better solution.

Edit 1: This is somewhat what I envisioned in my original post. The flow is (prototype):

  1. Open browser, go to desired website in monitor 1
  2. In monitor 2, run program (in my case from Net beans)
  3. Program captures first screenshot, but if Robot.mouseScroll is invoked it's the NetBeans window that scrolls not the webrowser's
  4. To scroll the browser screen I move the mouse Monitor 1, click it to gain focus, scroll and then take another screenshot. Now I have two png images that can be stitched.

The problem is, while I could technically do this, I would not be able to do anything else on my computer at the same time, because I am moving my mouse via the program. The goal is to have my program taking screenshots and concurrently analyzing the images (stitching and extracting information), while I work in Excel. As currently setup, that is not possible.

Further more, it's starting to look that Java won't be able to do this at all. Interestingly enough JavaScript may be able to and seems like C++ with Win Api will be able to so. Which is a shame. Any thougths?

/** * @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 ...");
    }
}

}

EDIT 2: New Approach Ok, thinking this through, even if I figured out the focus thing (e.g. using alt+tab instead of moving) it wouldn't help, because I need to concurrently work in excel. I will start another question regarding a "virtual monitor" idea. Thank you both for providing ideas.

like image 856
PBD10017 Avatar asked Feb 27 '26 21:02

PBD10017


1 Answers

Will the pages render in a JEditorPane? If so, see the ComponentImageCapture class.

like image 73
Andrew Thompson Avatar answered Mar 02 '26 11:03

Andrew Thompson