Java Code screenshot

Source: Internet
Author: User

Using Java, the robot class enables software screenshots, as follows:

Package Test;import Java.awt.awtexception;import Java.awt.displaymode;import java.awt.graphicsdevice;import Java.awt.graphicsenvironment;import Java.awt.image;import Java.awt.menuitem;import Java.awt.PopupMenu;import Java.awt.rectangle;import Java.awt.robot;import Java.awt.systemtray;import Java.awt.trayicon;import Java.awt.event.actionevent;import Java.awt.event.actionlistener;import Java.awt.event.keyevent;import Java.awt.event.mouseevent;import Java.awt.image.bufferedimage;import Java.io.file;import Java.io.filenotfoundexception;import Java.io.fileoutputstream;import Java.io.ioexception;import java.util.Date; Import Javax.imageio.imageio;public class Testrobot {private static Robot bt;private static GraphicsDevice gd;static {Gra Phicsenvironment ge = graphicsenvironment.getlocalgraphicsenvironment (); graphicsdevice[] GS = ge.getscreendevices (); gd = gs[0];try {bt = new Robot (GD);} catch (Awtexception e) {E.printstacktrace ();}} public static void Test1 () {try {displaymode mode = Gd.getdisPlayMode (); int width = mode.getwidth (); int height = mode.getheight (); System.out.println (width + "x" + height); BufferedImage img = bt.createscreencapture (new Rectangle (width,height));D ate date = new Date ();// The path below is used to save the path and name of the screenshot image FileOutputStream Fos = new FileOutputStream ("/home/qcq/" + date.gettime () + ". jpg"); Imageio.write ( IMG, "JPEG", FOS); Fos.close ();} catch (FileNotFoundException e) {e.printstacktrace ();} catch (IOException e) {e.printstacktrace ()}} public static void Main (string[] args) {Systemtray tray = Systemtray.getsystemtray ();//create the traytry {//here requires the icon for the given pallet. Image image = Imageio.read (new File ("src/1.jpg")); PopupMenu menu = new PopupMenu (); MenuItem m1 = new MenuItem ("screenshot"); MenuItem M0 = new MenuItem ("Exit"); Menu.add (M1); Menu.add (M0); TrayIcon TrayIcon = new TrayIcon (image, "screenshot", menu); Tray.add (TrayIcon); M1.addactionlistener (new ActionListener () {@ overridepublic void actionperformed (ActionEvent e) {test1 ();}}); M0.addactionlistener (new ActionListener () {@Overridepublic void actionPerformed (ActionEvent e) {system.exit (0);}}); /Automatically press the keyboard to display the desktop (Windows). Linux fails and can be used to intercept the desktop. Bt.keypress (keyevent.vk_windows); bt.keypress (keyevent.vk_d); Bt.keyrelease (keyevent.vk_windows); Bt.keyRelease ( Keyevent.vk_d);} catch (IOException e) {e.printstacktrace ();} catch (Awtexception e) {e.printstacktrace ()}}}


Java Code screenshot

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.