Tuesday

Capture screen shots for one or more monitor in Java (Each capture to a single file or to a separate file)

package screen.capture;

import java.awt.AWTException;
import java.awt.Dimension;
import java.awt.DisplayMode;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;

import javax.imageio.ImageIO;

import java.io.File;
import java.io.IOException;

public class ScreenCapture {

       public static void main(String[] args) throws Exception {
          ScreenCapture sc = new ScreenCapture();
     
          sc.forOneMonitor("oneMonitor"); // outputs::  D:\oneMonitor.png
          sc.forMultipleMonitorInOneFile("multipleMonitor"); // outputs:: D:\multipleMonitor.png
          sc.forMultipleMonitorInMultipleFile("multipleMonitor_"); // outputs::  D:\multipleMonitor_Display0.png, D:\multipleMonitor_Display1.png
       }

       public void forOneMonitor(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("d:\\" + fileName + ".png"));
       }

       public void forMultipleMonitorInOneFile(String fileName) throws AWTException, IOException {
              Rectangle screenRect = new Rectangle(0, 0, 0, 0);
     
              GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
              GraphicsDevice[] gs = ge.getScreenDevices();
     
              for (GraphicsDevice gd : gs) {
                screenRect = screenRect.union(gd.getDefaultConfiguration().getBounds());
              }
     
              Robot robot = new Robot();
              BufferedImage capture = robot.createScreenCapture(screenRect);
              ImageIO.write(capture, "png", new File("d:\\" + fileName + ".png"));
       }

       public void forMultipleMonitorInMultipleFile(String fileName) throws AWTException, IOException {
              GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
              GraphicsDevice[] gdList = ge.getScreenDevices();

              for (GraphicsDevice gd : gdList) {
                DisplayMode mode = gd.getDisplayMode();
                Rectangle bounds = gd.getDefaultConfiguration().getBounds();
                Rectangle screenRect = new Rectangle((int) bounds.getMinX(), (int) bounds.getMinY(), (int) bounds.getWidth(), (int) bounds.getHeight());

                System.out.println("gd.getIDstring(): " + gd.getIDstring());
                System.out.println("Min (" + bounds.getMinX() + "," + bounds.getMinY() + ") Max(" + bounds.getMaxX()  + "," + bounds.getMaxY() + ")");
                System.out.println("Width : " + mode.getWidth() + "; Height :" + mode.getHeight());

                Robot robot = new Robot();
                BufferedImage capture = robot.createScreenCapture(screenRect);
                ImageIO.write(capture, "png", new File("d:\\" + fileName + gd.getIDstring().replace("\\", "") + ".png"));
              }
       }
}

Outputs:


No comments:

Post a Comment