Tuesday, May 12, 2009

Sequential Screen Capture Utility

This is a simple java program that allows you take sequential screen prints with very less efforts.



import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Calendar;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;



public class ScreenCapture {


public static void main(String[] argv) throws Exception{

int i,time;
String option="S";
if(argv.length > 0) {
try {
time = Integer.parseInt(argv[0]);
for(i=0;i< time;i++){
captureandstorescreenshot();}}catch(NumberFormatException nfe) {
if (argv[0].equalsIgnoreCase("-help"))
{System.out.println("ScreenCapture Utility \n\n Usage:Run the program to capture screenshots for the next x seconds using the command [javac ScreenCapture x].\n Ex:javac ScreenCapture 20\n");
}
else if (argv[0].equalsIgnoreCase("-SE"))
{
while(!option.equalsIgnoreCase("X")){
InfiniteLoop t = new InfiniteLoop();
if(option.equalsIgnoreCase("S")){
t.start();}
Console console = System.console();
option= console.readLine("Enter E to end,S to start and X to exit?");
if(option.equalsIgnoreCase("E")){
Thread.sleep(1);
t.interrupt();}
}
}
else{
System.err.println(argv[0]+" is not a valid number of seconds.'javac ScreenCapture -help' for syntax.");
System.exit(-1);}}}
}
public static void CaptureAndStoreScreenShot(){
try{
Calendar rightNow = Calendar.getInstance();
Dimension scrensize = Toolkit.getDefaultToolkit().getScreenSize();
Robot robot = new Robot();
BufferedImage img = robot.createScreenCapture(new Rectangle(scrensize));
//difference (measured in milliseconds) between the current time and midnight, January 1, 1970 UTC
String filename ="screen"+System.currentTimeMillis()+".jpg";
ImageIO.write(img, "JPG", new File(filename));
}catch(Exception e){return;}
}
}

class InfiniteLoop extends Thread
{
public void run(){
for( ; ;){
ScreenCapture.CaptureAndStoreScreenShot();
if (Thread.interrupted()) {
// System.out.println("Interrupted");
return;
}
}
}
}
The below error occurs in older versions(Pre java 2) while interrupting the thread. It seems like a bug in older versions of java.

AWT blocker activation interrupted:
java.lang.InterruptedException
at java.lang.Object.wait(Native Method)
at java.lang.Object.wait(Object.java:429)
at sun.awt.AWTAutoShutdown.activateBlockerThread(AWTAutoShutdown.java:30
9)
at sun.awt.AWTAutoShutdown.setToolkitBusy(AWTAutoShutdown.java:226)
at sun.awt.AWTAutoShutdown.notifyToolkitThreadBusy(AWTAutoShutdown.java:
118)
at sun.awt.windows.WToolkit.(WToolkit.java:217)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstruct
orAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingC
onstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:274)
at java.lang.Class.newInstance0(Class.java:308)
at java.lang.Class.newInstance(Class.java:261)
at java.awt.Toolkit$2.run(Toolkit.java:760)
at java.security.AccessController.doPrivileged(Native Method)
at java.awt.Toolkit.getDefaultToolkit(Toolkit.java:739)
at ScreenCapture.CaptureAndStoreScreenShot(ScreenCapture.java:44)
at InfiniteLoop.run(ScreenCapture.java:58)

3 comments:

Anonymous said...

Ideal variant

crimsun said...

How do i use this? i desperatly need to schedule sequential screen shots of over 400 flash files! please help!

Nagarjun said...

@ crimsun

1)You need to save the program into .java file.
2)Compile it using the "javac" compiler, which can be installed by downloading and installing the latest version of the JDK from the sun website for free
3)run the command java filename. YOu can look up how to compile a java program.