001/*----------------------------------------------------------------------------*/
002/* Copyright (c) 2016-2018 FIRST. 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/**
013 * A source that represents a video camera.
014 */
015public class CvSource extends VideoSource {
016  /**
017   * Create an OpenCV source.
018   * @param name Source name (arbitrary unique identifier)
019   * @param mode Video mode being generated
020   */
021  public CvSource(String name, VideoMode mode) {
022    super(CameraServerJNI.createCvSource(name, mode.pixelFormat.getValue(), mode.width, mode.height, mode.fps));
023  }
024
025  /**
026   * Create an OpenCV source.
027   * @param name Source name (arbitrary unique identifier)
028   * @param pixelFormat Pixel format
029   * @param width width
030   * @param height height
031   * @param fps fps
032   */
033  public CvSource(String name, VideoMode.PixelFormat pixelFormat, int width, int height, int fps) {
034    super(CameraServerJNI.createCvSource(name, pixelFormat.getValue(), width, height, fps));
035  }
036
037  /**
038   * Put an OpenCV image and notify sinks.
039   * Only 8-bit single-channel or 3-channel (with BGR channel order) images
040   * are supported. If the format, depth or channel order is different, use
041   * Mat.convertTo() and/or cvtColor() to convert it first.
042   * @param image OpenCV image
043   */
044  public void putFrame(Mat image) {
045    CameraServerJNI.putSourceFrame(m_handle, image.nativeObj);
046  }
047
048  /**
049   * Signal sinks that an error has occurred.  This should be called instead
050   * of NotifyFrame when an error occurs.
051   */
052  public void notifyError(String msg) {
053    CameraServerJNI.notifySourceError(m_handle, msg);
054  }
055
056  /**
057   * Set source connection status.  Defaults to true.
058   * @param connected True for connected, false for disconnected
059   */
060  public void setConnected(boolean connected) {
061    CameraServerJNI.setSourceConnected(m_handle, connected);
062  }
063
064  /**
065   * Set source description.
066   * @param description Description
067   */
068  public void setDescription(String description) {
069    CameraServerJNI.setSourceDescription(m_handle, description);
070  }
071
072  /**
073   * Create a property.
074   * @param name Property name
075   * @param kind Property kind
076   * @param minimum Minimum value
077   * @param maximum Maximum value
078   * @param step Step value
079   * @param defaultValue Default value
080   * @param value Current value
081   * @return Property
082   */
083  public VideoProperty createProperty(String name, VideoProperty.Kind kind, int minimum, int maximum, int step, int defaultValue, int value) {
084    return new VideoProperty(
085        CameraServerJNI.createSourceProperty(m_handle, name, kind.getValue(), minimum, maximum, step, defaultValue, value));
086  }
087
088  /// Create an integer property.
089  /// @param name Property name
090  /// @param minimum Minimum value
091  /// @param maximum Maximum value
092  /// @param step Step value
093  /// @param defaultValue Default value
094  /// @param value Current value
095  /// @return Property
096  public VideoProperty createIntegerProperty(String name, int minimum, int maximum, int step, int defaultValue, int value) {
097    return new VideoProperty(
098        CameraServerJNI.createSourceProperty(m_handle, name, VideoProperty.Kind.kInteger.getValue(), minimum, maximum, step, defaultValue, value));
099  }
100
101  /// Create a boolean property.
102  /// @param name Property name
103  /// @param defaultValue Default value
104  /// @param value Current value
105  /// @return Property
106  public VideoProperty createBooleanProperty(String name, boolean defaultValue, boolean value) {
107    return new VideoProperty(
108        CameraServerJNI.createSourceProperty(m_handle, name, VideoProperty.Kind.kBoolean.getValue(), 0, 1, 1, defaultValue ? 1 : 0, value ? 1 : 0));
109  }
110
111  /// Create a string property.
112  /// @param name Property name
113  /// @param value Current value
114  /// @return Property
115  public VideoProperty createStringProperty(String name, String value) {
116    VideoProperty prop = new VideoProperty(
117        CameraServerJNI.createSourceProperty(m_handle, name, VideoProperty.Kind.kString.getValue(), 0, 0, 0, 0, 0));
118    prop.setString(value);
119    return prop;
120  }
121
122  /// Create a property with a change callback.
123  /// @param name Property name
124  /// @param kind Property kind
125  /// @param minimum Minimum value
126  /// @param maximum Maximum value
127  /// @param step Step value
128  /// @param defaultValue Default value
129  /// @param value Current value
130  /// @param onChange Callback to call when the property value changes
131  /// @return Property
132  //public VideoProperty createProperty(
133  //    String name, VideoProperty.Kind kind, int minimum, int maximum, int step, int defaultValue, int value,
134  //    std::function<void(VideoProperty property)>
135  //        onChange);
136
137  /**
138   * Configure enum property choices.
139   * @param property Property
140   * @param choices Choices
141   */
142  public void SetEnumPropertyChoices(VideoProperty property, String[] choices) {
143    CameraServerJNI.setSourceEnumPropertyChoices(m_handle, property.m_handle, choices);
144  }
145}