Skip to content

Commit 453eb56

Browse files
committed
Add get/set auto property methods for stream properties
1 parent f8df932 commit 453eb56

2 files changed

Lines changed: 47 additions & 4 deletions

File tree

src/main/java/io/github/doblon8/openpnp/capture/CaptureStream.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,43 @@ public void setProperty(CaptureProperty property, int value) {
138138
}
139139
}
140140

141+
/**
142+
* Get the automatic flag of a camera/stream property (e.g. zoom, focus etc.).
143+
*
144+
* @param property the property to get the automatic flag for.
145+
* @return true if the property is set to automatic, false if it is set to manual.
146+
* @throws CaptureException if an error occurs while getting the auto property value.
147+
*/
148+
public boolean getAutoProperty(CaptureProperty property) {
149+
try (Arena arena = Arena.ofConfined()) {
150+
MemorySegment valuePointer = arena.allocate(ValueLayout.JAVA_INT);
151+
int result = Cap_getAutoProperty(context.getSegment(), id, property.value(), valuePointer);
152+
CaptureResult captureResult = CaptureResult.values()[result];
153+
return switch (captureResult) {
154+
case OK -> valuePointer.get(ValueLayout.JAVA_INT, 0) == 1;
155+
case PROPERTY_NOT_SUPPORTED -> throw new CaptureException("Property " + property + " is not supported by this stream.");
156+
default -> throw new CaptureException("Error getting auto property value for property " + property + ": context, stream are invalid.");
157+
};
158+
}
159+
}
160+
161+
/**
162+
* Set the automatic flag of a camera/stream property (e.g. zoom, focus etc.).
163+
*
164+
* @param property the property to set the automatic flag for.
165+
* @param enable true to set the property to automatic, false to set it to manual.
166+
* @throws CaptureException if an error occurs while setting the auto property value.
167+
*/
168+
public void setAutoProperty(CaptureProperty property, boolean enable) {
169+
int result = Cap_setAutoProperty(context.getSegment(), id, property.value(), enable ? 1 : 0);
170+
CaptureResult captureResult = CaptureResult.values()[result];
171+
switch (captureResult) {
172+
case OK -> {}
173+
case PROPERTY_NOT_SUPPORTED -> throw new CaptureException("Property " + property + " is not supported by this stream.");
174+
default -> throw new CaptureException("Error setting auto property value for property " + property + ": context, stream are invalid.");
175+
}
176+
}
177+
141178
/**
142179
* Check if a stream is open, i.e. is capturing data.
143180
*

src/test/java/Scratch.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,16 @@ void main() {
3131
stream.setProperty(CaptureProperty.ZOOM, 200);
3232
System.out.println("Zoom value after setting to 200: " + stream.getPropertyValue(CaptureProperty.ZOOM));
3333

34-
35-
var image = stream.capture();
36-
ImageIO.write(image, "png", new File("/tmp/image.png"));
37-
System.out.println("Captured image saved to /tmp/image.png");
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");
3844
} catch (InterruptedException e) {
3945
System.err.println("Interrupted while waiting for webcam: " + e.getMessage());
4046
}

0 commit comments

Comments
 (0)