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

后端 未结 8 730
盖世英雄少女心
盖世英雄少女心 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:00

    I never liked using Robot, so I made my own simple method for making screenshots of JFrame objects:

    public static final void makeScreenshot(JFrame argFrame) {
        Rectangle rec = argFrame.getBounds();
        BufferedImage bufferedImage = new BufferedImage(rec.width, rec.height, BufferedImage.TYPE_INT_ARGB);
        argFrame.paint(bufferedImage.getGraphics());
    
        try {
            // Create temp file
            File temp = File.createTempFile("screenshot", ".png");
    
            // Use the ImageIO API to write the bufferedImage to a temporary file
            ImageIO.write(bufferedImage, "png", temp);
    
            // Delete temp file when program exits
            temp.deleteOnExit();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }
    
    0 讨论(0)
  • 2020-11-22 07:00
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();  
    GraphicsDevice[] screens = ge.getScreenDevices();       
    Rectangle allScreenBounds = new Rectangle();  
    for (GraphicsDevice screen : screens) {  
           Rectangle screenBounds = screen.getDefaultConfiguration().getBounds();        
           allScreenBounds.width += screenBounds.width;  
           allScreenBounds.height = Math.max(allScreenBounds.height, screenBounds.height);
           allScreenBounds.x=Math.min(allScreenBounds.x, screenBounds.x);
           allScreenBounds.y=Math.min(allScreenBounds.y, screenBounds.y);
          } 
    Robot robot = new Robot();
    BufferedImage bufferedImage = robot.createScreenCapture(allScreenBounds);
    File file = new File("C:\\Users\\Joe\\Desktop\\scr.png");
    if(!file.exists())
        file.createNewFile();
    FileOutputStream fos = new FileOutputStream(file);
    ImageIO.write( bufferedImage, "png", fos );
    

    bufferedImage will contain a full screenshot, this was tested with three monitors

    0 讨论(0)
  • 2020-11-22 07:03

    Toolkit returns pixels based on PPI, as a result, a screenshot is not created for the entire screen when using PPI> 100% in Windows. I propose to do this:

    DisplayMode displayMode = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()[0].getDisplayMode();
    Rectangle screenRectangle = new Rectangle(displayMode.getWidth(), displayMode.getHeight());
    BufferedImage screenShot = new Robot().createScreenCapture(screenRectangle);
    
    0 讨论(0)
  • 2020-11-22 07:08
    public void captureScreen(String fileName) throws Exception {
       Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
       Rectangle screenRectangle = new Rectangle(screenSize);
       Robot robot = new Robot();
       BufferedImage image = robot.createScreenCapture(screenRectangle);
       ImageIO.write(image, "png", new File(fileName));
    }
    
    0 讨论(0)
  • 2020-11-22 07:16
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Rectangle;
    import java.awt.Robot;
    import java.awt.Toolkit;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.image.BufferedImage;
    import java.io.File; 
    import javax.imageio.ImageIO;
    import javax.swing.*;  
    
    public class HelloWorldFrame extends JFrame implements ActionListener {
    
    JButton b;
    public HelloWorldFrame() {
        this.setVisible(true);
        this.setLayout(null);
        b = new JButton("Click Here");
        b.setBounds(380, 290, 120, 60);
        b.setBackground(Color.red);
        b.setVisible(true);
        b.addActionListener(this);
        add(b);
        setSize(1000, 700);
    }
    public void actionPerformed(ActionEvent e)
    {
        if (e.getSource() == b) 
        {
            this.dispose();
            try {
                Thread.sleep(1000);
                Toolkit tk = Toolkit.getDefaultToolkit(); 
                Dimension d = tk.getScreenSize();
                Rectangle rec = new Rectangle(0, 0, d.width, d.height);  
                Robot ro = new Robot();
                BufferedImage img = ro.createScreenCapture(rec);
                File f = new File("myimage.jpg");//set appropriate path
                ImageIO.write(img, "jpg", f);
            } catch (Exception ex) {
                System.out.println(ex.getMessage());
            }
        }
    }
    
    public static void main(String[] args) {
        HelloWorldFrame obj = new HelloWorldFrame();
    }
    }
    
    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题