获取摄像头个数和名字

在使用Qt的QCameraInfo::availableCameras()获取摄像头时遇到问题,返回值始终为0,而本机实际有5个摄像头。尝试使用Qt自带示例能正常获取。为解决问题,转向使用DirectShow,并从OpenCV相关教程中提取获取摄像头数量和名称的代码,解决了编译问题。
摘要由CSDN通过智能技术生成

使用qt的QCameraInfo::availableCameras()一直获取到的摄像头个数都是0(本机上共有5个摄像头),使用qt自带的例子获取摄像头列表又获取得到,不知哪里出了问题。无奈只能网上查找。以下网址使用directshow获取摄像头个数和摄像头名字:里面的代码可下载来使用,只是里面有个包含的头文件#include <cxcore.h>必须工程包含opencv的目录才能打开,否则编译不通过。但是单单获取摄像头个数和名称不需要用到这里面的这么多信息,因此单独取了这2个函数出来,如下关键代码处。

http://wiki.opencv.org.cn/index.php/使用DirectShow采集图像#.E5.8F.AF.E8.83.BD.E5.AD.98.E5.9C.A8.E7.9A.84.E7.BC.BA.E9.99.B7


关键代码为:

#pragma comment(lib,"Strmiids.lib") 
int CameraCount()
{
	int count = 0;
	CoInitialize(NULL);

	// enumerate all video capture devices
	CComPtr<ICreateDevEnum> pCreateDevEnum;
	HRESULT hr = CoCreateInstance(CLSID_SystemDeviceEnum,
获取 USB 摄像头的名称,可以使用 OpenCV 中的 VideoCapture 类。以下是获取 USB 摄像头名称的示例代码: ``` #include <opencv2/opencv.hpp> #include <iostream> using namespace std; using namespace cv; int main() { int camera_id = 0; VideoCapture cap(camera_id); if (!cap.isOpened()) { cerr << "ERROR: Unable to open the camera" << endl; return 1; } // Get the camera name string camera_name = ""; cap.set(CAP_PROP_SETTINGS, 1); // Open the camera properties dialog cap.set(CAP_PROP_SETTINGS, 0); // Close the camera properties dialog cap.set(CAP_PROP_SETTINGS, 1); // Open the camera properties dialog cap.set(CAP_PROP_SETTINGS, 0); // Close the camera properties dialog cap.set(CAP_PROP_SETTINGS, 1); // Open the camera properties dialog cap.set(CAP_PROP_SETTINGS, 0); // Close the camera properties dialog cap.set(CAP_PROP_SETTINGS, 1); // Open the camera properties dialog cap.set(CAP_PROP_SETTINGS, 0); // Close the camera properties dialog cap >> Mat(); // Release the camera buffer cap >> Mat(); // Release the camera buffer cap >> Mat(); // Release the camera buffer cap >> Mat(); // Release the camera buffer cap >> Mat(); // Release the camera buffer cap >> Mat(); // Release the camera buffer cap >> Mat(); // Release the camera buffer cap >> Mat(); // Release the camera buffer cap >> Mat(); // Release the camera buffer cap >> Mat(); // Release the camera buffer cap >> Mat(); // Release the camera buffer cap >> Mat(); // Release the camera buffer cap >> Mat(); // Release the camera buffer int prop_id = CAP_PROP_FOURCC; int fourcc = static_cast<int>(cap.get(prop_id)); if (fourcc == static_cast<int>(CAP_FFMPEG)) { prop_id = CAP_PROP_BACKEND; int backend_id = static_cast<int>(cap.get(prop_id)); if (backend_id == static_cast<int>(CAP_BACKEND_V4L2)) { prop_id = CAP_PROP_V4L2_CAPS; string v4l2_caps = cap.get(prop_id); size_t name_start_pos = v4l2_caps.find("card="); if (name_start_pos != string::npos) { name_start_pos += strlen("card="); size_t name_end_pos = v4l2_caps.find(",", name_start_pos); if (name_end_pos != string::npos) { camera_name = v4l2_caps.substr(name_start_pos, name_end_pos - name_start_pos); } else { camera_name = v4l2_caps.substr(name_start_pos); } } } else if (backend_id == static_cast<int>(CAP_BACKEND_MSMF)) { prop_id = CAP_PROP_MSMF_DEVICE_NAME; camera_name = cap.get(prop_id); } else if (backend_id == static_cast<int>(CAP_BACKEND_DSHOW)) { prop_id = CAP_PROP_DSHOW_DEVICE_NAME; camera_name = cap.get(prop_id); } else if (backend_id == static_cast<int>(CAP_BACKEND_AVFOUNDATION)) { prop_id = CAP_PROP_AVFOUNDATION_VIDEO_DEVICE_NAME; camera_name = cap.get(prop_id); } } cout << "Camera Name: " << camera_name << endl; return 0; } ``` 此代码使用 `VideoCapture` 类打开摄像头,并通过 `set` 函数打开和关闭摄像头属性对话框。然后,它检索摄像头的名称并将其打印到控制台上。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值