Search in sources :

Example 1 with Robot

use of javafx.scene.robot.Robot in project qupath by qupath.

the class GuiTools method makeSnapshotFX.

/**
 * Make a snapshot as a JavaFX {@link Image}.
 * @param qupath
 * @param viewer the viewer to use (or null to use the current viewer)
 * @param type
 * @return
 */
public static WritableImage makeSnapshotFX(final QuPathGUI qupath, QuPathViewer viewer, final GuiTools.SnapshotType type) {
    if (!Platform.isFxApplicationThread()) {
        var temp = viewer;
        return callOnApplicationThread(() -> makeSnapshotFX(qupath, temp, type));
    }
    Stage stage = qupath.getStage();
    Scene scene = stage.getScene();
    switch(type) {
        case VIEWER:
            if (viewer == null)
                viewer = qupath.getViewer();
            // Temporarily remove the selected border color while copying
            Color borderColor = viewer.getBorderColor();
            try {
                qupath.getViewer().setBorderColor(null);
                return viewer.getView().snapshot(null, null);
            } finally {
                viewer.setBorderColor(borderColor);
            }
        case MAIN_SCENE:
            return scene.snapshot(null);
        case MAIN_WINDOW_SCREENSHOT:
            double x = scene.getX() + stage.getX();
            double y = scene.getY() + stage.getY();
            double width = scene.getWidth();
            double height = scene.getHeight();
            try {
                // For reasons I do not understand, this occasionally throws an ArrayIndexOutOfBoundsException
                return new Robot().getScreenCapture(null, x, y, width, height, false);
            } catch (Exception e) {
                logger.error("Unable to make main window screenshot, will resort to trying to crop a full screenshot instead", e);
                var img2 = makeSnapshotFX(qupath, viewer, GuiTools.SnapshotType.FULL_SCREENSHOT);
                return new WritableImage(img2.getPixelReader(), (int) x, (int) y, (int) width, (int) height);
            }
        case FULL_SCREENSHOT:
            var screen = Screen.getPrimary();
            var bounds = screen.getBounds();
            return new Robot().getScreenCapture(null, bounds.getMinX(), bounds.getMinY(), bounds.getWidth(), bounds.getHeight());
        default:
            throw new IllegalArgumentException("Unknown snapshot type " + type);
    }
}
Also used : WritableImage(javafx.scene.image.WritableImage) Color(javafx.scene.paint.Color) Stage(javafx.stage.Stage) Scene(javafx.scene.Scene) Robot(javafx.scene.robot.Robot) IOException(java.io.IOException)

Aggregations

IOException (java.io.IOException)1 Scene (javafx.scene.Scene)1 WritableImage (javafx.scene.image.WritableImage)1 Color (javafx.scene.paint.Color)1 Robot (javafx.scene.robot.Robot)1 Stage (javafx.stage.Stage)1