brevleq
brevleq

Reputation: 2141

AWT Robot - only taking screenshot of current program on MacOS

I'm creating a simple test program to take screenshots of the entire screen, after some research, I created the following code:

public class PrintScreenCatcher {

    public String capture(){
        try {
            Rectangle rectangle = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
            Robot robot = new Robot();
            BufferedImage screenShot = robot.createScreenCapture(rectangle);
            File file=createTempFilePath();
            ImageIO.write(screenShot, "jpg", file);
            return file.getAbsolutePath();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    private File createTempFilePath() throws IOException {
        DateTimeFormatter formatter=DateTimeFormatter.ofPattern("yyyyMMddHHmmssS");
        return File.createTempFile("screen-"+LocalDateTime.now().format(formatter),".jpg");
    }
}

After run this program, I was expecting a image with my entire screen (MacOS menu, Intellij, etc):

native print screen

Instead I got a image only containing my desktop's background:

java print screen

I think robot is trying to take a screenshot only of my program. What should I do to take a screenshot of the opened screens?

Upvotes: 0

Views: 976

Answers (2)

brevleq
brevleq

Reputation: 2141

The problem was MacOS permission. According with this answer, the program needs permission to record the screen. I simply added the access to IntelliJ in System Preferences -> Security and Privacy -> Privacy -> Screen Recording and it worked.

Upvotes: 2

Gilbert Le Blanc
Gilbert Le Blanc

Reputation: 51445

I added a main method and captured the following image on Windows 10 using Java 14.0.2 with the code compiled to a Java 8 standard.

Screenshot

Here's the code I ran with. If you still get a blank screen on Mac, well I can't help you.

import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

import javax.imageio.ImageIO;

public class PrintScreenCatcher {

    public static void main(String[] args) {
        System.out.println(new PrintScreenCatcher().capture());
    }

    public String capture() {
        try {
            Rectangle rectangle = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
            Robot robot = new Robot();
            BufferedImage screenShot = robot.createScreenCapture(rectangle);
            File file = createTempFilePath();
            ImageIO.write(screenShot, "jpg", file);
            return file.getAbsolutePath();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    private File createTempFilePath() throws IOException {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmssS");
        return File.createTempFile("screen-" + LocalDateTime.now().format(formatter), ".jpg");
    }

}

Upvotes: -1

Related Questions