Skip to content

Commit fc3b14f

Browse files
committed
Simplify example in Scratch.java
1 parent 9286eac commit fc3b14f

1 file changed

Lines changed: 21 additions & 40 deletions

File tree

src/test/java/Scratch.java

Lines changed: 21 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,33 @@
1-
import io.github.doblon8.openpnp.capture.CaptureException;
2-
import io.github.doblon8.openpnp.capture.CaptureProperty;
3-
import io.github.doblon8.openpnp.capture.LogLevel;
4-
import io.github.doblon8.openpnp.capture.OpenPnpCapture;
1+
import io.github.doblon8.openpnp.capture.*;
52

63
import javax.imageio.ImageIO;
7-
import java.awt.*;
84

9-
import static io.github.doblon8.openpnp.capture.OpenPnpCapture.installCustomLogFunction;
10-
import static io.github.doblon8.openpnp.capture.OpenPnpCapture.setLogLevel;
5+
import static io.github.doblon8.openpnp.capture.OpenPnpCapture.*;
116

12-
void main() {
13-
var version = OpenPnpCapture.getLibraryVersion();
14-
System.out.println("OpenPnP Capture Library Version: " + version);
7+
void main() throws Exception {
8+
System.out.println("openpnp-capture version: " + OpenPnpCapture.getLibraryVersion());
159

16-
setLogLevel(LogLevel.DEBUG);
17-
installCustomLogFunction((logLevel, message) -> System.out.println("Custom Log [" + logLevel + "]: " + message));
10+
setLogLevel(LogLevel.INFO);
11+
installCustomLogFunction((logLevel, message) -> System.out.println("Log [" + logLevel + "]: " + message));
1812

1913
try (var capture = new OpenPnpCapture()) {
20-
// No checking if devices and format are available, just get the first ones for testing
21-
var device = capture.getDevices().getFirst();
22-
var format = device.getFormats().getFirst();
23-
System.out.println("Using device '" + device.getName() + "' with format " + format);
14+
var device = capture.getDevices().stream()
15+
.findFirst()
16+
.orElseThrow(() -> new CaptureException("No capture devices found."));
17+
18+
var format = device.getFormats().stream()
19+
.findFirst()
20+
.orElseThrow(() -> new CaptureException("No formats found."));
2421

2522
try (var stream = device.openStream(format)) {
26-
System.out.println("Stream is open: " + stream.isOpen());
27-
TimeUnit.SECONDS.sleep(1); // wait a bit to get the webcam ready
28-
29-
System.out.println("Zoom property limits: " + stream.getPropertyLimits(CaptureProperty.ZOOM));
30-
System.out.println("Current zoom value: " + stream.getPropertyValue(CaptureProperty.ZOOM));
31-
stream.setProperty(CaptureProperty.ZOOM, 200);
32-
System.out.println("Zoom value after setting to 200: " + stream.getPropertyValue(CaptureProperty.ZOOM));
33-
34-
var image1 = stream.capture();
35-
ImageIO.write(image1, "png", new File("/tmp/image_manual_zoom.png"));
36-
System.out.println("Captured image with manual zoom saved to /tmp/image_manual_zoom.png");
37-
38-
System.out.println("Current auto focus value: " + stream.getAutoProperty(CaptureProperty.FOCUS));
39-
stream.setAutoProperty(CaptureProperty.FOCUS, false);
40-
System.out.println("Auto focus after setting to false: " + stream.getAutoProperty(CaptureProperty.FOCUS));
41-
var image2 = stream.capture();
42-
ImageIO.write(image2, "png", new File("/tmp/image_auto_focus_disabled.png"));
43-
System.out.println("Captured image with auto focus disabled saved to /tmp/image_auto_focus_disabled.png");
44-
} catch (InterruptedException e) {
45-
System.err.println("Interrupted while waiting for webcam: " + e.getMessage());
23+
TimeUnit.SECONDS.sleep(1); // let the camera initialize
24+
25+
var zoomLimits = stream.getPropertyLimits(CaptureProperty.ZOOM);
26+
System.out.println("Zoom limits: " + zoomLimits);
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");
4631
}
47-
} catch (CaptureException e) {
48-
System.err.println("Error using CaptureContext: " + e.getMessage());
49-
} catch (IOException e) {
50-
System.err.println("Error saving image: " + e.getMessage());
5132
}
5233
}

0 commit comments

Comments
 (0)