|
| 1 | +import javafx.application.Application; |
| 2 | +import javafx.geometry.Rectangle2D; |
| 3 | +import javafx.scene.SnapshotParameters; |
| 4 | +import javafx.scene.image.Image; |
| 5 | +import javafx.scene.image.PixelReader; |
| 6 | +import javafx.scene.image.WritableImage; |
| 7 | +import javafx.stage.Screen; |
| 8 | +import javafx.stage.Stage; |
| 9 | + |
| 10 | +import org.opencv.core.Mat; |
| 11 | +import org.opencv.imgcodecs.Imgcodecs; |
| 12 | + |
| 13 | +public class WaylandScreenCapture { |
| 14 | + |
| 15 | + // Assuming you have OpenCV setup correctly in your project |
| 16 | + |
| 17 | + public static Mat captureScreenshot() { |
| 18 | + // Get primary screen bounds |
| 19 | + Rectangle2D screenBounds = Screen.getPrimary().getBounds(); |
| 20 | + |
| 21 | + // Create a SnapshotParameters object |
| 22 | + SnapshotParameters params = new SnapshotParameters(); |
| 23 | + params.setFill(javafx.scene.paint.Color.TRANSPARENT); // Transparent background for accurate capture |
| 24 | + |
| 25 | + // Capture the screenshot as a WritableImage |
| 26 | + WritableImage image = Screen.getPrimary().snapshot(params, null); |
| 27 | + |
| 28 | + // Get pixel reader from the captured image |
| 29 | + PixelReader reader = image.getPixelReader(); |
| 30 | + |
| 31 | + // Create OpenCV Mat to store captured pixels |
| 32 | + Mat mat = new Mat((int) screenBounds.getHeight(), (int) screenBounds.getWidth(), org.opencv.core.CvType.CV_8UC3); |
| 33 | + |
| 34 | + // Iterate over the pixels and copy them into the OpenCV Mat |
| 35 | + for (int y = 0; y < image.getHeight(); y++) { |
| 36 | + for (int x = 0; x < image.getWidth(); x++) { |
| 37 | + javafx.scene.paint.Color color = reader.getColor(x, y); |
| 38 | + |
| 39 | + // Convert JavaFX Color to OpenCV BGR format |
| 40 | + mat.put(y, x, new double[]{color.getBlue() * 255, color.getGreen() * 255, color.getRed() * 255}); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + return mat; |
| 45 | + } |
0 commit comments