5

I'm trying to capture the screen using the below code on Windows 7

 Rectangle screenRect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
 BufferedImage capture = new Robot().createScreenCapture(screenRect);
 ImageIO.write(capture, "jpg", new File("C:/capture/ScreenShot.jpg"));

and this code is returning a black image, don't know why please help.

Imports are as below:

import java.awt.AWTException;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;

Thank you in advance.

Nomi Khurram
  • 101
  • 1
  • 11

4 Answers4

1

Check this, I think it will help you.

    public void screenCapture() {
    Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
    Rectangle rectangle = new Rectangle(dimension);
    BufferedImage screen = robot.createScreenCapture(rectangle);
    try {
        ImageIO.write(screen, "jpg", new File("screenshot.jpg"));
    } catch (IOException e) {

        e.printStackTrace();
    }

And after just use this void. :)

Brade
  • 17
  • 7
0

Had a similar issue on Ubuntu with Java 11 and Java 12, fixed by upgrading to Java 13.

AlexO
  • 1,311
  • 12
  • 18
0

In my case, the problem was in wildfly startup mode. When it started as a service, I got a black screen. When I started it manually through standalone.bat the problem went away.

-1

Maybe, there is another ScreenDevice.
Try

Robot r = new Robot(GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice());
BufferedImage capture = r.createScreenCapture(screenRect);
Saladan
  • 7
  • 2