From dcf773c3ef9a90d67d578f7c4a1e6073301d9424 Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Wed, 16 Nov 2016 20:55:30 -0800 Subject: [PATCH] Add USBCamera setting functions similar to the old WPILib nivision ones. --- include/cscore_oo.h | 39 ++++++++++++ include/cscore_oo.inl | 44 ++++++++++++++ java/src/edu/wpi/cscore/USBCamera.java | 83 ++++++++++++++++++++++++++ 3 files changed, 166 insertions(+) diff --git a/include/cscore_oo.h b/include/cscore_oo.h index 71c7b66a99..10dcbe92bc 100644 --- a/include/cscore_oo.h +++ b/include/cscore_oo.h @@ -191,7 +191,22 @@ class VideoSource { /// A source that represents a USB camera. class USBCamera : public VideoSource { + private: + static constexpr char const* kPropWbAuto = "white_balance_temperature_auto"; + static constexpr char const* kPropWbValue = "white_balance_temperature"; + static constexpr char const* kPropExAuto = "exposure_auto"; + static constexpr char const* kPropExValue = "exposure_absolute"; + static constexpr char const* kPropBrValue = "brightness"; + public: + enum WhiteBalance { + kFixedIndoor = 3000, + kFixedOutdoor1 = 4000, + kFixedOutdoor2 = 5000, + kFixedFluorescent1 = 5100, + kFixedFlourescent2 = 5200 + }; + /// Create a source for a USB camera based on device number. /// @param name Source name (arbitrary unique identifier) /// @param dev Device number (e.g. 0 for /dev/video0) @@ -205,6 +220,30 @@ class USBCamera : public VideoSource { /// Enumerate USB cameras on the local system. /// @return Vector of USB camera information (one for each camera) static std::vector EnumerateUSBCameras(); + + /// Set the brightness, as a percentage (0-100). + void SetBrightness(int brightness); + + /// Get the brightness, as a percentage (0-100). + int GetBrightness(); + + /// Set the white balance to auto. + void SetWhiteBalanceAuto(); + + /// Set the white balance to hold current. + void SetWhiteBalanceHoldCurrent(); + + /// Set the white balance to manual, with specified color temperature. + void SetWhiteBalanceManual(int value); + + /// Set the exposure to auto aperature. + void SetExposureAuto(); + + /// Set the exposure to hold current. + void SetExposureHoldCurrent(); + + /// Set the exposure to manual, as a percentage (0-100). + void SetExposureManual(int value); }; /// A source that represents a MJPEG-over-HTTP (IP) camera. diff --git a/include/cscore_oo.inl b/include/cscore_oo.inl index 1a40bfd0c8..0d2b0a693d 100644 --- a/include/cscore_oo.inl +++ b/include/cscore_oo.inl @@ -173,6 +173,50 @@ inline std::vector USBCamera::EnumerateUSBCameras() { return ::cs::EnumerateUSBCameras(&status); } +inline void USBCamera::SetBrightness(int brightness) { + if (brightness > 100) { + brightness = 100; + } else if (brightness < 0) { + brightness = 0; + } + GetProperty(kPropBrValue).Set(brightness); +} + +inline int USBCamera::GetBrightness() { + return GetProperty(kPropBrValue).Get(); +} + +inline void USBCamera::SetWhiteBalanceAuto() { + GetProperty(kPropWbAuto).Set(1); // auto +} + +inline void USBCamera::SetWhiteBalanceHoldCurrent() { + GetProperty(kPropWbAuto).Set(0); // manual +} + +inline void USBCamera::SetWhiteBalanceManual(int value) { + GetProperty(kPropWbAuto).Set(0); // manual + GetProperty(kPropWbValue).Set(value); +} + +inline void USBCamera::SetExposureAuto() { + GetProperty(kPropExAuto).Set(0); // auto; yes, this is opposite of WB +} + +inline void USBCamera::SetExposureHoldCurrent() { + GetProperty(kPropExAuto).Set(1); // manual +} + +inline void USBCamera::SetExposureManual(int value) { + GetProperty(kPropExAuto).Set(1); // manual + if (value > 100) { + value = 100; + } else if (value < 0) { + value = 0; + } + GetProperty(kPropExValue).Set(value); +} + inline HTTPCamera::HTTPCamera(llvm::StringRef name, llvm::StringRef url) { m_handle = CreateHTTPCamera(name, url, &m_status); } diff --git a/java/src/edu/wpi/cscore/USBCamera.java b/java/src/edu/wpi/cscore/USBCamera.java index b0c644230b..3bcad348c8 100644 --- a/java/src/edu/wpi/cscore/USBCamera.java +++ b/java/src/edu/wpi/cscore/USBCamera.java @@ -9,6 +9,27 @@ package edu.wpi.cscore; /// A source that represents a USB camera. public class USBCamera extends VideoSource { + private static final String kPropWbAuto = "white_balance_temperature_auto"; + private static final String kPropWbValue = "white_balance_temperature"; + private static final String kPropExAuto = "exposure_auto"; + private static final String kPropExValue = "exposure_absolute"; + private static final String kPropBrValue = "brightness"; + + public class WhiteBalance { + public static final int kFixedIndoor = 3000; + public static final int kFixedOutdoor1 = 4000; + public static final int kFixedOutdoor2 = 5000; + public static final int kFixedFluorescent1 = 5100; + public static final int kFixedFlourescent2 = 5200; + } + + // Cached to avoid duplicate string lookups + private VideoProperty m_wbAuto; + private VideoProperty m_wbValue; + private VideoProperty m_exAuto; + private VideoProperty m_exValue; + private VideoProperty m_brValue; + /// Create a source for a USB camera based on device number. /// @param name Source name (arbitrary unique identifier) /// @param dev Device number (e.g. 0 for /dev/video0) @@ -28,4 +49,66 @@ public class USBCamera extends VideoSource { public static USBCameraInfo[] enumerateUSBCameras() { return CameraServerJNI.enumerateUSBCameras(); } + + /// Set the brightness, as a percentage (0-100). + public synchronized void setBrightness(int brightness) { + if (brightness > 100) { + brightness = 100; + } else if (brightness < 0) { + brightness = 0; + } + if (m_brValue == null) m_brValue = getProperty(kPropBrValue); + m_brValue.set(brightness); + } + + /// Get the brightness, as a percentage (0-100). + public synchronized int getBrightness() { + if (m_brValue == null) m_brValue = getProperty(kPropBrValue); + return m_brValue.get(); + } + + /// Set the white balance to auto. + public synchronized void setWhiteBalanceAuto() { + if (m_wbAuto == null) m_wbAuto = getProperty(kPropWbAuto); + m_wbAuto.set(1); // auto + } + + /// Set the white balance to hold current. + public synchronized void setWhiteBalanceHoldCurrent() { + if (m_wbAuto == null) m_wbAuto = getProperty(kPropWbAuto); + m_wbAuto.set(0); // manual + } + + /// Set the white balance to manual, with specified color temperature. + public synchronized void setWhiteBalanceManual(int value) { + if (m_wbAuto == null) m_wbAuto = getProperty(kPropWbAuto); + m_wbAuto.set(0); // manual + if (m_wbValue == null) m_wbValue = getProperty(kPropWbValue); + m_wbValue.set(value); + } + + /// Set the exposure to auto aperature. + public synchronized void setExposureAuto() { + if (m_exAuto == null) m_exAuto = getProperty(kPropExAuto); + m_exAuto.set(0); // auto; yes, this is opposite of white balance. + } + + /// Set the exposure to hold current. + public synchronized void setExposureHoldCurrent() { + if (m_exAuto == null) m_exAuto = getProperty(kPropExAuto); + m_exAuto.set(1); // manual + } + + /// Set the exposure to manual, as a percentage (0-100). + public synchronized void setExposureManual(int value) { + if (m_exAuto == null) m_exAuto = getProperty(kPropExAuto); + m_exAuto.set(1); // manual + if (value > 100) { + value = 100; + } else if (value < 0) { + value = 0; + } + if (m_exValue == null) m_exValue = getProperty(kPropExValue); + m_exValue.set(value); + } }