001/*----------------------------------------------------------------------------*/
002/* Copyright (c) FIRST 2016. All Rights Reserved.                             */
003/* Open Source Software - may be modified and shared by FRC teams. The code   */
004/* must be accompanied by the FIRST BSD license file in the root directory of */
005/* the project.                                                               */
006/*----------------------------------------------------------------------------*/
007
008package edu.wpi.cscore;
009
010import org.opencv.core.Mat;
011
012/// A sink for user code to accept video frames as OpenCV images.
013public class CvSink extends VideoSink {
014  /// Create a sink for accepting OpenCV images.
015  /// WaitForFrame() must be called on the created sink to get each new
016  /// image.
017  /// @param name Source name (arbitrary unique identifier)
018  public CvSink(String name) {
019    super(CameraServerJNI.createCvSink(name));
020  }
021
022  /// Create a sink for accepting OpenCV images in a separate thread.
023  /// A thread will be created that calls WaitForFrame() and calls the
024  /// processFrame() callback each time a new frame arrives.
025  /// @param name Source name (arbitrary unique identifier)
026  /// @param processFrame Frame processing function; will be called with a
027  ///        time=0 if an error occurred.  processFrame should call GetImage()
028  ///        or GetError() as needed, but should not call (except in very
029  ///        unusual circumstances) WaitForImage().
030  //public CvSink(llvm::StringRef name,
031  //       std::function<void(uint64_t time)> processFrame) {
032  //  super(CameraServerJNI.createCvSinkCallback(name, processFrame));
033  //}
034
035  /// Set sink description.
036  /// @param description Description
037  public void setDescription(String description) {
038    CameraServerJNI.setSinkDescription(m_handle, description);
039  }
040
041  /// Wait for the next frame and get the image.
042  /// The provided image will have three 3-bit channels stored in BGR order.
043  /// @return Frame time, or 0 on error (call GetError() to obtain the error
044  ///         message);
045  public long grabFrame(Mat image) {
046    return CameraServerJNI.grabSinkFrame(m_handle, image.nativeObj);
047  }
048
049  /// Get error string.  Call this if WaitForFrame() returns 0 to determine
050  /// what the error is.
051  public String getError() {
052    return CameraServerJNI.getSinkError(m_handle);
053  }
054
055  /// Enable or disable getting new frames.
056  /// Disabling will cause processFrame (for callback-based CvSinks) to not
057  /// be called and WaitForFrame() to not return.  This can be used to save
058  /// processor resources when frames are not needed.
059  public void setEnabled(boolean enabled) {
060    CameraServerJNI.setSinkEnabled(m_handle, enabled);
061  }
062}