mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-24 01:31:46 +00:00
Rename from cameraserver to cscore.
This is primarily to avoid header naming conflicts with wpilib.
This commit is contained in:
169
java/src/edu/wpi/cscore/CameraServerJNI.java
Normal file
169
java/src/edu/wpi/cscore/CameraServerJNI.java
Normal file
@@ -0,0 +1,169 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) FIRST 2016. All Rights Reserved. */
|
||||
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
||||
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
||||
/* the project. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
package edu.wpi.cscore;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
public class CameraServerJNI {
|
||||
static boolean libraryLoaded = false;
|
||||
static File jniLibrary = null;
|
||||
static {
|
||||
if (!libraryLoaded) {
|
||||
try {
|
||||
System.loadLibrary("cameraserver");
|
||||
} catch (UnsatisfiedLinkError e) {
|
||||
try {
|
||||
String osname = System.getProperty("os.name");
|
||||
String resname;
|
||||
if (osname.startsWith("Windows"))
|
||||
resname = "/Windows/" + System.getProperty("os.arch") + "/";
|
||||
else
|
||||
resname = "/" + osname + "/" + System.getProperty("os.arch") + "/";
|
||||
System.out.println("platform: " + resname);
|
||||
if (osname.startsWith("Windows"))
|
||||
resname += "cameraserver.dll";
|
||||
else if (osname.startsWith("Mac"))
|
||||
resname += "libcameraserver.dylib";
|
||||
else
|
||||
resname += "libcameraserver.so";
|
||||
InputStream is = CameraServerJNI.class.getResourceAsStream(resname);
|
||||
if (is != null) {
|
||||
// create temporary file
|
||||
if (System.getProperty("os.name").startsWith("Windows"))
|
||||
jniLibrary = File.createTempFile("CameraServerJNI", ".dll");
|
||||
else if (System.getProperty("os.name").startsWith("Mac"))
|
||||
jniLibrary = File.createTempFile("libCameraServerJNI", ".dylib");
|
||||
else
|
||||
jniLibrary = File.createTempFile("libCameraServerJNI", ".so");
|
||||
// flag for delete on exit
|
||||
jniLibrary.deleteOnExit();
|
||||
OutputStream os = new FileOutputStream(jniLibrary);
|
||||
|
||||
byte[] buffer = new byte[1024];
|
||||
int readBytes;
|
||||
try {
|
||||
while ((readBytes = is.read(buffer)) != -1) {
|
||||
os.write(buffer, 0, readBytes);
|
||||
}
|
||||
} finally {
|
||||
os.close();
|
||||
is.close();
|
||||
}
|
||||
System.load(jniLibrary.getAbsolutePath());
|
||||
} else {
|
||||
System.loadLibrary("cameraserver");
|
||||
}
|
||||
} catch (IOException ex) {
|
||||
ex.printStackTrace();
|
||||
System.exit(1);
|
||||
}
|
||||
}
|
||||
libraryLoaded = true;
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// Property Functions
|
||||
//
|
||||
public static native int getPropertyType(int property);
|
||||
public static native String getPropertyName(int property);
|
||||
public static native int getProperty(int property);
|
||||
public static native void setProperty(int property, int value);
|
||||
public static native int getPropertyMin(int property);
|
||||
public static native int getPropertyMax(int property);
|
||||
public static native int getPropertyStep(int property);
|
||||
public static native int getPropertyDefault(int property);
|
||||
public static native String getStringProperty(int property);
|
||||
public static native void setStringProperty(int property, String value);
|
||||
public static native String[] getEnumPropertyChoices(int property);
|
||||
|
||||
//
|
||||
// Source Creation Functions
|
||||
//
|
||||
public static native int createUSBCameraDev(String name, int dev);
|
||||
public static native int createUSBCameraPath(String name, String path);
|
||||
public static native int createHTTPCamera(String name, String url);
|
||||
public static native int createCvSource(String name, int pixelFormat, int width, int height, int fps);
|
||||
|
||||
//
|
||||
// Source Functions
|
||||
//
|
||||
public static native String getSourceName(int source);
|
||||
public static native String getSourceDescription(int source);
|
||||
public static native long getSourceLastFrameTime(int source);
|
||||
public static native boolean isSourceConnected(int source);
|
||||
public static native int getSourceProperty(int source, String name);
|
||||
public static native int[] enumerateSourceProperties(int source);
|
||||
public static native VideoMode getSourceVideoMode(int source);
|
||||
public static native boolean setSourceVideoMode(int source, int pixelFormat, int width, int height, int fps);
|
||||
public static native boolean setSourcePixelFormat(int source, int pixelFormat);
|
||||
public static native boolean setSourceResolution(int source, int width, int height);
|
||||
public static native boolean setSourceFPS(int source, int fps);
|
||||
public static native VideoMode[] enumerateSourceVideoModes(int source);
|
||||
public static native int copySource(int source);
|
||||
public static native void releaseSource(int source);
|
||||
|
||||
//
|
||||
// OpenCV Source Functions
|
||||
//
|
||||
public static native void putSourceFrame(int source, long imageNativeObj);
|
||||
public static native void notifySourceError(int source, String msg);
|
||||
public static native void setSourceConnected(int source, boolean connected);
|
||||
public static native void setSourceDescription(int source, String description);
|
||||
public static native int createSourceProperty(int source, String name, int type, int minimum, int maximum, int step, int defaultValue, int value);
|
||||
public static native void setSourceEnumPropertyChoices(int source, int property, String[] choices);
|
||||
|
||||
//
|
||||
// Sink Creation Functions
|
||||
//
|
||||
public static native int createMJPEGServer(String name, String listenAddress, int port);
|
||||
public static native int createCvSink(String name);
|
||||
//public static native int createCvSinkCallback(String name,
|
||||
// void (*processFrame)(long time));
|
||||
|
||||
//
|
||||
// Sink Functions
|
||||
//
|
||||
public static native String getSinkName(int sink);
|
||||
public static native String getSinkDescription(int sink);
|
||||
public static native void setSinkSource(int sink, int source);
|
||||
public static native int getSinkSourceProperty(int sink, String name);
|
||||
public static native int getSinkSource(int sink);
|
||||
public static native int copySink(int sink);
|
||||
public static native void releaseSink(int sink);
|
||||
|
||||
//
|
||||
// OpenCV Sink Functions
|
||||
//
|
||||
public static native void setSinkDescription(int sink, String description);
|
||||
public static native long grabSinkFrame(int sink, long imageNativeObj);
|
||||
public static native String getSinkError(int sink);
|
||||
public static native void setSinkEnabled(int sink, boolean enabled);
|
||||
|
||||
//
|
||||
// Listener Functions
|
||||
//
|
||||
public static native int addListener(VideoListenerFunction listener,
|
||||
int eventMask, boolean immediateNotify);
|
||||
|
||||
public static native void removeListener(int handle);
|
||||
|
||||
//
|
||||
// Utility Functions
|
||||
//
|
||||
public static native USBCameraInfo[] enumerateUSBCameras();
|
||||
|
||||
public static native int[] enumerateSources();
|
||||
|
||||
public static native int[] enumerateSinks();
|
||||
}
|
||||
61
java/src/edu/wpi/cscore/CvSink.java
Normal file
61
java/src/edu/wpi/cscore/CvSink.java
Normal file
@@ -0,0 +1,61 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) FIRST 2016. All Rights Reserved. */
|
||||
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
||||
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
||||
/* the project. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
package edu.wpi.cscore;
|
||||
|
||||
import org.opencv.core.Mat;
|
||||
|
||||
/// A sink for user code to accept video frames as OpenCV images.
|
||||
public class CvSink extends VideoSink {
|
||||
/// Create a sink for accepting OpenCV images.
|
||||
/// WaitForFrame() must be called on the created sink to get each new
|
||||
/// image.
|
||||
/// @param name Source name (arbitrary unique identifier)
|
||||
public CvSink(String name) {
|
||||
super(CameraServerJNI.createCvSink(name));
|
||||
}
|
||||
|
||||
/// Create a sink for accepting OpenCV images in a separate thread.
|
||||
/// A thread will be created that calls WaitForFrame() and calls the
|
||||
/// processFrame() callback each time a new frame arrives.
|
||||
/// @param name Source name (arbitrary unique identifier)
|
||||
/// @param processFrame Frame processing function; will be called with a
|
||||
/// time=0 if an error occurred. processFrame should call GetImage()
|
||||
/// or GetError() as needed, but should not call (except in very
|
||||
/// unusual circumstances) WaitForImage().
|
||||
//public CvSink(llvm::StringRef name,
|
||||
// std::function<void(uint64_t time)> processFrame) {
|
||||
// super(CameraServerJNI.createCvSinkCallback(name, processFrame));
|
||||
//}
|
||||
|
||||
/// Set sink description.
|
||||
/// @param description Description
|
||||
public void setDescription(String description) {
|
||||
CameraServerJNI.setSinkDescription(m_handle, description);
|
||||
}
|
||||
|
||||
/// Wait for the next frame and get the image.
|
||||
/// @return Frame time, or 0 on error (call GetError() to obtain the error
|
||||
/// message);
|
||||
public long grabFrame(Mat image) {
|
||||
return CameraServerJNI.grabSinkFrame(m_handle, image.nativeObj);
|
||||
}
|
||||
|
||||
/// Get error string. Call this if WaitForFrame() returns 0 to determine
|
||||
/// what the error is.
|
||||
public String getError() {
|
||||
return CameraServerJNI.getSinkError(m_handle);
|
||||
}
|
||||
|
||||
/// Enable or disable getting new frames.
|
||||
/// Disabling will cause processFrame (for callback-based CvSinks) to not
|
||||
/// be called and WaitForFrame() to not return. This can be used to save
|
||||
/// processor resources when frames are not needed.
|
||||
public void setEnabled(boolean enabled) {
|
||||
CameraServerJNI.setSinkEnabled(m_handle, enabled);
|
||||
}
|
||||
}
|
||||
90
java/src/edu/wpi/cscore/CvSource.java
Normal file
90
java/src/edu/wpi/cscore/CvSource.java
Normal file
@@ -0,0 +1,90 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) FIRST 2016. All Rights Reserved. */
|
||||
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
||||
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
||||
/* the project. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
package edu.wpi.cscore;
|
||||
|
||||
import org.opencv.core.Mat;
|
||||
|
||||
/// A source that represents a video camera.
|
||||
public class CvSource extends VideoSource {
|
||||
/// Create an OpenCV source.
|
||||
/// @param name Source name (arbitrary unique identifier)
|
||||
/// @param mode Video mode being generated
|
||||
public CvSource(String name, VideoMode mode) {
|
||||
super(CameraServerJNI.createCvSource(name, mode.pixelFormat.getValue(), mode.width, mode.height, mode.fps));
|
||||
}
|
||||
|
||||
/// Create an OpenCV source.
|
||||
/// @param name Source name (arbitrary unique identifier)
|
||||
/// @param pixelFormat Pixel format
|
||||
/// @param width width
|
||||
/// @param height height
|
||||
/// @param fps fps
|
||||
public CvSource(String name, VideoMode.PixelFormat pixelFormat, int width, int height, int fps) {
|
||||
super(CameraServerJNI.createCvSource(name, pixelFormat.getValue(), width, height, fps));
|
||||
}
|
||||
|
||||
/// Put an OpenCV image and notify sinks.
|
||||
/// @param image OpenCV image
|
||||
public void putFrame(Mat image) {
|
||||
CameraServerJNI.putSourceFrame(m_handle, image.nativeObj);
|
||||
}
|
||||
|
||||
/// Signal sinks that an error has occurred. This should be called instead
|
||||
/// of NotifyFrame when an error occurs.
|
||||
public void notifyError(String msg) {
|
||||
CameraServerJNI.notifySourceError(m_handle, msg);
|
||||
}
|
||||
|
||||
/// Set source connection status. Defaults to true.
|
||||
/// @param connected True for connected, false for disconnected
|
||||
public void setConnected(boolean connected) {
|
||||
CameraServerJNI.setSourceConnected(m_handle, connected);
|
||||
}
|
||||
|
||||
/// Set source description.
|
||||
/// @param description Description
|
||||
public void setDescription(String description) {
|
||||
CameraServerJNI.setSourceDescription(m_handle, description);
|
||||
}
|
||||
|
||||
/// Create a property.
|
||||
/// @param name Property name
|
||||
/// @param type Property type
|
||||
/// @param minimum Minimum value
|
||||
/// @param maximum Maximum value
|
||||
/// @param step Step value
|
||||
/// @param defaultValue Default value
|
||||
/// @param value Current value
|
||||
/// @return Property
|
||||
public VideoProperty createProperty(String name, VideoProperty.Type type, int minimum, int maximum, int step, int defaultValue, int value) {
|
||||
return new VideoProperty(
|
||||
CameraServerJNI.createSourceProperty(m_handle, name, type.getValue(), minimum, maximum, step, defaultValue, value));
|
||||
}
|
||||
|
||||
/// Create a property with a change callback.
|
||||
/// @param name Property name
|
||||
/// @param type Property type
|
||||
/// @param minimum Minimum value
|
||||
/// @param maximum Maximum value
|
||||
/// @param step Step value
|
||||
/// @param defaultValue Default value
|
||||
/// @param value Current value
|
||||
/// @param onChange Callback to call when the property value changes
|
||||
/// @return Property
|
||||
//public VideoProperty createProperty(
|
||||
// String name, VideoProperty.Type type, int minimum, int maximum, int step, int defaultValue, int value,
|
||||
// std::function<void(VideoProperty property)>
|
||||
// onChange);
|
||||
|
||||
/// Configure enum property choices.
|
||||
/// @param property Property
|
||||
/// @param choices Choices
|
||||
public void SetEnumPropertyChoices(VideoProperty property, String[] choices) {
|
||||
CameraServerJNI.setSourceEnumPropertyChoices(m_handle, property.m_handle, choices);
|
||||
}
|
||||
}
|
||||
18
java/src/edu/wpi/cscore/HTTPCamera.java
Normal file
18
java/src/edu/wpi/cscore/HTTPCamera.java
Normal file
@@ -0,0 +1,18 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) FIRST 2016. All Rights Reserved. */
|
||||
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
||||
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
||||
/* the project. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
package edu.wpi.cscore;
|
||||
|
||||
/// A source that represents a MJPEG-over-HTTP (IP) camera.
|
||||
public class HTTPCamera extends VideoSource {
|
||||
/// Create a source for a MJPEG-over-HTTP (IP) camera.
|
||||
/// @param name Source name (arbitrary unique identifier)
|
||||
/// @param url Camera URL (e.g. "http://10.x.y.11/video/stream.mjpg")
|
||||
public HTTPCamera(String name, String url) {
|
||||
super(CameraServerJNI.createHTTPCamera(name, url));
|
||||
}
|
||||
}
|
||||
26
java/src/edu/wpi/cscore/MJPEGServer.java
Normal file
26
java/src/edu/wpi/cscore/MJPEGServer.java
Normal file
@@ -0,0 +1,26 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) FIRST 2016. All Rights Reserved. */
|
||||
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
||||
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
||||
/* the project. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
package edu.wpi.cscore;
|
||||
|
||||
/// A sink that acts as a MJPEG-over-HTTP network server.
|
||||
public class MJPEGServer extends VideoSink {
|
||||
/// Create a MJPEG-over-HTTP server sink.
|
||||
/// @param name Sink name (arbitrary unique identifier)
|
||||
/// @param listenAddress TCP listen address (empty string for all addresses)
|
||||
/// @param port TCP port number
|
||||
public MJPEGServer(String name, String listenAddress, int port) {
|
||||
super(CameraServerJNI.createMJPEGServer(name, listenAddress, port));
|
||||
}
|
||||
|
||||
/// Create a MJPEG-over-HTTP server sink.
|
||||
/// @param name Sink name (arbitrary unique identifier)
|
||||
/// @param port TCP port number
|
||||
public MJPEGServer(String name, int port) {
|
||||
this(name, "", port);
|
||||
}
|
||||
}
|
||||
31
java/src/edu/wpi/cscore/USBCamera.java
Normal file
31
java/src/edu/wpi/cscore/USBCamera.java
Normal file
@@ -0,0 +1,31 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) FIRST 2016. All Rights Reserved. */
|
||||
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
||||
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
||||
/* the project. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
package edu.wpi.cscore;
|
||||
|
||||
/// A source that represents a USB camera.
|
||||
public class USBCamera extends VideoSource {
|
||||
/// Create a source for a USB camera based on device number.
|
||||
/// @param name Source name (arbitrary unique identifier)
|
||||
/// @param dev Device number (e.g. 0 for /dev/video0)
|
||||
public USBCamera(String name, int dev) {
|
||||
super(CameraServerJNI.createUSBCameraDev(name, dev));
|
||||
}
|
||||
|
||||
/// Create a source for a USB camera based on device path.
|
||||
/// @param name Source name (arbitrary unique identifier)
|
||||
/// @param path Path to device (e.g. "/dev/video0" on Linux)
|
||||
public USBCamera(String name, String path) {
|
||||
super(CameraServerJNI.createUSBCameraPath(name, path));
|
||||
}
|
||||
|
||||
/// Enumerate USB cameras on the local system.
|
||||
/// @return Vector of USB camera information (one for each camera)
|
||||
public static USBCameraInfo[] enumerateUSBCameras() {
|
||||
return CameraServerJNI.enumerateUSBCameras();
|
||||
}
|
||||
}
|
||||
24
java/src/edu/wpi/cscore/USBCameraInfo.java
Normal file
24
java/src/edu/wpi/cscore/USBCameraInfo.java
Normal file
@@ -0,0 +1,24 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) FIRST 2016. All Rights Reserved. */
|
||||
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
||||
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
||||
/* the project. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
package edu.wpi.cscore;
|
||||
|
||||
/// USB camera information
|
||||
public class USBCameraInfo {
|
||||
public USBCameraInfo(int dev, String path, String name) {
|
||||
this.dev = dev;
|
||||
this.path = path;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
/// Device number (e.g. N in '/dev/videoN' on Linux)
|
||||
public int dev;
|
||||
/// Path to device if available (e.g. '/dev/video0' on Linux)
|
||||
public String path;
|
||||
/// Vendor/model name of the camera as provided by the USB driver
|
||||
public String name;
|
||||
}
|
||||
83
java/src/edu/wpi/cscore/VideoEvent.java
Normal file
83
java/src/edu/wpi/cscore/VideoEvent.java
Normal file
@@ -0,0 +1,83 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) FIRST 2016. All Rights Reserved. */
|
||||
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
||||
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
||||
/* the project. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
package edu.wpi.cscore;
|
||||
|
||||
/// Video event
|
||||
public class VideoEvent {
|
||||
public enum Type {
|
||||
kSourceCreated(0x0001),
|
||||
kSourceDestroyed(0x0002),
|
||||
kSourceConnected(0x0004),
|
||||
kSourceDisconnected(0x0008),
|
||||
kSourceVideoModesUpdated(0x0010),
|
||||
kSourceVideoModeChanged(0x0020),
|
||||
kSinkCreated(0x0100),
|
||||
kSinkDestroyed(0x0200),
|
||||
kSinkEnabled(0x0400),
|
||||
kSinkDisabled(0x0800),
|
||||
kSourcePropertyCreated(0x1000),
|
||||
kSourcePropertyValueUpdated(0x2000),
|
||||
kSourcePropertyChoicesUpdated(0x4000);
|
||||
|
||||
private int value;
|
||||
|
||||
private Type(int value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public int getValue() {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
private static final Type[] m_typeValues = Type.values();
|
||||
|
||||
VideoEvent(int type, int source, int sink, String name, int pixelFormat,
|
||||
int width, int height, int fps, int property, int propertyType,
|
||||
int value, String valueStr) {
|
||||
this.type = m_typeValues[type];
|
||||
this.sourceHandle = source;
|
||||
this.sinkHandle = sink;
|
||||
this.name = name;
|
||||
this.mode = new VideoMode(pixelFormat, width, height, fps);
|
||||
this.propertyHandle = property;
|
||||
this.propertyType = VideoProperty.m_typeValues[propertyType];
|
||||
this.value = value;
|
||||
this.valueStr = valueStr;
|
||||
}
|
||||
|
||||
public Type type;
|
||||
|
||||
// Valid for kSource* and kSink* respectively
|
||||
private int sourceHandle;
|
||||
private int sinkHandle;
|
||||
|
||||
// Source/sink name
|
||||
public String name;
|
||||
|
||||
// Fields for kSourceVideoModeChanged event
|
||||
public VideoMode mode;
|
||||
|
||||
// Fields for kSourceProperty* events
|
||||
private int propertyHandle;
|
||||
public VideoProperty.Type propertyType;
|
||||
public int value;
|
||||
public String valueStr;
|
||||
|
||||
public VideoSource getSource() {
|
||||
return new VideoSource(CameraServerJNI.copySource(sourceHandle));
|
||||
}
|
||||
|
||||
public VideoSink getSink() {
|
||||
return new VideoSink(CameraServerJNI.copySink(sinkHandle));
|
||||
}
|
||||
|
||||
public VideoProperty getProperty() {
|
||||
return new VideoProperty(propertyHandle, propertyType);
|
||||
}
|
||||
|
||||
}
|
||||
35
java/src/edu/wpi/cscore/VideoListener.java
Normal file
35
java/src/edu/wpi/cscore/VideoListener.java
Normal file
@@ -0,0 +1,35 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) FIRST 2016. All Rights Reserved. */
|
||||
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
||||
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
||||
/* the project. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
package edu.wpi.cscore;
|
||||
|
||||
/// An event listener. This calls back to a desigated callback function when
|
||||
/// an event matching the specified mask is generated by the library.
|
||||
public class VideoListener {
|
||||
/// Create an event listener.
|
||||
/// @param listener Listener function
|
||||
/// @param eventMask Bitmask of VideoEvent.Type values
|
||||
/// @param immediateNotify Whether callback should be immediately called with
|
||||
/// a representative set of events for the current library state.
|
||||
public VideoListener(VideoListenerFunction listener, int eventMask,
|
||||
boolean immediateNotify) {
|
||||
m_handle = CameraServerJNI.addListener(listener, eventMask, immediateNotify);
|
||||
}
|
||||
|
||||
public synchronized void free() {
|
||||
if (m_handle != 0) {
|
||||
CameraServerJNI.removeListener(m_handle);
|
||||
}
|
||||
m_handle = 0;
|
||||
}
|
||||
|
||||
public boolean isValid() {
|
||||
return m_handle != 0;
|
||||
}
|
||||
|
||||
private int m_handle;
|
||||
}
|
||||
12
java/src/edu/wpi/cscore/VideoListenerFunction.java
Normal file
12
java/src/edu/wpi/cscore/VideoListenerFunction.java
Normal file
@@ -0,0 +1,12 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) FIRST 2016. All Rights Reserved. */
|
||||
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
||||
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
||||
/* the project. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
package edu.wpi.cscore;
|
||||
|
||||
public interface VideoListenerFunction {
|
||||
void apply(VideoEvent event);
|
||||
}
|
||||
41
java/src/edu/wpi/cscore/VideoMode.java
Normal file
41
java/src/edu/wpi/cscore/VideoMode.java
Normal file
@@ -0,0 +1,41 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) FIRST 2016. All Rights Reserved. */
|
||||
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
||||
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
||||
/* the project. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
package edu.wpi.cscore;
|
||||
|
||||
/// Video mode
|
||||
public class VideoMode {
|
||||
public enum PixelFormat {
|
||||
kUnknown(0), kMJPEG(1), kYUYV(2), kRGB565(3);
|
||||
private int value;
|
||||
|
||||
private PixelFormat(int value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public int getValue() {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
private static final PixelFormat[] m_pixelFormatValues = PixelFormat.values();
|
||||
|
||||
public VideoMode(int pixelFormat, int width, int height, int fps) {
|
||||
this.pixelFormat = m_pixelFormatValues[pixelFormat];
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
this.fps = fps;
|
||||
}
|
||||
|
||||
/// Pixel format
|
||||
public PixelFormat pixelFormat;
|
||||
/// Width in pixels
|
||||
public int width;
|
||||
/// Height in pixels
|
||||
public int height;
|
||||
/// Frames per second
|
||||
public int fps;
|
||||
}
|
||||
104
java/src/edu/wpi/cscore/VideoProperty.java
Normal file
104
java/src/edu/wpi/cscore/VideoProperty.java
Normal file
@@ -0,0 +1,104 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) FIRST 2016. All Rights Reserved. */
|
||||
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
||||
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
||||
/* the project. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
package edu.wpi.cscore;
|
||||
|
||||
public class VideoProperty {
|
||||
public enum Type {
|
||||
kNone(0), kBoolean(1), kInteger(2), kString(4), kEnum(8);
|
||||
private int value;
|
||||
|
||||
private Type(int value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public int getValue() {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
static final Type[] m_typeValues = Type.values();
|
||||
|
||||
public String getName() {
|
||||
return CameraServerJNI.getPropertyName(m_handle);
|
||||
}
|
||||
|
||||
public Type type() {
|
||||
return m_type;
|
||||
}
|
||||
|
||||
public boolean isValid() {
|
||||
return m_type != Type.kNone;
|
||||
}
|
||||
|
||||
// Type checkers
|
||||
public boolean isBoolean() {
|
||||
return m_type == Type.kBoolean;
|
||||
}
|
||||
|
||||
public boolean isInteger() {
|
||||
return m_type == Type.kInteger;
|
||||
}
|
||||
|
||||
public boolean isString() {
|
||||
return m_type == Type.kString;
|
||||
}
|
||||
|
||||
public boolean isEnum() {
|
||||
return m_type == Type.kEnum;
|
||||
}
|
||||
|
||||
public int get() {
|
||||
return CameraServerJNI.getProperty(m_handle);
|
||||
}
|
||||
|
||||
public void set(int value) {
|
||||
CameraServerJNI.setProperty(m_handle, value);
|
||||
}
|
||||
|
||||
public int getMin() {
|
||||
return CameraServerJNI.getPropertyMin(m_handle);
|
||||
}
|
||||
|
||||
public int getMax() {
|
||||
return CameraServerJNI.getPropertyMax(m_handle);
|
||||
}
|
||||
|
||||
public int getStep() {
|
||||
return CameraServerJNI.getPropertyStep(m_handle);
|
||||
}
|
||||
|
||||
public int getDefault() {
|
||||
return CameraServerJNI.getPropertyDefault(m_handle);
|
||||
}
|
||||
|
||||
// String-specific functions
|
||||
public String getString() {
|
||||
return CameraServerJNI.getStringProperty(m_handle);
|
||||
}
|
||||
|
||||
public void setString(String value) {
|
||||
CameraServerJNI.setStringProperty(m_handle, value);
|
||||
}
|
||||
|
||||
// Enum-specific functions
|
||||
public String[] getChoices() {
|
||||
return CameraServerJNI.getEnumPropertyChoices(m_handle);
|
||||
}
|
||||
|
||||
VideoProperty(int handle) {
|
||||
m_handle = handle;
|
||||
m_type = m_typeValues[CameraServerJNI.getPropertyType(handle)];
|
||||
}
|
||||
|
||||
VideoProperty(int handle, Type type) {
|
||||
m_handle = handle;
|
||||
m_type = type;
|
||||
}
|
||||
|
||||
int m_handle;
|
||||
private Type m_type;
|
||||
}
|
||||
77
java/src/edu/wpi/cscore/VideoSink.java
Normal file
77
java/src/edu/wpi/cscore/VideoSink.java
Normal file
@@ -0,0 +1,77 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) FIRST 2016. All Rights Reserved. */
|
||||
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
||||
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
||||
/* the project. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
package edu.wpi.cscore;
|
||||
|
||||
/// A source for video that provides a sequence of frames. Each frame may
|
||||
/// consist of multiple images (e.g. from a stereo or depth camera); these
|
||||
/// are called channels.
|
||||
public class VideoSink {
|
||||
protected VideoSink(int handle) {
|
||||
m_handle = handle;
|
||||
}
|
||||
|
||||
public synchronized void free() {
|
||||
if (m_handle != 0) {
|
||||
CameraServerJNI.releaseSink(m_handle);
|
||||
}
|
||||
m_handle = 0;
|
||||
}
|
||||
|
||||
public boolean isValid() {
|
||||
return m_handle != 0;
|
||||
}
|
||||
|
||||
/// Get the name of the sink. The name is an arbitrary identifier
|
||||
/// provided when the sink is created, and should be unique.
|
||||
public String getName() {
|
||||
return CameraServerJNI.getSinkName(m_handle);
|
||||
}
|
||||
|
||||
/// Get the sink description. This is sink-type specific.
|
||||
public String getDescription() {
|
||||
return CameraServerJNI.getSinkDescription(m_handle);
|
||||
}
|
||||
|
||||
/// Configure which source should provide frames to this sink. Each sink
|
||||
/// can accept frames from only a single source, but a single source can
|
||||
/// provide frames to multiple clients.
|
||||
/// @param source Source
|
||||
public void setSource(VideoSource source) {
|
||||
CameraServerJNI.setSinkSource(m_handle, source.m_handle);
|
||||
}
|
||||
|
||||
/// Get the connected source.
|
||||
/// @return Connected source; nullptr if no source connected.
|
||||
public VideoSource getSource() {
|
||||
// While VideoSource.free() will call releaseSource(), getSinkSource()
|
||||
// increments the internal reference count so this is okay to do.
|
||||
return new VideoSource(CameraServerJNI.getSinkSource(m_handle));
|
||||
}
|
||||
|
||||
/// Get a property of the associated source.
|
||||
/// @param name Property name
|
||||
/// @return Property (type Property::kNone if no property with
|
||||
/// the given name exists or no source connected)
|
||||
public VideoProperty getSourceProperty(String name) {
|
||||
return new VideoProperty(
|
||||
CameraServerJNI.getSinkSourceProperty(m_handle, name));
|
||||
}
|
||||
|
||||
/// Enumerate all existing sinks.
|
||||
/// @return Vector of sinks.
|
||||
public static VideoSink[] enumerateSinks() {
|
||||
int[] handles = CameraServerJNI.enumerateSinks();
|
||||
VideoSink[] rv = new VideoSink[handles.length];
|
||||
for (int i=0; i<handles.length; i++) {
|
||||
rv[i] = new VideoSink(handles[i]);
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
|
||||
protected int m_handle;
|
||||
}
|
||||
128
java/src/edu/wpi/cscore/VideoSource.java
Normal file
128
java/src/edu/wpi/cscore/VideoSource.java
Normal file
@@ -0,0 +1,128 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) FIRST 2016. All Rights Reserved. */
|
||||
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
||||
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
||||
/* the project. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
package edu.wpi.cscore;
|
||||
|
||||
/// A source for video that provides a sequence of frames. Each frame may
|
||||
/// consist of multiple images (e.g. from a stereo or depth camera); these
|
||||
/// are called channels.
|
||||
public class VideoSource {
|
||||
protected VideoSource(int handle) {
|
||||
m_handle = handle;
|
||||
}
|
||||
|
||||
public synchronized void free() {
|
||||
if (m_handle != 0) {
|
||||
CameraServerJNI.releaseSource(m_handle);
|
||||
}
|
||||
m_handle = 0;
|
||||
}
|
||||
|
||||
public boolean isValid() {
|
||||
return m_handle != 0;
|
||||
}
|
||||
|
||||
/// Get the name of the source. The name is an arbitrary identifier
|
||||
/// provided when the source is created, and should be unique.
|
||||
public String getName() {
|
||||
return CameraServerJNI.getSourceName(m_handle);
|
||||
}
|
||||
|
||||
/// Get the source description. This is source-type specific.
|
||||
public String getDescription() {
|
||||
return CameraServerJNI.getSourceDescription(m_handle);
|
||||
}
|
||||
|
||||
/// Get the last time a frame was captured.
|
||||
public long getLastFrameTime() {
|
||||
return CameraServerJNI.getSourceLastFrameTime(m_handle);
|
||||
}
|
||||
|
||||
/// Is the source currently connected to whatever is providing the images?
|
||||
public boolean isConnected() {
|
||||
return CameraServerJNI.isSourceConnected(m_handle);
|
||||
}
|
||||
|
||||
/// Get a property.
|
||||
/// @param name Property name
|
||||
/// @return Property contents (of type Property::kNone if no property with
|
||||
/// the given name exists)
|
||||
public VideoProperty getProperty(String name) {
|
||||
return new VideoProperty(CameraServerJNI.getSourceProperty(m_handle, name));
|
||||
}
|
||||
|
||||
/// Enumerate all properties of this source.
|
||||
public VideoProperty[] enumerateProperties() {
|
||||
int[] handles = CameraServerJNI.enumerateSourceProperties(m_handle);
|
||||
VideoProperty[] rv = new VideoProperty[handles.length];
|
||||
for (int i=0; i<handles.length; i++) {
|
||||
rv[i] = new VideoProperty(handles[i]);
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
|
||||
/// Get the current video mode.
|
||||
public VideoMode getVideoMode() {
|
||||
return CameraServerJNI.getSourceVideoMode(m_handle);
|
||||
}
|
||||
|
||||
/// Set the video mode.
|
||||
/// @param mode Video mode
|
||||
public boolean setVideoMode(VideoMode mode) {
|
||||
return CameraServerJNI.setSourceVideoMode(m_handle, mode.pixelFormat.getValue(), mode.width, mode.height, mode.fps);
|
||||
}
|
||||
|
||||
/// Set the video mode.
|
||||
/// @param pixelFormat desired pixel format
|
||||
/// @param width desired width
|
||||
/// @param height desired height
|
||||
/// @param fps desired FPS
|
||||
/// @return True if set successfully
|
||||
public boolean setVideoMode(VideoMode.PixelFormat pixelFormat, int width, int height, int fps) {
|
||||
return CameraServerJNI.setSourceVideoMode(m_handle, pixelFormat.getValue(), width, height, fps);
|
||||
}
|
||||
|
||||
/// Set the pixel format.
|
||||
/// @param pixelFormat desired pixel format
|
||||
/// @return True if set successfully
|
||||
public boolean setPixelFormat(VideoMode.PixelFormat pixelFormat) {
|
||||
return CameraServerJNI.setSourcePixelFormat(m_handle, pixelFormat.getValue());
|
||||
}
|
||||
|
||||
/// Set the resolution.
|
||||
/// @param width desired width
|
||||
/// @param height desired height
|
||||
/// @return True if set successfully
|
||||
public boolean setResolution(int width, int height) {
|
||||
return CameraServerJNI.setSourceResolution(m_handle, width, height);
|
||||
}
|
||||
|
||||
/// Set the frames per second (FPS).
|
||||
/// @param fps desired FPS
|
||||
/// @return True if set successfully
|
||||
public boolean setFPS(int fps) {
|
||||
return CameraServerJNI.setSourceFPS(m_handle, fps);
|
||||
}
|
||||
|
||||
/// Enumerate all known video modes for this source.
|
||||
public VideoMode[] enumerateVideoModes() {
|
||||
return CameraServerJNI.enumerateSourceVideoModes(m_handle);
|
||||
}
|
||||
|
||||
/// Enumerate all existing sources.
|
||||
/// @return Vector of sources.
|
||||
public static VideoSource[] enumerateSources() {
|
||||
int[] handles = CameraServerJNI.enumerateSources();
|
||||
VideoSource[] rv = new VideoSource[handles.length];
|
||||
for (int i=0; i<handles.length; i++) {
|
||||
rv[i] = new VideoSource(handles[i]);
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
|
||||
protected int m_handle;
|
||||
}
|
||||
Reference in New Issue
Block a user