Example usage for java.awt Robot createScreenCapture

List of usage examples for java.awt Robot createScreenCapture

Introduction

In this page you can find the example usage for java.awt Robot createScreenCapture.

Prototype

public synchronized BufferedImage createScreenCapture(Rectangle screenRect) 

Source Link

Document

Creates an image containing pixels read from the screen.

Usage

From source file:Main.java

public static void main(String[] arg) throws Exception {
    Dimension screenDim = Toolkit.getDefaultToolkit().getScreenSize();
    Robot robot = new Robot();

    BufferedImage image = robot
            .createScreenCapture(new Rectangle(0, 0, (int) screenDim.getWidth(), (int) screenDim.getHeight()));

    JWindow window = new JWindow(new JFrame());
    window.getContentPane().setLayout(new BorderLayout());
    window.getContentPane().add(BorderLayout.CENTER, new JLabel(new ImageIcon(image)));
    window.pack();// w w  w. j  a va2 s . c  o m
    window.setVisible(true);
}

From source file:Capture.java

public static void main(String[] args) {
    JFrame capture = new JFrame();
    capture.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    Toolkit kit = Toolkit.getDefaultToolkit();
    final Dimension d = kit.getScreenSize();
    capture.setSize(d);//  w w  w .  j  av a2s  .co m

    Rectangle rect = new Rectangle(d);
    try {
        Robot robot = new Robot();
        final BufferedImage image = robot.createScreenCapture(rect);
        image.flush();
        JPanel panel = new JPanel() {
            public void paintComponent(Graphics g) {
                g.drawImage(image, 0, 0, d.width, d.height, this);
            }
        };
        panel.setOpaque(false);
        panel.prepareImage(image, panel);
        panel.repaint();
        capture.getContentPane().add(panel);
    } catch (Exception e) {
        e.printStackTrace();
    }

    capture.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Robot robot = new Robot();
    Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
    BufferedImage image = robot.createScreenCapture(new Rectangle(d));
    BufferedImage sub = image.getSubimage(0, 0, 400, 400);
    File f = new File("SubImage.png");
    ImageIO.write(sub, "png", f);
    final ImageIcon im = new ImageIcon(f.toURI().toURL());

    Runnable r = new Runnable() {

        @Override/*  w  w  w .j  a va2  s  . co  m*/
        public void run() {
            JOptionPane.showMessageDialog(null, new JLabel(im));
        }
    };
    SwingUtilities.invokeLater(r);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Robot robot = new Robot();
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    BufferedImage screen = robot.createScreenCapture(new Rectangle(screenSize));

    new ScreenCaptureRectangle(screen);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Robot robot = new Robot();
    int x = 100;/*from  w w  w  .  ja va  2 s .c o m*/
    int y = 100;
    int width = 200;
    int height = 200;
    Rectangle area = new Rectangle(x, y, width, height);
    BufferedImage bufferedImage = robot.createScreenCapture(area);

    area = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
    bufferedImage = robot.createScreenCapture(area);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Robot robot = new Robot();
    int x = 100;//from  www.  ja  v  a  2  s  .c o  m
    int y = 100;
    int width = 200;
    int height = 200;
    Rectangle area = new Rectangle(x, y, width, height);
    BufferedImage bufferedImage = robot.createScreenCapture(area);

    // Capture the whole screen
    area = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
    bufferedImage = robot.createScreenCapture(area);

}

From source file:net.librec.util.Systems.java

/**
 * Capture screen with the input string as file name
 *
 * @param fileName  a given file name//from   ww  w.j  a va2 s .co  m
 * @throws Exception if error occurs
 */
public static 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);

    File file = new File(fileName);
    ImageIO.write(image, "png", file);

    LOG.debug("A screenshot is captured to: " + file.getPath());
}

From source file:org.pentaho.reporting.designer.core.actions.global.ScreenCaptureAction.java

public static void saveScreenShot(final int modifiers) {
    final Component component = KeyboardFocusManager.getCurrentKeyboardFocusManager().getActiveWindow();
    final GraphicsConfiguration graphicsConfiguration = component.getGraphicsConfiguration();
    final GraphicsDevice graphicsDevice = graphicsConfiguration.getDevice();
    try {/* ww  w  . j  ava2  s  .c om*/
        final Robot robot = new Robot(graphicsDevice);
        final BufferedImage image;
        if ((modifiers & ActionEvent.SHIFT_MASK) == ActionEvent.SHIFT_MASK) {
            image = robot.createScreenCapture(graphicsConfiguration.getBounds());
        } else {
            image = robot.createScreenCapture(component.getBounds());
        }

        final String homeDirectory = ReportDesignerBoot.getInstance().getGlobalConfig()
                .getConfigProperty("user.home", ".");
        final File homeDir = new File(homeDirectory);
        final File f = generateName(homeDir);
        if (f == null) {
            return;
        }
        final FileOutputStream fout = new FileOutputStream(f);
        try {
            final PngEncoder encoder = new PngEncoder();
            encoder.setCompressionLevel(6);
            encoder.setEncodeAlpha(false);
            encoder.setImage(image);
            final byte[] bytes = encoder.pngEncode();
            fout.write(bytes);
        } finally {
            fout.close();
        }
    } catch (IOException ioe) {
        UncaughtExceptionsModel.getInstance().addException(ioe);
    } catch (AWTException e1) {
        // ignore
        UncaughtExceptionsModel.getInstance().addException(e1);
    }
}

From source file:com.tascape.qa.th.Utils.java

/**
 *
 * @param png picture file name/*  www .j a v a 2  s  . c  om*/
 *
 * @throws AWTException UI related exception
 * @throws IOException  file IO issue
 */
public static void captureScreen(File png) throws AWTException, IOException {
    Robot robot = new Robot();
    BufferedImage image = robot.createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
    ImageIO.write(image, "png", png);
    LOG.debug("Save screenshot to {}", png.getAbsolutePath());
}

From source file:com.cisco.dbds.utils.selenium.SeleniumUtilities.java

/**
 * This function captures the Screenshot of the screen and converts it into
 * a byte array to embed in cucumber reports.
 * /* w w w .  j av a 2  s . c o m*/
 * @return the byte[]
 * @throws Exception
 *             the exception
 */
public static byte[] captureScreen() throws Exception {

    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    Rectangle screenRectangle = new Rectangle(screenSize);
    Robot robot = new Robot();
    BufferedImage image = robot.createScreenCapture(screenRectangle);

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ImageIO.write(image, "png", baos);
    baos.flush();
    byte[] bytes = baos.toByteArray();
    baos.close();
    return bytes;

}