I have an application deployed online and it only works as expected on Windows OS as of now. I am trying to make the app that will also work on Mac OS as well but the problem is that all maximized applications like Finder or the IDE itself wasn't included in the capture, it just captured the background. (Please see this screenshot taken from the code below)
I want the opened or maximized applications to be included in the screenshot but all those aren't working using the code below.
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.List;
public class ScreenCapture extends Application {
@Override
public void start(Stage primaryStage) {
captureScreenshot();
}
private void captureScreenshot() {
List<Screen> screens = Screen.getScreens();
for (int i = 0; i < screens.size(); i++) {
Screen screen = screens.get(i);
Rectangle2D bounds = screen.getBounds();
Robot robot = new Robot();
WritableImage screenshot = robot.getScreenCapture(null, bounds);
BufferedImage image = javafx.embed.swing.SwingFXUtils.fromFXImage(screenshot, null);
try {
ImageIO.write(image, "png", new File("screenshot" + i + ".png"));
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
}
}
public static void main(String[] args) {
launch(args);
}
}
I tried looking for answers on Google, Youtube and even chatgpt but i cant find any solutions.
Starting with macOS 10.15 Catalina, there is a privacy and security permission that you need to capture the screen on a Mac. If an app isn't permitted to record the screen, it will receive the desktop instead.
Usually the first use of a screen recording API will present a dialog to the user, but I don't know how this is implemented in JavaFX - it could be that the request is silently denied. In this case - or if a user denied the request - you must direct them to performing the appropriate action. Be aware that your app will have to be restarted before the recording permission comes into effect.