Java screenshot Tool

Source: Internet
Author: User

Java screen tool class, which can capture the current screen. The saved image format supports JPG and PNG formats and provides the main function test. This type of function is widely used, for example, in the CS system. Similar to the operating system function, images can be saved in local folders. In BS, the path is displayed in the browser, and you can enter the path. In our project, this class is mainly used to capture error page information. If the program encounters an exception, it will automatically display the error screen at this time. You can see what information the user entered when the program fails. <Br/> Public static void error (Object message) {<br/> logerror. Error (Message); <br/> guicamera. snapshot (); <br/>}

Package COM. hoo. utils; </P> <p> Import Java. AWT. dimension; <br/> Import Java. AWT. rectangle; <br/> Import Java. AWT. robot; <br/> Import Java. AWT. toolkit; <br/> Import Java. AWT. image. bufferedimage; <br/> Import Java. io. file; <br/> Import Java. text. simpledateformat; <br/> Import Java. util. calendar; <br/> Import javax. imageIO. imageIO; </P> <p>/** <br/> * the JavaBean can be called directly in other Java applications, implement "photographing" on the screen <br/> */<br/> @ suppresswarnings ("UNUSED ") <br/> public class guicamera {<br/>/** <br/> * the default file prefix is guicamera, file Format: PNG format the default construct will use the default <br/> * image file surname "guicamera ", and default image format "PNG" <br/> */<br/> private final static string format_png = "PNG "; <br/> private final static string format_jpg = "jpg"; <br/> Private Static string filepath = ""; // storage path <br/> Private Static string filename = "guicamera _"; // file prefix <br/> Private Static int serialnum = 0; <br/> Private Static string imageformat = guicamera. format_png; // Image File Format </P> <p> Private Static dimension D = toolkit. getdefatooltoolkit (). getscreensize (); </P> <p>/** <br/> * Snapshot the GUI once <br/> */<br/> Public static void snapshot () throws exception {<br/> try {<br/> // copy the screen to a bufferedimage object screenshot <br/> bufferedimage screenshot = (new robot ()). createscreencapture (New rectangle (0, 0, (INT) d. getwidth (), <br/> (INT) d. getheight (); <br/> serialnum ++; <br/> // depending on the file prefix variable and file format variable, automatically generate file name <br/> calendar c = calendar. getinstance (); <br/> simpledateformat SDF = new simpledateformat ("Mm, DD, yyyy, HH, mm, SS seconds "); <br/> string name = filepath + filename + SDF. format (C. gettime () + ". "+ imageformat; <br/> file F = new file (name); <br/> // write the screenshot object to the image file. <br/> ImageIO. write (screenshot, imageformat, f); <br/>} catch (exception e) {<br/> throw E; <br/>}</P> <p> Public static void main (string [] ARGs) throws exception {<br/> guicamera. filepath = "D: //"; <br/> guicamera. snapshot (); <br/>}< br/>

 

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.