Java Function Code 6-use Java to implement screen capture

Source: Internet
Author: User

6. Implement the screen using Java

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; </P> <p> Import javax. imageIO. imageIO; </P> <p> /******************************** * ********************************* <br/> * use Java to implement the screen <br/> * this JavaBean can be called directly in other Java applications <SPAN class = 'wp _ keywordlink '> Program </span>, implement "photo" on the screen <br/> **************************************** * ************/</P> <p> public class guicamera {<br/> private string filename; // file prefix <br/> private string defaultname = "guicamera"; <br/> static int serialnum = 0; <br/> private string imageformat; // Image File Format <br/> private string defaultimageformat = "PNG"; <br/> dimension D = toolkit. getdefatooltoolkit (). getscreensize (); </P> <p> /******************************** * * ***************************** <Br/> * 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/> public guicamera () {<br/> filename = defaultname; <br/> imageformat = defaultimageformat; </P> <p >}</P> <p> /************************ ************ * ************************** <Br/> * @ Param S <br/> * the surname of the snapshot file <br/> * @ Param Format <br/> * The format of the image file, it can be "jpg" or "PNG" <br/> * This structure supports the storage of JPG and PNG files <br/> ************ **************************************** * ***********/<br/> Public guicamera (string s, string format) {</P> <p> filename = s; <br/> imageformat = format; <br/>}</P> <p> /******************** **************************************** * *** <Br/> * takes a screenshot of the screen, Snapshot the GUI once <br/> ******************* **************************************** * *****/<br/> Public void snapshot () {</P> <p> try {<br/> // copy the screen to a bufferedimage object screenshot <br/> bufferedimage screenshot = (new robot ()) <br/>. createscreencapture (New rectangle (0, 0, <br/> (INT) d. getwidth (), (INT) d. getheight (); <br/> serialnum ++; <br/> // Automatically generate a file name based on the file prefix variable and file format variable <br/> string name = filename + String. valueof (serialnum) + ". "<br/> + imageformat; <br/> file F = new file (name); <br/> system. out. print ("save file" + name); <br/> // write the screenshot object to the image file <br/> ImageIO. write (screenshot, imageformat, f); <br/> system. out. print (".. finished! /N "); <br/>}catch (exception ex) {<br/> system. out. println (Ex); <br/>}</P> <p> Public static void main (string [] ARGs) {<br/> guicamera cam = new guicamera ("D: // hello", "PNG"); // <br/> cam. 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.