1 package test; 2 3 6 7 import java.awt.Dimension ; 8 import java.awt.Rectangle ; 9 import java.awt.Robot ; 10 import java.awt.Toolkit ; 11 import java.awt.image.BufferedImage ; 12 import java.io.File ; 13 14 import javax.imageio.ImageIO ; 15 16 public class Screenshot 17 { 18 19 public static void main(String [] args) throws Exception 20 { 21 if (args.length != 2) 24 { 25 System.err.println("Usage: java Screenshot " 26 + "WAITSECONDS OUTFILE.png"); 27 System.exit(1); 28 } 29 String outFileName = args[1]; 31 if (!outFileName.toLowerCase().endsWith(".png")) 32 { 33 System.err.println("Error: output file name must " 34 + "end with \".png\"."); 35 System.exit(1); 36 } 37 try 39 { 40 long time = Long.parseLong(args[0]) * 1000L; 41 System.out.println("Waiting " + (time / 1000L) + " second(s)..."); 42 Thread.sleep(time); 43 } 44 catch (NumberFormatException nfe) 45 { 46 System.err.println(args[0] + " does not seem to be a " 47 + "valid number of seconds."); 48 System.exit(1); 49 } 50 Toolkit toolkit = Toolkit.getDefaultToolkit(); 52 Dimension screenSize = toolkit.getScreenSize(); 53 Rectangle screenRect = new Rectangle (screenSize); 54 Robot robot = new Robot (); 56 BufferedImage image = robot.createScreenCapture(screenRect); 57 ImageIO.write(image, "png", new File (outFileName)); 59 System.out.println("Saved screen shot (" + image.getWidth() + " x " 61 + image.getHeight() + " pixels) to file \"" + outFileName 62 + "\"."); 63 } 67 } | Popular Tags |