1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| package test.capture;
import javax.imageio.ImageIO;
import java.awt.*;
import java.io.File;
import java.io.IOException;
public final class Main {
public static void main(final String... args) {
System.out.println(System.getProperty("java.version"));
screenCapture();
}
public static void screenCapture() {
try {
final var robot = new Robot();
final var env = GraphicsEnvironment.getLocalGraphicsEnvironment();
final var devices = env.getScreenDevices();
final int numberOfScreens = devices.length;
for (int index = 0 ; index < numberOfScreens ; index++) {
final var device = devices[index];
final var mode = device.getDisplayMode();
final var bounds = device.getDefaultConfiguration().getBounds();
System.out.println(device.getIDstring());
System.out.printf("Min : (%f, %f) ; Max : (%f, %f)%n", bounds.getMinX(), bounds.getMinY(), bounds.getMaxX(), bounds.getMaxY());
System.out.printf("Width : %d ; Height : %d%n", mode.getWidth(), mode.getHeight());
final var rectangle = new Rectangle((int)bounds.getMinX(), (int)bounds.getMinY(), (int)bounds.getWidth(), (int)bounds.getHeight());
final var screen = robot.createScreenCapture(rectangle);
ImageIO.write(screen, "jpg", new File("screenshot-%d.jpg".formatted(index)));
}
} catch (IOException | AWTException ex) {
ex.printStackTrace();
}
}
} |
Partager