Saturday, January 9, 2016

JavaFX example to capture screen/GUI nodes


JavaFX example to capture screen/GUI nodes:
package javafxcapturescreen;

import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Application;
import javafx.embed.swing.SwingFXUtils;
import javafx.event.ActionEvent;
import javafx.scene.Scene;
import javafx.scene.SnapshotParameters;
import javafx.scene.control.Button;
import javafx.scene.image.WritableImage;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javax.imageio.ImageIO;

/**
 *
 * @web http://java-buddy.blogspot.com/
 */
public class JavaFXCaptureScreen extends Application {
    
    @Override
    public void start(Stage primaryStage) {
        StackPane root = new StackPane();
        Scene scene = new Scene(root, 300, 250);
        
        Button btnCaptureScene = new Button();
        btnCaptureScene.setText("Capture scene");
        btnCaptureScene.setOnAction((ActionEvent event) -> {
            
            WritableImage writableImage = scene.snapshot(null);
            File file = new File("capturedScene.png");
            try {
                ImageIO.write(SwingFXUtils.fromFXImage(writableImage, null), "png", file);
                System.out.println("Captured: " + file.getAbsolutePath());
            } catch (IOException ex) {
                Logger.getLogger(JavaFXCaptureScreen.class.getName()).log(Level.SEVERE, null, ex);
            }
        });
        
        Button btnCaptureRoot = new Button();
        btnCaptureRoot.setText("Capture root");
        btnCaptureRoot.setOnAction((ActionEvent event) -> {
            WritableImage writableImage = root.snapshot(new SnapshotParameters(), null);

            File file = new File("capturedRoot.png");
            try {
                ImageIO.write(SwingFXUtils.fromFXImage(writableImage, null), "png", file);
                System.out.println("Captured: " + file.getAbsolutePath());
            } catch (IOException ex) {
                Logger.getLogger(JavaFXCaptureScreen.class.getName()).log(Level.SEVERE, null, ex);
            }
        });
        
        VBox vBox = new VBox();
        vBox.getChildren().addAll(btnCaptureScene, btnCaptureRoot);
        root.getChildren().add(vBox);
        
        primaryStage.setTitle("java-buddy.blogspot.com");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
    
}



capturedScene.png

capturedRoot.png

Thursday, January 7, 2016

Capture screen using java.awt.Robot

Java example to capture screen using java.awt.Robot:

package javarobotcapturescreen;

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;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;

/**
 *
 * @web http://java-buddy.blogspot.com/
 */
public class JavaRobotCaptureScreen {

    public static void main(String[] args) {
        try {
            Robot robot = new Robot();
            
            Toolkit myToolkit = Toolkit.getDefaultToolkit();
            Dimension screenSize = myToolkit.getScreenSize();
            
            Rectangle screen = new Rectangle(screenSize);
            
            BufferedImage screenFullImage = robot.createScreenCapture(screen);
            ImageIO.write(screenFullImage, "jpg", new File("screen.jpg"));
            
        } catch (AWTException | IOException ex) {
            Logger.getLogger(JavaRobotCaptureScreen.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
}




captured screen:


Get screen size and resolution, with java.awt.Toolkit

Java example to get screen size and resolution, with java.awt.Toolkit:

package javagetscreensize;

import java.awt.Dimension;
import java.awt.Toolkit;

/**
 *
 * @web http://java-buddy.blogspot.com/
 */
public class JavaGetScreenSize {

    public static void main(String[] args) {
        Toolkit myToolkit = Toolkit.getDefaultToolkit();
        Dimension screenSize = myToolkit.getScreenSize();
        int screenResolution = myToolkit.getScreenResolution();
        System.out.println("Screen size = " + screenSize.width + " x " + screenSize.height);
        System.out.println("Screen Resolution: " + screenResolution + " dots-per-inch");
    }
    
}