Pick Java Screen capture

Source: Internet
Author: User

Import Java.awt.Desktop;
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;

Public class Capturescreen {
public static void Capturescreen (String filename,string folder) throws exception{
Dimension defines the size of the picture, some methods defined by Toolkit can query the native operating system directly. The meaning of this sentence is to get the system screen size, save the Dimension type of screensize inside
Dimension screensize = Toolkit.getdefaulttoolkit (). Getscreensize () ;    
//rectangle Specifies an area in the coordinate space that can be defined by width and height
Rectangle screenrectangle = new Rectangle (screensize);
This class is used for native system input events for test automation, self-running demos, and other applications that require control of the mouse and keyboard.
Robot Robot = new Robot (); The Creatscreencapture method of the
//robot object is to create an image that contains the pixels read from the screen. The image does not include the mouse cursor. The images are then assigned to the Image object.
BufferedImage image = Robot.createscreencapture (Screenrectangle);    

//Save the path, save the image object as a picture file
//Create folder
File Screenfile = new file (fileName);
Determines whether the folder exists
if (!screenfile.exists ()) {
//creates the directory specified by this abstract path name.
Screenfile.mkdir ();
}

//Create a file based on the folder object and file name and format
. file F = new file (screenfile,folder);
Writes an image to File using any imagewriter that supports a fixed type. If there is already a File present, its contents are discarded.    The first of the
//period is the image memory data, the second is the text format, the third is the file object (also can be the data flow, but here to write files, so with the file stream)
Imageio.write (image, "PNG", f);
Automatically open
/* After Jdk1.6 a new class--desktop:the DeskTop class allows a Java application to
launch associated Applic Ations registered on the native desktop to handle a URI or a file.*/
//isdesktopsupported:tests whether this class is supported on the current platform.
//issupported:tests whether an action was supported on the current platform.
//action:represents an Action type. Plus. Open is the name that determines whether it can be opened, returns True
//getdesktop (): Returns the Desktop instance of The current browser context;
if (desktop.isdesktopsupported () &&desktop.getdesktop (). issupported (Desktop.Action.OPEN))
//  Gets the Desktop object that opens the object that is the file object
Desktop.getdesktop (). open (f);
}

public static void Main (string[] args) {
TODO auto-generated Method Stub
try{
Capturescreen ("f:\\ hello", "11.png");
}catch (Exception e) {
E.printstacktrace ();
}
}

}

Note: Selected from open source China, the copyright belongs to the author, prohibit any form of infringement!

Excerpt URL: http://www.oschina.net/code/snippet_1586782_48653

Pick Java Screen capture

Related Article

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.