Import java.awt.Dimension;
Import Java.awt.Rectangle;
Import Java.awt.Robot;
Import Java.awt.Toolkit;
Import Java.awt.image.BufferedImage;
Import Java.io.File;
Import Javax.imageio.ImageIO;
/*******************************************************************
* This javabean can be invoked directly in other Java applications to achieve a "picture" of the screen
* This JavaBean are used to snapshot the GUI in a
* Java application! You can embeded
* It in to your Java application source code, and US
* It to snapshot the right GUI of the application
* @see javax. ImageIO
* @author Liluqun ([Email]liluqun@263.net[/email])
* @version 1.0
*
*****************************************************/
Public class Guicamera
{
private String fileName;//file prefix
& nbsp Private String DefaultName = "Guicamera";
static int serialnum=0;
private string imageformat;//image File format
private string defaultimageformat= "PNG";
Dimension d = Toolkit.getdefaulttoolkit (). Getscreensize ();
/****************************************************************
* Default file prefix is guicamera, file format is PNG format
* The default construct would use the default
* Image file surname "Guicamera",
* and default image format "PNG"
****************************************************************/
Public Guicamera () {
FileName = DefaultName;
Imageformat=defaultimageformat;
}
/****************************************************************
* @param s The surname of the snapshot file
* @param format the format of the image file,
* It can be "JPG" or "PNG"
* This construct supports the storage of JPG and PNG files
****************************************************************/
Public Guicamera (String s,string format) {
FileName = s;
Imageformat=format;
}
/****************************************************************
* Take a picture of the screen
* SnapShot the Gui once
****************************************************************/
public void SnapShot () {
try {
Copy screen to a BufferedImage object screenshot
BufferedImage screenshot = (new Robot ()). Createscreencapture (New
Rectangle (0, 0, (int) d.getwidth (), (int) d.getheight ()));
serialnum++;
Automatically generate file names based on file prefix variables and file format variables
String name=filename+string.valueof (Serialnum) + "." +imageformat;
File F = new file (name);
System.out.print ("Save File" +name);
To write a screenshot object to an image file
Imageio.write (screenshot, ImageFormat, F);
System.out.print (".. finished!/n ");
}
catch (Exception ex) {
System.out.println (ex);
}
}
public static void Main (string[] args)
{
Guicamera cam= New Guicamera ("D://hello", "PNG");
Cam.snapshot ();
}
}