Java uses the Java runtime method to get the current screenshot

Source: Internet
Author: User

To save the screenshot picture to a local path:

 Packagecom.test;Importjava.awt.AWTException;Importjava.awt.Dimension;Importjava.awt.HeadlessException;ImportJava.awt.Rectangle;ImportJava.awt.Robot;ImportJava.awt.Toolkit;ImportJava.awt.image.BufferedImage;ImportJava.io.File;Importjava.io.IOException;ImportJavax.imageio.ImageIO; Public classMainrun {/**     * @paramargs*/     Public Static voidMain (string[] args) {Try{Dimension Dimension=Toolkit.getdefaulttoolkit (). Getscreensize (); BufferedImage screenshot= (NewRobot ()). Createscreencapture (NewRectangle (0,                    0, (int) Dimension.getwidth (), (int) (Dimension.getheight ())); File File=NewFile ("C:/screen.jpg"); Imageio.write (screenshot,"JPG", file); } Catch(headlessexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(awtexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }    }}

Web version, which will show the live picture as a stream back to the page:

/*** Get current screen * *@paramRequest *@paramResponse*/    Private voidGetscreenimg (HttpServletRequest request, httpservletresponse response)throwsException {thread.sleep (3000); Response.setcontenttype ("Image/jpg"); Servletoutputstream SOS=Response.getoutputstream (); //Disable page CachingResponse.setheader ("Pragma", "No-cache"); Response.setheader ("Cache-control", "No-cache"); Response.setdateheader ("Expires", 0); //Create a memory image and get its graphics contextDimension Dimension =Toolkit.getdefaulttoolkit (). Getscreensize (); BufferedImage screenshot= (NewRobot ()). Createscreencapture (NewRectangle (0, 0, (int) dimension. GetWidth (), (int) (Dimension.getheight ())); //outputting an image to the clientBytearrayoutputstream BOS =NewBytearrayoutputstream (); Imageio.write (screenshot,"JPG", BOS); byte[] buf =Bos.tobytearray ();        Response.setcontentlength (buf.length);        Sos.write (BUF);        Bos.close ();    Sos.close (); }

http://blog.csdn.net/songylwq/article/details/7714603

Java uses the Java runtime method to get the current screen method (go)

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.