Java screenshot Code __java

Source: Internet
Author: User
Tags save file string format

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 invoked directly in other Java applications to achieve a "picture" of the screen
* This JavaBean are 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 ([Email]liluqun@263.net[/email])
* @version 1.0
*
*****************************************************/

Public class Guicamera
{  
    private String fileName;//file prefix
  & nbsp Private String DefaultName = "Guicamera";
    static int serialnum=0;
    private string imageformat;//image File format
    private string defaultimageformat= "PNG";
    Dimension d = Toolkit.getdefaulttoolkit (). Getscreensize ();

/****************************************************************
* Default file prefix is guicamera, file format is PNG format
* The default construct would use the default
* Image file surname "Guicamera",
* and default image format "PNG"
****************************************************************/
Public Guicamera () {
FileName = DefaultName;
Imageformat=defaultimageformat;

}

/****************************************************************
* @param s The surname of the snapshot file
* @param format the format of the image file,
* It can be "JPG" or "PNG"
* This construct supports the storage of JPG and PNG files
****************************************************************/
Public Guicamera (String s,string format) {

FileName = s;
Imageformat=format;
}

/****************************************************************
* Take a picture of the screen
* SnapShot the Gui once
****************************************************************/
public void SnapShot () {

try {
Copy screen to a BufferedImage object screenshot
BufferedImage screenshot = (new Robot ()). Createscreencapture (New
Rectangle (0, 0, (int) d.getwidth (), (int) d.getheight ()));
serialnum++;
Automatically generate file names based on file prefix variables and file format variables
String name=filename+string.valueof (Serialnum) + "." +imageformat;
File F = new file (name);
System.out.print ("Save File" +name);
To write a screenshot object to an 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 ();
}
}

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.