Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OpenCVGrabber: Make it possible to select a specific device, #528

Merged
merged 2 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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