QT c++ 海康红外热像仪

//本文描述2通道海康通道红外热像仪预览和抓图

#include "mainwindow.h"

#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)

: QMainWindow(parent)

, ui(new Ui::MainWindow)

{

ui->setupUi(this);

userID=-1;

previewID=-1;

if(initSDK())

qDebug()<<"SDK init Success!\n";

}

long MainWindow::play(HWND hWnd, NET_DVR_PREVIEWINFO &struPlayInfo)

{

struPlayInfo={0};

struPlayInfo.hPlayWnd = hWnd;//需要 SDK 解码时句柄设为有效值,仅取流不解码时可设为空

struPlayInfo.lChannel = 1;//预览通道号

struPlayInfo.dwStreamType = 0;//码流类型:0-主码流,1-子码流,2-三码流,3-虚拟码流,以此类推

struPlayInfo.dwLinkMode = 0;//0- TCP 方式,1- UDP 方式,2- 多播方式,3- RTP 方式,4-RTP/RTSP,5-RSTP/HTTP

struPlayInfo.bBlocked = 1;//0- 非阻塞取流,1- 阻塞取流

long IRealPlayHandle = NET_DVR_RealPlay_V40(userID, &struPlayInfo, NULL, NULL);

if(IRealPlayHandle <0)

{

qDebug() << "NET_DVR_RealPlay_V40 error;error number " <<NET_DVR_GetLastError();

QMessageBox::warning(this,"error","NET_DVR_RealPlay_V40 error;error number "+QString::number(NET_DVR_GetLastError()));

NET_DVR_Logout(userID);

userID=-1;

return -1;

}

return IRealPlayHandle;

}

long MainWindow::play2(HWND hWnd, NET_DVR_PREVIEWINFO &struPlayInfo)

{

struPlayInfo={0};

struPlayInfo.hPlayWnd = hWnd;//需要 SDK 解码时句柄设为有效值,仅取流不解码时可设为空

struPlayInfo.lChannel = 2;//预览通道号

struPlayInfo.dwStreamType = 0;//码流类型:0-主码流,1-子码流,2-三码流,3-虚拟码流

struPlayInfo.dwLinkMode = 0;//0- TCP 方式,1- UDP 方式,2- 多播方式,3- RTP 方式

//4-RTP/RTSP,5-RSTP/HTTP

struPlayInfo.bBlocked = 1;//0- 非阻塞取流,1- 阻塞取流

long IRealPlayHandle = NET_DVR_RealPlay_V40(userID, &struPlayInfo, NULL, NULL);

if(IRealPlayHandle <0)

{

qDebug() << "NET_DVR_RealPlay_V40 error;error number " <<NET_DVR_GetLastError();

QMessageBox::warning(this,"error","NET_DVR_RealPlay_V40 error;error number "+QString::number(NET_DVR_GetLastError()));

NET_DVR_Logout**(userID);**

userID=-1;

return -1;

}

return IRealPlayHandle;

}

bool MainWindow::initSDK()

{

bool isok = NET_DVR_Init();

if(isok == false)

{

qDebug()<< "NET_DVR_Init error;error number is " <<NET_DVR_GetLastError();

QMessageBox::warning(this,"error","NET_DVR_Init error;error number is "+QString::number(NET_DVR_GetLastError()));

return isok;

}

//设置连接时间与重连时间

NET_DVR_SetConnectTime(2000, 1);

NET_DVR_SetReconnect(10000, true);

return isok;

}

bool MainWindow::uninitSDK()//好像没啥效果

{

bool isok = NET_DVR_Cleanup();

if(isok == false)

{

qDebug()<< "NET_DVR_Cleanup error;error number is " <<NET_DVR_GetLastError();

QMessageBox::warning(this,"error","NET_DVR_Cleanup error;error number "+QString::number(NET_DVR_GetLastError()));

return isok;

}

return isok;

}

MainWindow::~MainWindow()

{ uninitSDK();

delete ui;

}

void MainWindow::on_PreviewButton_clicked()

{

Preview_Channel1();

Preview_Channel2();

}

void MainWindow::Preview_Channel1()//通道1

{

if(userID==-1)

{

qDebug() <<"Need Login!\n";

QMessageBox::warning(this,"error","Need Login!");

return;

}

HWND hWnd = (HWND)ui->label ->winId();//显示到label

NET_DVR_PREVIEWINFO struPlayInfo;

previewID = play(hWnd, struPlayInfo);

if(previewID==-1)

{

return;

}

qDebug()<<"channel 1 Preview Success!" << userID;

}

void MainWindow::Preview_Channel2()//通道2

{

if(userID==-1)

{

qDebug() <<"Need Login!\n";

QMessageBox::warning(this,"error","Need Login!");

return;

}

HWND hWnd = (HWND)ui->label2->winId();//显示到label2

NET_DVR_PREVIEWINFO struPlayInfo;

previewID2 = play2(hWnd, struPlayInfo);

if(previewID==-1)

{

return;

}

qDebug()<<"channel 2 Preview Success!" << userID;

}

