Skip to content

Commit d2d30a3

Browse files
committed
Capture and save webcam image to file in Scratch.java
1 parent a113333 commit d2d30a3

1 file changed

Lines changed: 12 additions & 0 deletions

File tree

src/test/java/Scratch.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
import io.github.doblon8.openpnp.capture.LogLevel;
33
import io.github.doblon8.openpnp.capture.OpenPnpCapture;
44

5+
import javax.imageio.ImageIO;
6+
import java.awt.*;
7+
58
import static io.github.doblon8.openpnp.capture.OpenPnpCapture.installCustomLogFunction;
69
import static io.github.doblon8.openpnp.capture.OpenPnpCapture.setLogLevel;
710

@@ -20,8 +23,17 @@ void main() {
2023

2124
try (var stream = device.openStream(format)) {
2225
System.out.println("Stream is open: " + stream.isOpen());
26+
TimeUnit.SECONDS.sleep(1); // wait a bit to get the webcam ready
27+
28+
var image = stream.capture();
29+
ImageIO.write(image, "png", new File("/tmp/image.png"));
30+
System.out.println("Captured image saved to /tmp/image.png");
31+
} catch (InterruptedException e) {
32+
System.err.println("Interrupted while waiting for webcam: " + e.getMessage());
2333
}
2434
} catch (CaptureException e) {
2535
System.err.println("Error using CaptureContext: " + e.getMessage());
36+
} catch (IOException e) {
37+
System.err.println("Error saving image: " + e.getMessage());
2638
}
2739
}

0 commit comments

Comments
 (0)