Add support for OpenCV to Java wrappers.

This commit is contained in:
Peter Johnson
2016-10-21 14:50:24 -07:00
parent 6641612de5
commit 017ec83ce3
5 changed files with 47 additions and 7 deletions

View File

@@ -21,6 +21,11 @@ sourceSets {
}
}
dependencies {
compile 'org.opencv:opencv-java:3.1.0'
runtime 'org.opencv:opencv-java:3.1.0'
}
jar {
description = 'Generates CameraServer jar, with the JNI shared libraries embedded'
baseName = 'cameraserver'
@@ -85,7 +90,7 @@ task jniHeadersCameraServer {
exec {
executable org.gradle.internal.jvm.Jvm.current().getExecutable('javah')
args '-d', outputFolder
args '-classpath', sourceSets.main.output.classesDir
args '-classpath', sourceSets.main.runtimeClasspath.asPath
args 'edu.wpi.cameraserver.CameraServerJNI'
}
}

View File

@@ -521,6 +521,20 @@ JNIEXPORT void JNICALL Java_edu_wpi_cameraserver_CameraServerJNI_releaseSource
CheckStatus(env, status);
}
/*
* Class: edu_wpi_cameraserver_CameraServerJNI
* Method: putSourceFrame
* Signature: (IJ)V
*/
JNIEXPORT void JNICALL Java_edu_wpi_cameraserver_CameraServerJNI_putSourceFrame
(JNIEnv *env, jclass, jint source, jlong imageNativeObj)
{
cv::Mat& image = *((cv::Mat*)imageNativeObj);
CS_Status status;
cs::PutSourceFrame(source, image, &status);
CheckStatus(env, status);
}
/*
* Class: edu_wpi_cameraserver_CameraServerJNI
* Method: notifySourceError
@@ -716,6 +730,21 @@ JNIEXPORT void JNICALL Java_edu_wpi_cameraserver_CameraServerJNI_releaseSink
CheckStatus(env, status);
}
/*
* Class: edu_wpi_cameraserver_CameraServerJNI
* Method: grabSinkFrame
* Signature: (IJ)J
*/
JNIEXPORT jlong JNICALL Java_edu_wpi_cameraserver_CameraServerJNI_grabSinkFrame
(JNIEnv *env, jclass, jint sink, jlong imageNativeObj)
{
cv::Mat& image = *((cv::Mat*)imageNativeObj);
CS_Status status;
auto rv = cs::GrabSinkFrame(sink, image, &status);
CheckStatus(env, status);
return rv;
}
/*
* Class: edu_wpi_cameraserver_CameraServerJNI
* Method: getSinkError

View File

@@ -116,7 +116,7 @@ public class CameraServerJNI {
//
// OpenCV Source Functions
//
//public static native void putSourceFrame(int source, CvMat image);
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 int createSourceProperty(int source, String name, int type);
@@ -149,7 +149,7 @@ public class CameraServerJNI {
//
// OpenCV Sink Functions
//
//public static native int grabSinkFrame(int sink, CvMat image);
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);

View File

@@ -7,6 +7,8 @@
package edu.wpi.cameraserver;
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.
@@ -33,9 +35,9 @@ public class CvSink extends VideoSink {
/// 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(CvMat image) {
// return CameraServerJNI.grabSinkFrame(m_handle, image);
//}
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.

View File

@@ -7,6 +7,8 @@
package edu.wpi.cameraserver;
import org.opencv.core.Mat;
/// A source that represents a video camera.
public class CvSource extends VideoSource {
/// Create an OpenCV source.
@@ -28,7 +30,9 @@ public class CvSource extends VideoSource {
/// Put an OpenCV image and notify sinks.
/// @param image OpenCV image
//public void putFrame(Mat 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.