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



Views expressed in this blog are those of the author and do not necessary reflect those of the author's employer. Views expressed in the comments are those of the responding individual.

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


About Jim ...


Click **here**
to try out MailWrench;
a command-line SMTP /
SMTPS (Google Gmail)
mailer for Windows.


Follow me on Twitter

http://twitter.com/lawlessGuy


Recent Posts

A JavaScript REPL for Android Devices

MailSend is Free

My Blog Engine

The October 10th Bug

A Review of Kevin Mitnick's Book Ghost in the Wires

Spellbound by Web Programming

Backlinks to my Blog Posts

Play MP3 Files with Python on Windows


Random Posts

Directory Traversal in Rhino JavaScript

My Foray into Shareware

A Review of Kevin Mitnick's Book Ghost in the Wires

COM Scripting in C by way of JavaScript

A Lightweight Alternative to Windows Shortcuts

Windows Text to Speech in WSH JavaScript

Stacking Images with PerlMagick

An Embedded Mini-Interpreter

Setting Text Color in a Batch File

Generating Primes with XSLT


Full List of Posts

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


Recent Posts from my Other Blog

Remembering Dr. San Guinary

Why Some Web Sites will go Dark on Jan 18th

SNL Superhero Skit

More Ruby Games

My Ruby Game Challenge Entry

Steal this Bookmarklet

Nerd Toys

Learn New Jargon, You Must

Spot the Wiebe

Tech Magazine Glory Days

Book Review : Paull Allen - Idea Man

A 90's Experiment in Online Systems - The U.S. West CommunityLink Service