海康摄像机库函数连接及显示视频
发表于 : 周六 8月 17, 2024 6:43 pm
连接需要摄像机的:IP地址、端口号、用户名、登录密码。
开发包下载:
开发包下载:
Code: [全选] [Expand/Collapse]
- void MainWindow::on_btn_dvr_connect_clicked()
- {
- if(!dvr_connected) {
- // 1.初始化海康监控SDK
- bool isok = NET_DVR_Init();
- if(isok) {
- qDebug() << "SDK初始化成功!";
- loger("1.海康SDK初始化成功!连接摄像头:" + ui->edit_dvr_host->text() + ":" + ui->edit_dvr_post->text());
- //设置连接时间与重连时间
- // NET_DVR_SetConnectTime(3000, 3);
- // NET_DVR_SetReconnect(30000, true);
- // 2.设置连接相机的信息,用户注册设备
- NET_DVR_USER_LOGIN_INFO struLoginInfo; //包含相机参数的结构体
- NET_DVR_DEVICEINFO_V40 struDeviceInfoV40; //登录后的相机信息
- struLoginInfo.bUseAsynLogin = 0; //同步登录方式
- strcpy_s(struLoginInfo.sDeviceAddress, ui->edit_dvr_host->text().toLatin1()); //设备IP地址
- struLoginInfo.wPort = ui->edit_dvr_post->text().toInt(); //设备服务端口
- strcpy_s(struLoginInfo.sUserName, ui->edit_dvr_user->text().toLatin1()); //设备登录用户名
- strcpy_s(struLoginInfo.sPassword, ui->edit_dvr_password->text().toLatin1()); //设备登录密码
- lUserID = NET_DVR_Login_V40(&struLoginInfo, &struDeviceInfoV40); //设备登录
- if(lUserID < 0) {
- qDebug() << "登录失败: " << NET_DVR_GetLastError();
- loger(" ...摄像头连接失败,错误代码:" + QString::number(NET_DVR_GetLastError()));
- NET_DVR_Cleanup();//释放 SDK 资源
- return;
- }
- else {
- qDebug() << "登录成功 ";
- loger(" ...摄像头连接成功!");
- // HWND hWnd = (HWND)ui->graphicsView->winId(); //获取窗口句柄
- HWND hWnd = (HWND)ui->graphicsView->winId(); //获取窗口句柄
- NET_DVR_PREVIEWINFO struPlayInfo;
- struPlayInfo.hPlayWnd = hWnd; //需要 SDK 解码时句柄设为有效值,仅取流不解码时可设为空
- struPlayInfo.lChannel = 1; //预览通道号
- struPlayInfo.dwStreamType = 0; //0-主码流,1-子码流,2-码流 3,3-码流 4,以此类推
- struPlayInfo.dwLinkMode = 0; //0- TCP 方式,1- UDP 方式,2- 多播方式,3- RTP 方式,4-RTP/RTSP,5-RSTP/HTTP
- struPlayInfo.bBlocked = 1; //0- 非阻塞取流,1- 阻塞取流
- lRealPlayHandle = NET_DVR_RealPlay_V40(lUserID, &struPlayInfo, nullptr, nullptr);
- if(lRealPlayHandle < 0) {
- qDebug() << "预览失败: " << NET_DVR_GetLastError();
- loger(" ....图像预览失败,错误代码:" + QString::number(NET_DVR_GetLastError()));
- NET_DVR_Logout(lUserID);
- NET_DVR_Cleanup();
- return;
- }
- ui->btn_dvr_connect->setText("关闭摄像头");
- dvr_connected = true;
- }
- }
- else {
- qDebug() << "SDK初始化失败: " << NET_DVR_GetLastError();
- loger(">海康SDK初始化失败,错误代码:" + QString::number(NET_DVR_GetLastError()));
- }
- }
- else {
- //关闭预览
- NET_DVR_StopRealPlay(lRealPlayHandle);
- //注销用户
- NET_DVR_Logout(lUserID);
- NET_DVR_Cleanup();
- loger(">断开与摄像头连接。");
- ui->btn_dvr_connect->setText("连接摄像头");
- dvr_connected = false;
- }
- }