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;
/*************************************** ****************************
* The JavaBean can be directly used in other Java applications.ProgramTo enable "photographing" on the screen"
* This Javabean is used to Snapshot the GUI in
* 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
{< br> private string filename; // file prefix
private string defaultname = "guicamera ";
static int serialnum = 0;
private string imageformat; // Image File Format
private string defaultimageformat = "PNG ";
dimension D = toolkit. getdefatooltoolkit (). getscreensize ();
/*************************************** *************************
* The default file prefix is guicamera. The file format is PNG.
* The default construct will 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 structure supports the storage of JPG and PNG files.
**************************************** ************************/
Public guicamera (string S, string format ){
Filename = s;
Imageformat = format;
}
/*************************************** *************************
* Take a photo of the screen
* Snapshot the GUI once
**************************************** ************************/
Public void snapshot (){
Try {
// Copy the screen to a bufferedimage object screenshot
Bufferedimage screenshot = (new robot (). createscreencapture (New
Rectangle (0, 0, (INT) D. getwidth (), (INT) D. getheight ()));
Serialnum ++;
// Automatically generate a file name based on the file prefix variable and file format variable
String name = filename + String. valueof (serialnum) + "." + imageformat;
File F = new file (name );
System. Out. Print ("save file" + name );
// Write the screenshot object to the 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 ();
}
}