mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-26 01:51:41 +00:00
Add support for enumerating and changing USB camera video mode.
This commit is contained in:
@@ -21,6 +21,7 @@ using namespace wpi::java;
|
||||
// Used for callback.
|
||||
static JavaVM *jvm = nullptr;
|
||||
static jclass usbCameraInfoCls = nullptr;
|
||||
static jclass videoModeCls = nullptr;
|
||||
|
||||
extern "C" {
|
||||
|
||||
@@ -40,6 +41,12 @@ JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) {
|
||||
if (!usbCameraInfoCls) return JNI_ERR;
|
||||
env->DeleteLocalRef(local);
|
||||
|
||||
local = env->FindClass("edu/wpi/cameraserver/VideoMode");
|
||||
if (!local) return JNI_ERR;
|
||||
videoModeCls = static_cast<jclass>(env->NewGlobalRef(local));
|
||||
if (!videoModeCls) return JNI_ERR;
|
||||
env->DeleteLocalRef(local);
|
||||
|
||||
return JNI_VERSION_1_6;
|
||||
}
|
||||
|
||||
@@ -49,6 +56,7 @@ JNIEXPORT void JNICALL JNI_OnUnload(JavaVM *vm, void *reserved) {
|
||||
return;
|
||||
// Delete global references
|
||||
if (usbCameraInfoCls) env->DeleteGlobalRef(usbCameraInfoCls);
|
||||
if (videoModeCls) env->DeleteGlobalRef(videoModeCls);
|
||||
jvm = nullptr;
|
||||
}
|
||||
|
||||
@@ -73,6 +81,15 @@ static jobject MakeJObject(JNIEnv *env, const cs::USBCameraInfo &info) {
|
||||
static_cast<jint>(info.dev), path.obj(), name.obj());
|
||||
}
|
||||
|
||||
static jobject MakeJObject(JNIEnv *env, const cs::VideoMode &videoMode) {
|
||||
static jmethodID constructor =
|
||||
env->GetMethodID(videoModeCls, "<init>", "(IIII)V");
|
||||
return env->NewObject(
|
||||
videoModeCls, constructor, static_cast<jint>(videoMode.pixelFormat),
|
||||
static_cast<jint>(videoMode.width), static_cast<jint>(videoMode.height),
|
||||
static_cast<jint>(videoMode.fps));
|
||||
}
|
||||
|
||||
extern "C" {
|
||||
|
||||
/*
|
||||
@@ -276,13 +293,19 @@ JNIEXPORT jint JNICALL Java_edu_wpi_cameraserver_CameraServerJNI_createHTTPSourc
|
||||
/*
|
||||
* Class: edu_wpi_cameraserver_CameraServerJNI
|
||||
* Method: createCvSource
|
||||
* Signature: (Ljava/lang/String;)I
|
||||
* Signature: (Ljava/lang/String;IIII)I
|
||||
*/
|
||||
JNIEXPORT jint JNICALL Java_edu_wpi_cameraserver_CameraServerJNI_createCvSource
|
||||
(JNIEnv *env, jclass, jstring name)
|
||||
(JNIEnv *env, jclass, jstring name, jint pixelFormat, jint width, jint height,
|
||||
jint fps)
|
||||
{
|
||||
CS_Status status;
|
||||
auto val = cs::CreateCvSource(JStringRef{env, name}, &status);
|
||||
auto val = cs::CreateCvSource(
|
||||
JStringRef{env, name},
|
||||
cs::VideoMode{static_cast<cs::VideoMode::PixelFormat>(pixelFormat),
|
||||
static_cast<int>(width), static_cast<int>(height),
|
||||
static_cast<int>(fps)},
|
||||
&status);
|
||||
CheckStatus(env, status);
|
||||
return val;
|
||||
}
|
||||
@@ -374,6 +397,103 @@ JNIEXPORT jintArray JNICALL Java_edu_wpi_cameraserver_CameraServerJNI_enumerateS
|
||||
return MakeJIntArray(env, arr);
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: edu_wpi_cameraserver_CameraServerJNI
|
||||
* Method: getSourceVideoMode
|
||||
* Signature: (I)Ledu/wpi/cameraserver/VideoMode;
|
||||
*/
|
||||
JNIEXPORT jobject JNICALL Java_edu_wpi_cameraserver_CameraServerJNI_getSourceVideoMode
|
||||
(JNIEnv *env, jclass, jint source)
|
||||
{
|
||||
CS_Status status;
|
||||
auto val = cs::GetSourceVideoMode(source, &status);
|
||||
if (!CheckStatus(env, status)) return nullptr;
|
||||
return MakeJObject(env, val);
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: edu_wpi_cameraserver_CameraServerJNI
|
||||
* Method: setSourceVideoMode
|
||||
* Signature: (IIIII)Z
|
||||
*/
|
||||
JNIEXPORT jboolean JNICALL Java_edu_wpi_cameraserver_CameraServerJNI_setSourceVideoMode
|
||||
(JNIEnv *env, jclass, jint source, jint pixelFormat, jint width, jint height,
|
||||
jint fps)
|
||||
{
|
||||
CS_Status status;
|
||||
auto val = cs::SetSourceVideoMode(
|
||||
source,
|
||||
cs::VideoMode(static_cast<cs::VideoMode::PixelFormat>(pixelFormat), width,
|
||||
height, fps),
|
||||
&status);
|
||||
CheckStatus(env, status);
|
||||
return val;
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: edu_wpi_cameraserver_CameraServerJNI
|
||||
* Method: setSourcePixelFormat
|
||||
* Signature: (II)Z
|
||||
*/
|
||||
JNIEXPORT jboolean JNICALL Java_edu_wpi_cameraserver_CameraServerJNI_setSourcePixelFormat
|
||||
(JNIEnv *env, jclass, jint source, jint pixelFormat)
|
||||
{
|
||||
CS_Status status;
|
||||
auto val = cs::SetSourcePixelFormat(
|
||||
source, static_cast<cs::VideoMode::PixelFormat>(pixelFormat), &status);
|
||||
CheckStatus(env, status);
|
||||
return val;
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: edu_wpi_cameraserver_CameraServerJNI
|
||||
* Method: setSourceResolution
|
||||
* Signature: (III)Z
|
||||
*/
|
||||
JNIEXPORT jboolean JNICALL Java_edu_wpi_cameraserver_CameraServerJNI_setSourceResolution
|
||||
(JNIEnv *env, jclass, jint source, jint width, jint height)
|
||||
{
|
||||
CS_Status status;
|
||||
auto val = cs::SetSourceResolution(source, width, height, &status);
|
||||
CheckStatus(env, status);
|
||||
return val;
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: edu_wpi_cameraserver_CameraServerJNI
|
||||
* Method: setSourceFPS
|
||||
* Signature: (II)Z
|
||||
*/
|
||||
JNIEXPORT jboolean JNICALL Java_edu_wpi_cameraserver_CameraServerJNI_setSourceFPS
|
||||
(JNIEnv *env, jclass, jint source, jint fps)
|
||||
{
|
||||
CS_Status status;
|
||||
auto val = cs::SetSourceFPS(source, fps, &status);
|
||||
CheckStatus(env, status);
|
||||
return val;
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: edu_wpi_cameraserver_CameraServerJNI
|
||||
* Method: enumerateSourceVideoModes
|
||||
* Signature: (I)[Ledu/wpi/cameraserver/VideoMode;
|
||||
*/
|
||||
JNIEXPORT jobjectArray JNICALL Java_edu_wpi_cameraserver_CameraServerJNI_enumerateSourceVideoModes
|
||||
(JNIEnv *env, jclass, jint source)
|
||||
{
|
||||
CS_Status status;
|
||||
auto arr = cs::EnumerateSourceVideoModes(source, &status);
|
||||
if (!CheckStatus(env, status)) return nullptr;
|
||||
jobjectArray jarr =
|
||||
env->NewObjectArray(arr.size(), videoModeCls, nullptr);
|
||||
if (!jarr) return nullptr;
|
||||
for (size_t i = 0; i < arr.size(); ++i) {
|
||||
JLocal<jobject> jelem{env, MakeJObject(env, arr[i])};
|
||||
env->SetObjectArrayElement(jarr, i, jelem);
|
||||
}
|
||||
return jarr;
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: edu_wpi_cameraserver_CameraServerJNI
|
||||
* Method: copySource
|
||||
|
||||
@@ -93,7 +93,7 @@ public class CameraServerJNI {
|
||||
public static native int createUSBSourceDev(String name, int dev);
|
||||
public static native int createUSBSourcePath(String name, String path);
|
||||
public static native int createHTTPSource(String name, String url);
|
||||
public static native int createCvSource(String name);
|
||||
public static native int createCvSource(String name, int pixelFormat, int width, int height, int fps);
|
||||
|
||||
//
|
||||
// Source Functions
|
||||
@@ -104,6 +104,12 @@ public class CameraServerJNI {
|
||||
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);
|
||||
|
||||
|
||||
@@ -11,8 +11,19 @@ package edu.wpi.cameraserver;
|
||||
public class CvSource extends VideoSource {
|
||||
/// Create an OpenCV source.
|
||||
/// @param name Source name (arbitrary unique identifier)
|
||||
public CvSource(String name) {
|
||||
super(CameraServerJNI.createCvSource(name));
|
||||
/// @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.
|
||||
|
||||
41
java/src/edu/wpi/cameraserver/VideoMode.java
Normal file
41
java/src/edu/wpi/cameraserver/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.cameraserver;
|
||||
|
||||
/// 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;
|
||||
}
|
||||
@@ -19,7 +19,7 @@ public class VideoProperty {
|
||||
public int getValue() {
|
||||
return value;
|
||||
}
|
||||
};
|
||||
}
|
||||
private static final Type[] m_typeValues = Type.values();
|
||||
|
||||
public String getName() {
|
||||
|
||||
@@ -65,6 +65,54 @@ public class VideoSource {
|
||||
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() {
|
||||
|
||||
Reference in New Issue
Block a user