Skip to content

Commit 49f9bb4

Browse files
authored
Create ScreenPrinterWayland.java
This file is to contain screen capture functionality for Wayland Linux systems utilizing OpenJFX. The output will be compatible with OpenCV, which is used for all other systems. This is intended to minimize changes in the image processing workflow for OCR.
1 parent e0af3cb commit 49f9bb4

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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

Comments
 (0)