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, implement "photo" * This JavaBean is 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 (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; // format of the image file priv Ate 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 = defaul TName; imageFormat = defaultImageFormat ;} /*************************************** * ************************ @ param s the surname of the snapshot file * @ param format format of the image file, * it can be "jpg" or "png" * 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 screenshotBufferedImage screenshot = (new Robot ()). createScreenCapture (newRectangle (0, 0, (int) d. getWidth (), (int) d. getHeight (); serialNum ++; // automatically generates the file name String name = fileName + String Based on the file prefix variable and file format variable. valueOf (seri AlNum) + ". "+ 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 ();}}
This article from the "Dream think Xi" blog, please be sure to keep this source http://qiangmzsx.blog.51cto.com/2052549/1304580