Jim Lawless' Blog


Screen Capture from Multiple Monitors in Java

Originally published on: Fri, 23 Apr 2010 01:06:05 +0000

Here's a simple command-line screen-capture program in Java:

ScreenGrab.java


// ScreenGrab
// A simple command-line screen-grabber example.
//
// License: MIT / X11
// Copyright (c) 2010 by James K. Lawless
// jimbo@radiks.net http://www.radiks.net/~jimbo
// http://www.mailsend-online.com
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.

import java.awt.*;
import java.awt.image.*;
import javax.imageio.*;
import java.io.*;
 
public class ScreenGrab {
    public static void main(String[] arg) throws Exception {
      Dimension screenDim = Toolkit.getDefaultToolkit().getScreenSize();
      Robot robot = new Robot();
      BufferedImage image = robot.createScreenCapture(
         new Rectangle(0, 0, (int)screenDim.getWidth(),
            (int)screenDim.getHeight()));
       
      File fi=new File("ScreenGrab.jpg");
      ImageIO.write(image,"jpg",fi);
      Toolkit.getDefaultToolkit().beep();
    }
}

Type the following at a command-prompt after compiling the above to execute the program:

java ScreenGrab

You should hear an audible tone after the screen image has been saved. The file "ScreenGrab.jpg" in the current directory will contain the screen image.

In order to capture a single image that contains the screen bitmaps from both of two monitors, one simple has to multiply the screen width ( obtained from getScreenSize() ) by two:

ScreenGrab2.java


// ScreenGrab2
// A simple command-line screen-grabber example that grabs
// the screen images from two monitors.
//
// License: MIT / X11
// Copyright (c) 2010 by James K. Lawless
// jimbo@radiks.net http://www.radiks.net/~jimbo
// http://www.mailsend-online.com
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.

import java.awt.*;
import java.awt.image.*;
import javax.imageio.*;
import java.io.*;
 
public class ScreenGrab2 {
    public static void main(String[] arg) throws Exception {
      Dimension screenDim = Toolkit.getDefaultToolkit().getScreenSize();
      Robot robot = new Robot();
      BufferedImage image = robot.createScreenCapture(
         new Rectangle(0, 0, (int)screenDim.getWidth()*2,
            (int)screenDim.getHeight()));
       
      File fi=new File("ScreenGrab2.jpg");
      ImageIO.write(image,"jpg",fi);
      Toolkit.getDefaultToolkit().beep();
    }
}

Execute the above by typing the following at a command-prompt:

java ScreenGrab2

The file "ScreenGrab2.jpg" in the current directory should hold the combined bitmap images from both monitors.

The screen images for multiple monitors are actually just a single image, so we can obtain the entire image for all montiors or we can crop any segment of the entire image.

In order to capture the image from the second monitor only we simply crop the large image by specifying screenDim.getWidth() as the "x" coordinate while specifying the width of a single screen ( which is the same value ).

ScreenGrab3.java


// ScreenGrab3
// A simple command-line screen-grabber example that grabs
// the screen images from the second of two monitors.
//
// License: MIT / X11
// Copyright (c) 2010 by James K. Lawless
// jimbo@radiks.net http://www.radiks.net/~jimbo
// http://www.mailsend-online.com
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.

import java.awt.*;
import java.awt.image.*;
import javax.imageio.*;
import java.io.*;
 
public class ScreenGrab3 {
    public static void main(String[] arg) throws Exception {
      Dimension screenDim = Toolkit.getDefaultToolkit().getScreenSize();
      Robot robot = new Robot();
      BufferedImage image = robot.createScreenCapture(
         new Rectangle((int)screenDim.getWidth(), 0, (int)screenDim.getWidth(),
            (int)screenDim.getHeight()));
       
      File fi=new File("ScreenGrab3.jpg");
      ImageIO.write(image,"jpg",fi);
      Toolkit.getDefaultToolkit().beep();
    }
}

Execute the above by typing the following at a command-prompt:

java ScreenGrab3

The file "ScreenGrab3.jpg" in the current directory should hold the bitmap images from the second monitor only.

The same technique should work for any number of monitors. Please note that this code has been tested only under Windows.

Unless otherwise noted, all code and text entries are Copyright ©2010 by James K. Lawless

del_icio_us Save to del.icio.us
stumbleupon Save to StumbleUpon
digg Digg it
reddit Save to Reddit
facebook Share on Facebook
twitter Share on Twitter
aolfav More bookmarks



Previous post: Blogoversary
Next post:Twimmando No More


Search this Blog (and site)

Search this Site with PicoSearch


Subscribe to this Blog

 Subscribe!


Contact Me

Email: jimbo@radiks.net


Follow me on Twitter

http://twitter.com/lawlessGuy


Recent Posts

Mad Schemes : Learning Lisp via SICP

Auto Save Clipboard Images Redux

Extending SpiderMonkey JavaScript on Windows

Rhino JavaScript to EXE with launch4j

Compiling Rhino JavaScript to Java

Directory Traversal in Rhino JavaScript

Taking Shape

We've Moved!


Popular Posts

A Command-Line MP3 Player for Windows

Auto Save Images from the Clipboard

Java in a Windows EXE with launch4j

An Interview with Tom Zimmer: Forth System Developer

Setting Windows Console Text Colors in C


Random Posts

An SMTP Server Simulator in Perl

Along Came AWK

Directory Traversal in Rhino JavaScript

Setting Text Color in a Batch File

A TCP Command Line Interface in Rhino JavaScript

Throwaway Software: HangUp

Checking Shift States with DEBUG

An Interview with Game Developer James Hague

A Scrolling Banner using Canvas and JavaScript

Extracting URL Addresses from Text in C


Full List of Posts

http://www.mailsend-online.com/bloglist.htm


Blogroll

MicroISV on a Shoestring
DadHacker
The Bottom Feeder
Writin' That Code!
The Recursive ISV
The Thomsen Blog
Prototypically Speaking
The Reinvigorated Programmer