Skip to content

Commit

Permalink
Merge pull request #528 from nicost/OpenCV
Browse files Browse the repository at this point in the history
OpenCVGrabber: Make it possible to select a specific device,
  • Loading branch information
nicost authored Nov 22, 2024
2 parents 4425e54 + 2ed58f9 commit 7d951c2
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 10 deletions.
2 changes: 1 addition & 1 deletion DeviceAdapters/OpenCVgrabber/DeviceEnumerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ std::map<int, OpenCVDevice> 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;
Expand Down
44 changes: 35 additions & 9 deletions DeviceAdapters/OpenCVgrabber/OpenCVgrabber.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<int, OpenCVDevice> 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";
Expand Down Expand Up @@ -1067,12 +1074,31 @@ int COpenCVgrabber::OnFlipY(MM::PropertyBase* pProp, MM::ActionType eAct)
int COpenCVgrabber::OnCameraID(MM::PropertyBase* pProp, MM::ActionType eAct)
{
#ifdef WIN32
DeviceEnumerator de;
map<int, OpenCVDevice> devices = de.getVideoDevicesMap();
if (eAct == MM::AfterSet)
{
string srcName;
pProp->Get(srcName);
GetPropertyData(cIDName, srcName.c_str(), cameraID_);
for (std::pair<int, OpenCVDevice> device : devices) {
if (device.second.deviceName == srcName) {
cameraID_ = device.second.id;
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<int, OpenCVDevice> 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)
{
Expand Down

0 comments on commit 7d951c2

Please sign in to comment.