之前在https://blog.csdn.net/fengbingchun/article/details/103507754 博文中介绍过如何在Linux获取usb视频设备的vendor id和product id,这里介绍在Windows下获取的方式:
1. 打开设备管理器,如下图所示,内置摄像头显示名为”Integrated Webcam”,插入的usb摄像头显示名为”Logitech HD Webcam C310”:
选中其中一个,如”Logitech HD Webcam C310”,点击右键,选中”属性”点击,–>详细信息,–>属性选择”硬件id”,可获取vid和pid值,如下图所示,十六进制vid为”046D”,pid为”081B”:
2. 通过执行ffmpeg获取,执行命令及执行结果如下图所示:
3. 通过Windows的SetupDiGetDeviceInstanceId函数可以获得所有usb设备的vid和vid,测试code如下:
int get_all_usb_devices_vid_pid)
{GUID *guidDev = GUID*)&GUID_DEVCLASS_USB;HDEVINFO deviceInfoSet = SetupDiGetClassDevsguidDev, NULL, NULL, DIGCF_PRESENT | DIGCF_PROFILE);CHAR buffer[4000];DWORD memberIndex = 0;while true) {SP_DEVINFO_DATA deviceInfoData;ZeroMemory&deviceInfoData, sizeofSP_DEVINFO_DATA));deviceInfoData.cbSize = sizeofSP_DEVINFO_DATA);if SetupDiEnumDeviceInfodeviceInfoSet, memberIndex, &deviceInfoData) == FALSE) {if GetLastError) == ERROR_NO_MORE_ITEMS) {break;}}DWORD nSize = 0;SetupDiGetDeviceInstanceIddeviceInfoSet, &deviceInfoData, buffer, sizeofbuffer), &nSize);buffer[nSize] = '\0';fprintfstdout, "%s\n", buffer);memberIndex++;}if deviceInfoSet) {SetupDiDestroyDeviceInfoListdeviceInfoSet);}return 0;
}
执行结果如下:
4. 提取FFmpeg源码中部分code,测试code如下:
std::unique_ptr<char[]> dup_wchar_to_utf8wchar_t *w, int& len)
{len = WideCharToMultiByteCP_UTF8, 0, w, -1, 0, 0, 0, 0);std::unique_ptr<char[]> snew char[len]);WideCharToMultiByteCP_UTF8, 0, w, -1, s.get), len, 0, 0);return s;
}int parse_stringconst std::string& src, std::string& vid, std::string& pid)
{int pos = src.find"vid_");if pos == std::string::npos) return -1;vid.assignsrc, pos + 4, 4);pos = src.find"pid_");if pos == std::string::npos) return -1;pid.assignsrc, pos + 4, 4);return 0;
}int get_usb_video_devices_vid_pid)
{CoInitialize0);ICreateDevEnum *devenum = NULL;int r = CoCreateInstanceCLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC_SERVER, IID_ICreateDevEnum, reinterpret_cast<void**>&devenum));if r != S_OK) {fprintfstdout, "fail to CoCreateInstance: %d\n", r);return -1;}IEnumMoniker *classenum = NULL;r = ICreateDevEnum_CreateClassEnumeratordevenum, CLSID_VideoInputDeviceCategory, IEnumMoniker **)&classenum, 0);if r != S_OK) {fprintfstdout, "fail to ICreateDevEnum_CreateClassEnumerator: %d\n", r);return -1;}IBaseFilter *device_filter = NULL;IMoniker *m = NULL;while !device_filter && IEnumMoniker_Nextclassenum, 1, &m, NULL) == S_OK) {IPropertyBag *bag = NULL;VARIANT var;r = IMoniker_BindToStoragem, 0, 0, IID_IPropertyBag, void **)&bag);if r != S_OK) {fprintfstdout, "fail to IMoniker_BindToStorage: %d\n", r);return -1;}var.vt = VT_BSTR;r = IPropertyBag_Readbag, L"FriendlyName", &var, NULL);if r != S_OK) {fprintfstdout, "fail to IPropertyBag_Read: %d\n", r);return -1;}int length;auto friendly_name = dup_wchar_to_utf8var.bstrVal, length);fprintfstdout, "friendly_name: %s\n", friendly_name.get));LPMALLOC co_malloc = NULL;r = CoGetMalloc1, &co_malloc);if r != S_OK) {fprintfstdout, "fail to CoGetMalloc: %d\n", r);return -1;}IBindCtx *bind_ctx = NULL;r = CreateBindCtx0, &bind_ctx);if r != S_OK) {fprintfstdout, "fail to CreateBindCtx: %d\n", r);return -1;}LPOLESTR olestr = NULL;r = IMoniker_GetDisplayNamem, bind_ctx, NULL, &olestr);if r != S_OK) {fprintfstdout, "fail to IMoniker_GetDisplayName: %d\n", r);return -1;}auto unique_name = dup_wchar_to_utf8olestr, length);/* replace ':' with '_' since we use : to delineate between sources */std::for_eachunique_name.get), unique_name.get) + length, []char& ch) { if ch == ':') ch = '_'; });fprintfstdout, "unique_name: %s\n", unique_name.get));std::string srcunique_name.get)), vid, pid;parse_stringsrc, vid, pid);unsigned int value_vid, value_pid;std::istringstreamvid) >> std::hex >> value_vid;std::istringstreampid) >> std::hex >> value_pid;fprintfstdout, "usb device name: %s, vid: %s, value: %d; pid: %s, value: %d\n",friendly_name.get), vid.c_str), value_vid, pid.c_str), value_pid);if olestr && co_malloc)IMalloc_Freeco_malloc, olestr);if bind_ctx)IBindCtx_Releasebind_ctx);if bag)IPropertyBag_Releasebag);IMoniker_Releasem);}IEnumMoniker_Releaseclassenum);return 0;
}
执行结果如下:
GitHub:https://github.com//fengbingchun/OpenCV_Test