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
* Http://community.csdn.net/Expert/topic/4844/4844995.xml? Temp =. 2917292.
**************************************** *************/
Public class test
{
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 test (){
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 test (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 ()));
Rectangle (200,330,520,400 ));
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 String cut (){
Test cam = new test ("D: // hello", "PNG ");
Cam. snapshot ();
Return "success ";
}
Public static void main (string [] ARGs)
{
Test cam = new test ("D: // hello", "PNG ");//
cam. snapshot ();
}< BR >}