Java Application Implementation Screen "take photos"

Source: Internet
Author: User
Tags save file string format

Original: http://www.cnblogs.com/visec479/p/3842970.html

Sometimes, in Java application development, such as remote monitoring or distance learning, often need to intercept the computer's screen, because screen interception is closer to the operating system, under the Windows operating system, the operation is almost a VC, VB and other patents, in fact, the use of Java JDK1.4 robot object, to complete the "screen capture operation, more simple." Java JDK1.4 Robot object, which can complete a copy of the "screen" pixel to complete the screen image interception operation. This object can be called directly in a Java application to complete a screen capture of a particular application, and it is easy to monitor the remote server screen if the feature is combined with the network. This article shows you how to build a screen "camera" with Java and monitor the remote server screen, and give the corresponding Java source code.

 PackageCamera;ImportJava.awt.image.BufferedImage;ImportJava.io.*;ImportJavax.imageio.*;Importjava.awt.*;/******************************************************************* * This javabean can be called directly in other Java applications, enabling "take photos" of the screen This JavaBean was used to snapshot the GUI in a * Java application! You can embeded * it on to your Java application source code, and US * it to snapshot the right GUI of the application * /c6>@seejavax. ImageIO *@authorvisec Dana *@version1.0 *****************************************************/ Public classGuicamera {PrivateString FileName;//the prefix of the file    PrivateString defaultname = "Guicamera"; Static intSerialnum=0; PrivateString ImageFormat;//format of the image file    PrivateString defaultimageformat= "PNG"; Dimension D=Toolkit.getdefaulttoolkit (). Getscreensize (); /**************************************************************** * The default file prefix is guicamera and the file format is PNG format * that default Co NStruct would use the default * image file Surname "Guicamera", * and default image format "PNG" ************ ****************************************************/     PublicGuicamera () {fileName=DefaultName; ImageFormat=Defaultimageformat; }    /****************************************************************     * @params The surname of the snapshot file *@paramformat the format of the image file, * It can be "JPG" or "PNG" * This construct supports the storage of JPG and PNG files ******************* *********************************************/     PublicGuicamera (String s,string format) {FileName=s; ImageFormat=format; }    /**************************************************************** * Take a picture of the screen * SnapShot the Gui once ********* *******************************************************/     Public voidSnapShot () {Try {            //Copy the screen to a BufferedImage object screenshotBufferedImage screenshot = (NewRobot ()). Createscreencapture (NewRectangle (0, 0, (int) D.getwidth (), (int) (D.getheight ())); //automatically generate filenames based on file prefix variables and file format variablesString name=filename+string.valueof (Serialnum) + "." +ImageFormat; File F=NewFile (name); System.out.print ("Save File" +name); //to write an screenshot object to an image fileImageio.write (screenshot, ImageFormat, F); System.out.print (".. Finished!\n "); }        Catch(Exception ex) {System.out.println (ex); }    }}

To invoke a test case

 PackageCamera;ImportJava.text.SimpleDateFormat;Importjava.util.Date;/*** * To achieve "photo" on the screen *@authorvisec Dana*/ Public classclient{ Public Static voidMain (string[] args) {Date date=NewDate (); SimpleDateFormat DF=NewSimpleDateFormat ("yyyy-mm-dd-hh-mm"); Guicamera Cam=NewGuicamera ("f://" +df.format (date), "PNG");    Cam.snapshot (); }}

Data record generation picture

Java Application Implementation Screen "take photos"

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.