J2se screenshot Program

Source: Internet
Author: User

Source: http://www.study-code.com/java/j2se/66419.htm

Package com. test;

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 called directly in other Java applications to implement "photo" This JavaBean is used to snapshot
* 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

Private String defaultName = "GuiCamera ";

Static int serialNum = 0;

Private String imageFormat; // Image File Format

Private String defaultImageFormat = "jpg ";

Dimension d = Toolkit. getdefatooltoolkit (). getScreenSize ();

/*************************************** ************************************
* The default file prefix is GuiCamera. the file format is PNG. The default construct will 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 structure supports the storage of JPG and PNG files.
**************************************** **********************************/
Public GuiCamera (String s, String format ){

FileName = s;
ImageFormat = format;
}

/*************************************** ************************************
* SnapShot the Gui once
**************************************** **********************************/
Public void snapShot (){

Try {
// Copy the screen to a BufferedImage object screenshot
BufferedImage screenshot = (new Robot ())
. CreateScreenCapture (new Rectangle (0, 0,
(Int) d. getWidth (), (int) d. getHeight ()));
SerialNum ++;
// Automatically generate a file name based on the file prefix variable and file format variable
String name = fileName + String. valueOf (serialNum) + "."
+ ImageFormat;
File f = new File (name );
System. out. print ("Save File" + name );
// Write the screenshot object to the 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: \ qq", "bmp ");//
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.