void MainWindow::on_LoginButton_clicked()

{

NET_DVR_DEVICEINFO_V30 DeviceInfoTmp;

QSettings settings("./cameraConfig.ini", QSettings::IniFormat);

QSettings::Status status = settings.status();

// 获取指定键值示例

QString Ip = settings.value("ip_address/ip","192.168.6.208").toString();

int Port = settings.value("ip_address/port",8000).toInt();

userID=NET_DVR_Login_V30(Ip.toUtf8().data(),Port,(char *)"admin",(char *)"123456",&DeviceInfoTmp);//用户名和密码,读者可以加到配置文件里

if(userID<0)

{

qDebug() << "NET_DVR_Login_V30 error;" << "error number is " << NET_DVR_GetLastError();

QMessageBox::warning(this,"error","NET_DVR_Login_V30 error;error number "+QString::number(NET_DVR_GetLastError()));

return;

}

qDebug()<<"Login Success,userID:" << userID;

qDebug()<<"Login Success,userID:\n" ;

}

void MainWindow::on_StopPreviewButton_clicked()//两通道一起停

{

if(previewID!=-1)

{

if**(NET_DVR_StopRealPlay**(previewID)==false)

{

qDebug() << "NET_DVR_StopRealPlay error;" << "error number is " << NET_DVR_GetLastError();

QMessageBox::warning(this,"error","NET_DVR_StopRealPlay error;error number "+QString::number(NET_DVR_GetLastError()));

return;

}

previewID=-1;

qDebug() <<"Stop Preview1 Success!\n";

}

//------------------------

if(previewID2!=-1)

{

if(NET_DVR_StopRealPlay(previewID2)==false)

{

qDebug() << "NET_DVR_StopRealPlay error;" << "error number is " << NET_DVR_GetLastError();

QMessageBox::warning(this,"error","NET_DVR_StopRealPlay error;error number "+QString::number(NET_DVR_GetLastError()));

return;

}

previewID2=-1;

qDebug() <<"Stop Preview2 Success!\n";

}

}

void MainWindow::on_CaptureButton_clicked()//抓图

{

//channel1

if(userID!=-1)

{

NET_DVR_JPEGPARA IpJpegPara2;

IpJpegPara2.wPicQuality=0;

IpJpegPara2.wPicSize=0xff;

QDateTime current_date_time =QDateTime::currentDateTime();//当前时间

QString current_date =current_date_time.toString("yyyy-MM-dd_hh-mm-ss_zzz");//转换格式

current_date="d:\\Capture_Pic\\"+current_date+".jpg";//jpg格式

char* fileName;

QByteArray ba = current_date.toLatin1(); //

fileName=ba.data();

//channel2

if(NET_DVR_CaptureJPEGPicture(userID,1,&IpJpegPara2,fileName)==false)

{

qDebug() << "NET_DVR_CaptureJPEGPicture error;" << "error number is " << NET_DVR_GetLastError();

QMessageBox::warning(this,"error","NET_DVR_CaptureJPEGPicture error;error number "+QString::number(NET_DVR_GetLastError()));

return;

}

qDebug() <<"Capture Success!\n";

}

}

void MainWindow::on_LogoutButton_clicked()//好像没啥用,保留着

{

if(userID!=-1)

{

if(NET_DVR_Logout(userID)==false)

{

qDebug() << "NET_DVR_Logout error;" << "error number is " << NET_DVR_GetLastError();

QMessageBox::warning(this,"error","NET_DVR_Logout error;error number "+QString::number(NET_DVR_GetLastError()));

return;

}

userID=-1;

qDebug() <<"Logout Success!\n";

}

}

相关推荐
冰糖雪梨dd1 分钟前
JS中new的过程发生了什么
开发语言·javascript·原型模式
川石课堂软件测试40 分钟前
全链路Controller压测负载均衡
android·运维·开发语言·python·mysql·adb·负载均衡
杨福瑞1 小时前
C语言⽂件操作讲解(总)
c语言·开发语言
hz_zhangrl2 小时前
CCF-GESP 等级考试 2025年9月认证C++四级真题解析
开发语言·c++·算法·程序设计·gesp·c++四级·gesp2025年9月
止水编程 water_proof2 小时前
Java--网络编程(二)
java·开发语言·网络
润 下3 小时前
C语言——深入解析C语言指针:从基础到实践从入门到精通(三)
c语言·开发语言·经验分享·笔记·学习·程序人生·其他
知白守黑2673 小时前
docker网络
开发语言·php
细节控菜鸡3 小时前
【2025最新】ArcGIS for JS 范围裁剪(只保留特定区域显示),实现精准地理范围聚焦
开发语言·javascript·arcgis
一根甜苦瓜4 小时前
Go语言Slice的一道骚题
开发语言·后端·golang
驰羽4 小时前
[GO]Go语言泛型详解
开发语言·golang·xcode