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 called directly in other Java applications to implement "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 (liluqun@263.net)
* @ Version 1.0
*
**************************************** *************/
Public class GuiCamera
{
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: \", "jpg ");//
Cam. snapShot ();
}
}
Author: ERDP Technical Architecture"