2016-08-26 09:01:54 -07:00
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
/* 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. */
|
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
2016-11-05 22:11:55 -07:00
|
|
|
package edu.wpi.cscore;
|
2016-08-14 12:38:13 -07:00
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
|
import java.io.InputStream;
|
|
|
|
|
import java.io.OutputStream;
|
|
|
|
|
import java.io.FileOutputStream;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.nio.ByteBuffer;
|
2016-11-11 21:55:13 -08:00
|
|
|
import org.opencv.core.Core;
|
2016-08-14 12:38:13 -07:00
|
|
|
|
|
|
|
|
public class CameraServerJNI {
|
|
|
|
|
static boolean libraryLoaded = false;
|
2016-11-11 21:55:13 -08:00
|
|
|
static boolean cvLibraryLoaded = false;
|
2016-08-14 12:38:13 -07:00
|
|
|
static File jniLibrary = null;
|
|
|
|
|
static {
|
|
|
|
|
if (!libraryLoaded) {
|
|
|
|
|
try {
|
2016-11-06 19:28:14 -08:00
|
|
|
System.loadLibrary("cscore");
|
2016-11-05 11:37:12 -07:00
|
|
|
} catch (UnsatisfiedLinkError e) {
|
|
|
|
|
try {
|
|
|
|
|
String osname = System.getProperty("os.name");
|
|
|
|
|
String resname;
|
|
|
|
|
if (osname.startsWith("Windows"))
|
|
|
|
|
resname = "/Windows/" + System.getProperty("os.arch") + "/";
|
2016-08-14 12:38:13 -07:00
|
|
|
else
|
2016-11-05 11:37:12 -07:00
|
|
|
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);
|
2016-08-14 12:38:13 -07:00
|
|
|
|
2016-11-05 11:37:12 -07:00
|
|
|
byte[] buffer = new byte[1024];
|
|
|
|
|
int readBytes;
|
|
|
|
|
try {
|
|
|
|
|
while ((readBytes = is.read(buffer)) != -1) {
|
|
|
|
|
os.write(buffer, 0, readBytes);
|
|
|
|
|
}
|
|
|
|
|
} finally {
|
|
|
|
|
os.close();
|
|
|
|
|
is.close();
|
2016-08-14 12:38:13 -07:00
|
|
|
}
|
|
|
|
|
System.load(jniLibrary.getAbsolutePath());
|
2016-11-05 11:37:12 -07:00
|
|
|
} else {
|
2016-11-06 19:28:14 -08:00
|
|
|
System.loadLibrary("cscore");
|
2016-08-14 12:38:13 -07:00
|
|
|
}
|
2016-11-05 11:37:12 -07:00
|
|
|
} catch (IOException ex) {
|
|
|
|
|
ex.printStackTrace();
|
|
|
|
|
System.exit(1);
|
2016-08-14 12:38:13 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
libraryLoaded = true;
|
2016-11-11 21:55:13 -08:00
|
|
|
if (!cvLibraryLoaded) {
|
|
|
|
|
try {
|
|
|
|
|
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
|
|
|
|
|
} catch (UnsatisfiedLinkError ex) {
|
|
|
|
|
ex.printStackTrace();
|
|
|
|
|
System.exit(1);
|
|
|
|
|
}
|
|
|
|
|
cvLibraryLoaded = true;
|
|
|
|
|
}
|
2016-08-14 12:38:13 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-25 23:13:48 -07:00
|
|
|
//
|
|
|
|
|
// Property Functions
|
|
|
|
|
//
|
|
|
|
|
public static native int getPropertyType(int property);
|
2016-08-26 08:19:28 -07:00
|
|
|
public static native String getPropertyName(int property);
|
2016-09-20 22:17:12 -07:00
|
|
|
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);
|
2016-08-25 23:13:48 -07:00
|
|
|
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
|
|
|
|
|
//
|
2016-10-15 23:07:28 -07:00
|
|
|
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);
|
2016-09-29 00:04:16 -07:00
|
|
|
public static native int createCvSource(String name, int pixelFormat, int width, int height, int fps);
|
2016-08-25 23:13:48 -07:00
|
|
|
|
|
|
|
|
//
|
|
|
|
|
// 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);
|
2016-08-26 09:24:18 -07:00
|
|
|
public static native int getSourceProperty(int source, String name);
|
2016-08-26 08:19:28 -07:00
|
|
|
public static native int[] enumerateSourceProperties(int source);
|
2016-09-29 00:04:16 -07:00
|
|
|
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);
|
2016-08-25 23:13:48 -07:00
|
|
|
public static native int copySource(int source);
|
|
|
|
|
public static native void releaseSource(int source);
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
// OpenCV Source Functions
|
|
|
|
|
//
|
2016-10-21 14:50:24 -07:00
|
|
|
public static native void putSourceFrame(int source, long imageNativeObj);
|
2016-08-25 23:13:48 -07:00
|
|
|
public static native void notifySourceError(int source, String msg);
|
|
|
|
|
public static native void setSourceConnected(int source, boolean connected);
|
2016-10-23 08:43:06 -07:00
|
|
|
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);
|
2016-08-25 23:13:48 -07:00
|
|
|
|
|
|
|
|
//
|
|
|
|
|
// Sink Creation Functions
|
|
|
|
|
//
|
2016-10-15 22:44:26 -07:00
|
|
|
public static native int createMJPEGServer(String name, String listenAddress, int port);
|
2016-08-25 23:13:48 -07:00
|
|
|
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);
|
2016-08-26 09:24:18 -07:00
|
|
|
public static native int getSinkSourceProperty(int sink, String name);
|
2016-08-25 23:13:48 -07:00
|
|
|
public static native int getSinkSource(int sink);
|
|
|
|
|
public static native int copySink(int sink);
|
|
|
|
|
public static native void releaseSink(int sink);
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
// OpenCV Sink Functions
|
|
|
|
|
//
|
2016-10-26 23:37:00 -07:00
|
|
|
public static native void setSinkDescription(int sink, String description);
|
2016-10-21 14:50:24 -07:00
|
|
|
public static native long grabSinkFrame(int sink, long imageNativeObj);
|
2016-08-25 23:13:48 -07:00
|
|
|
public static native String getSinkError(int sink);
|
|
|
|
|
public static native void setSinkEnabled(int sink, boolean enabled);
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
// Listener Functions
|
|
|
|
|
//
|
2016-11-05 13:13:09 -07:00
|
|
|
public static native int addListener(VideoListenerFunction listener,
|
|
|
|
|
int eventMask, boolean immediateNotify);
|
2016-08-25 23:13:48 -07:00
|
|
|
|
2016-11-04 12:46:22 -07:00
|
|
|
public static native void removeListener(int handle);
|
2016-08-25 23:13:48 -07:00
|
|
|
|
|
|
|
|
//
|
|
|
|
|
// Utility Functions
|
|
|
|
|
//
|
|
|
|
|
public static native USBCameraInfo[] enumerateUSBCameras();
|
|
|
|
|
|
|
|
|
|
public static native int[] enumerateSources();
|
|
|
|
|
|
|
|
|
|
public static native int[] enumerateSinks();
|
2016-08-14 12:38:13 -07:00
|
|
|
}
|