Java Screenshots and clipping

Source: Internet
Author: User

There is a robot class in the Java standard API that implements the screen and simulates the functions of the mouse keyboard operation. This shows only its screen.

The key method of Createscreencapture (Rectangle rect), which requires a Rectangle object, Rectangle is a rectangular area defining the screen, and constructing Rectangle is fairly easy:

New Rectangle (int x, int y, int width, int height), four parameters are the x-coordinate of the upper-left corner of the rectangle, the y-coordinate of the upper-left corner of the rectangle, the rectangle width, and the rectangle height. The BufferedImage method returns the object as a sample code:

1     /**2 * Specify the screen area, return the BufferedImage object3      * @paramx4      * @paramy5      * @paramwidth6      * @paramHeight7      * @return 8      */9      PublicBufferedImage Getscreenshot (intXintYintWidthintheight) {TenBufferedImage Bfimage =NULL; One         Try { ARobot Robot =NewRobot (); -Bfimage = Robot.createscreencapture (NewRectangle (x, y, width, height)); -}Catch(awtexception e) { the e.printstacktrace (); -         } -         returnBfimage; -}

If you need to keep the file, use Imageio.write (RenderedImage im, String formatname, file output), sample code:

1     /**2 * Specify screen area, save to specified directory3      * @paramx4      * @paramy5      * @paramwidth6      * @paramHeight7      * @paramSavepath-File Save path8      * @paramfilename-File Save name9      * @paramFormat -file formatTen      */ One      Public voidScreenshotasfile (intXintYintWidthintheight, string savepath, String fileName, string format) { A         Try { -Robot Robot =NewRobot (); -BufferedImage bfimage = Robot.createscreencapture (NewRectangle (x, y, width, height)); theFile Path =NewFile (savepath); -File File =NewFile (Path, filename+ "." +format); - imageio.write (bfimage, format, file); -}Catch(awtexception e) { + E.printstacktrace ();  -}Catch(IOException e) { + e.printstacktrace (); A         } at}

Java screen and clipping

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.