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-08-25 23:13:48 -07:00
|
|
|
package edu.wpi.cameraserver;
|
|
|
|
|
|
2016-10-21 14:50:24 -07:00
|
|
|
import org.opencv.core.Mat;
|
|
|
|
|
|
2016-08-25 23:13:48 -07:00
|
|
|
/// 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));
|
|
|
|
|
//}
|
|
|
|
|
|
2016-10-26 23:37:00 -07:00
|
|
|
/// Set sink description.
|
|
|
|
|
/// @param description Description
|
|
|
|
|
public void setDescription(String description) {
|
|
|
|
|
CameraServerJNI.setSinkDescription(m_handle, description);
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-18 14:14:35 -07:00
|
|
|
/// Wait for the next frame and get the image.
|
2016-08-25 23:13:48 -07:00
|
|
|
/// @return Frame time, or 0 on error (call GetError() to obtain the error
|
|
|
|
|
/// message);
|
2016-10-21 14:50:24 -07:00
|
|
|
public long grabFrame(Mat image) {
|
|
|
|
|
return CameraServerJNI.grabSinkFrame(m_handle, image.nativeObj);
|
|
|
|
|
}
|
2016-08-25 23:13:48 -07:00
|
|
|
|
|
|
|
|
/// 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);
|
|
|
|
|
}
|
|
|
|
|
}
|