From 99c54ab37f185dc33ce73ff0dd68519a4543af6c Mon Sep 17 00:00:00 2001 From: Nico Stuurman Date: Thu, 21 Nov 2024 15:51:35 -0800 Subject: [PATCH 1/2] OpenCVGrabber: Make it possible to select a specific device, uses a pre-initialization property. --- .../OpenCVgrabber/DeviceEnumerator.cpp | 2 +- .../OpenCVgrabber/OpenCVgrabber.cpp | 41 +++++++++++++++---- 2 files changed, 33 insertions(+), 10 deletions(-) diff --git a/DeviceAdapters/OpenCVgrabber/DeviceEnumerator.cpp b/DeviceAdapters/OpenCVgrabber/DeviceEnumerator.cpp index c1bbc3c3c..9ac325320 100644 --- a/DeviceAdapters/OpenCVgrabber/DeviceEnumerator.cpp +++ b/DeviceAdapters/OpenCVgrabber/DeviceEnumerator.cpp @@ -40,7 +40,7 @@ std::map DeviceEnumerator::getDevicesMap(const GUID deviceCla while (pEnum->Next(1, &pMoniker, NULL) == S_OK) { IPropertyBag *pPropBag; - HRESULT hr = pMoniker->BindToStorage(0, 0, IID_PPV_ARGS(&pPropBag)); + hr = pMoniker->BindToStorage(0, 0, IID_PPV_ARGS(&pPropBag)); if (FAILED(hr)) { pMoniker->Release(); continue; diff --git a/DeviceAdapters/OpenCVgrabber/OpenCVgrabber.cpp b/DeviceAdapters/OpenCVgrabber/OpenCVgrabber.cpp index acdaf1299..9a4445ca1 100644 --- a/DeviceAdapters/OpenCVgrabber/OpenCVgrabber.cpp +++ b/DeviceAdapters/OpenCVgrabber/OpenCVgrabber.cpp @@ -162,16 +162,23 @@ COpenCVgrabber::COpenCVgrabber() : SetErrorText(CAMERA_NOT_INITIALIZED, "Camera was not initialized"); #ifdef WIN32 - CPropertyAction* pAct = new CPropertyAction(this, &COpenCVgrabber::OnCameraID); - CreateProperty(cIDName, "Undefined", MM::String, false, pAct, true); - DeviceEnumerator de; - // Video Devices map devices = de.getVideoDevicesMap(); - - for (int i = 0; i++; devices.size()) - { - AddAllowedValue(cIDName, devices.at(i).deviceName.c_str(), long(i)); + CPropertyAction* pAct = new CPropertyAction(this, &COpenCVgrabber::OnCameraID); + size_t nrDevices = devices.size(); + std::string firstChoise = nrDevices > 0 ? devices.at(0).deviceName : "Undefined"; + CreateProperty(cIDName, firstChoise.c_str(), MM::String, false, pAct, true); + std::ostringstream os; + os << "Devices map size " << devices.size(); + LogMessage(os.str().c_str(), false); + if (nrDevices > 0) { + for (int i = 0; i < nrDevices; i++) + { + AddAllowedValue(cIDName, devices.at(i).deviceName.c_str()); + } + } + else { + AddAllowedValue(cIDName, "Undefined"); } #else String cIDNameReally = "Camera Number"; @@ -1067,12 +1074,28 @@ int COpenCVgrabber::OnFlipY(MM::PropertyBase* pProp, MM::ActionType eAct) int COpenCVgrabber::OnCameraID(MM::PropertyBase* pProp, MM::ActionType eAct) { #ifdef WIN32 + DeviceEnumerator de; + map devices = de.getVideoDevicesMap(); if (eAct == MM::AfterSet) { string srcName; pProp->Get(srcName); - GetPropertyData(cIDName, srcName.c_str(), cameraID_); + for (std::pair device : devices) { + if (device.second.deviceName == srcName) { + cameraID_ = device.second.id; + return DEVICE_OK; + } + } + } + else if (eAct == MM::BeforeGet) { + for (std::pair device : devices) { + if (device.second.id == cameraID_) { + pProp->Set(device.second.deviceName.c_str()); + return DEVICE_OK; + } + } } + return DEVICE_NOT_CONNECTED; #else if (eAct == MM::AfterSet) { From 2ed58f979d92ccf41f813790305d7bbd913a31dc Mon Sep 17 00:00:00 2001 From: Nico Stuurman Date: Thu, 21 Nov 2024 15:55:07 -0800 Subject: [PATCH 2/2] OpenCVGrabber: attempt to stay backward compatible. --- DeviceAdapters/OpenCVgrabber/OpenCVgrabber.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/DeviceAdapters/OpenCVgrabber/OpenCVgrabber.cpp b/DeviceAdapters/OpenCVgrabber/OpenCVgrabber.cpp index 9a4445ca1..4751bd195 100644 --- a/DeviceAdapters/OpenCVgrabber/OpenCVgrabber.cpp +++ b/DeviceAdapters/OpenCVgrabber/OpenCVgrabber.cpp @@ -1086,6 +1086,9 @@ int COpenCVgrabber::OnCameraID(MM::PropertyBase* pProp, MM::ActionType eAct) return DEVICE_OK; } } + // fallback, to not throw a fit with old config files + cameraID_ = 0; + return DEVICE_OK; } else if (eAct == MM::BeforeGet) { for (std::pair device : devices) {