Skip to content

Commit f8df932

Browse files
committed
Add get/set property methods for stream properties
1 parent 5f618e6 commit f8df932

2 files changed

Lines changed: 44 additions & 0 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
@@ -101,6 +101,43 @@ public PropertyLimits getPropertyLimits(CaptureProperty property) {
101101
}
102102
}
103103

104+
/**
105+
* Get the current value of a camera/stream property (e.g. zoom, exposure etc.).
106+
*
107+
* @param property the property to get the value for.
108+
* @return the current value of the property.
109+
* @throws CaptureException if an error occurs while getting the property value.
110+
*/
111+
public int getPropertyValue(CaptureProperty property) {
112+
try (Arena arena = Arena.ofConfined()) {
113+
MemorySegment valuePointer = arena.allocate(ValueLayout.JAVA_INT);
114+
int result = Cap_getProperty(context.getSegment(), id, property.value(), valuePointer);
115+
CaptureResult captureResult = CaptureResult.values()[result];
116+
return switch (captureResult) {
117+
case OK -> valuePointer.get(ValueLayout.JAVA_INT, 0);
118+
case PROPERTY_NOT_SUPPORTED -> throw new CaptureException("Property " + property + " is not supported by this stream.");
119+
default -> throw new CaptureException("Error getting property value for property " + property + ": context, stream are invalid or value is null.");
120+
};
121+
}
122+
}
123+
124+
/**
125+
* Set the value of a camera/stream property (e.g. zoom, exposure etc.).
126+
*
127+
* @param property the property to set the value for.
128+
* @param value the value to set for the property.
129+
* @throws CaptureException if an error occurs while setting the property value.
130+
*/
131+
public void setProperty(CaptureProperty property, int value) {
132+
int result = Cap_setProperty(context.getSegment(), id, property.value(), value);
133+
CaptureResult captureResult = CaptureResult.values()[result];
134+
switch (captureResult) {
135+
case OK -> {}
136+
case PROPERTY_NOT_SUPPORTED -> throw new CaptureException("Property " + property + " is not supported by this stream.");
137+
default -> throw new CaptureException("Error getting property limit for property " + property + ": context, stream are invalid.");
138+
}
139+
}
140+
104141
/**
105142
* Check if a stream is open, i.e. is capturing data.
106143
*

src/test/java/Scratch.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import io.github.doblon8.openpnp.capture.CaptureException;
2+
import io.github.doblon8.openpnp.capture.CaptureProperty;
23
import io.github.doblon8.openpnp.capture.LogLevel;
34
import io.github.doblon8.openpnp.capture.OpenPnpCapture;
45

@@ -25,6 +26,12 @@ void main() {
2526
System.out.println("Stream is open: " + stream.isOpen());
2627
TimeUnit.SECONDS.sleep(1); // wait a bit to get the webcam ready
2728

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+
2835
var image = stream.capture();
2936
ImageIO.write(image, "png", new File("/tmp/image.png"));
3037
System.out.println("Captured image saved to /tmp/image.png");

0 commit comments

Comments
 (0)