Is there a way to take a screenshot using Java and save it to some sort of image?

后端 未结 8 700
盖世英雄少女心
盖世英雄少女心 2020-11-22 06:19

Simple as the title states: Can you use only Java commands to take a screenshot and save it? Or, do I need to use an OS specific program to take the screenshot and then gra

8条回答
  •  囚心锁ツ
    2020-11-22 07:17

    Believe it or not, you can actually use java.awt.Robot to "create an image containing pixels read from the screen." You can then write that image to a file on disk.

    I just tried it, and the whole thing ends up like:

    Rectangle screenRect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
    BufferedImage capture = new Robot().createScreenCapture(screenRect);
    ImageIO.write(capture, "bmp", new File(args[0]));
    

    NOTE: This will only capture the primary monitor. See GraphicsConfiguration for multi-monitor support.

提交回复
热议问题