Implement screenshot function in java

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.