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
Save to del.icio.us
Save to StumbleUpon
Digg it
Save to Reddit
Share on Facebook
Share on Twitter
More bookmarks