暂时使用到的主要功能有四个:1.连接相机,2.实时显示,3.取图(bmp格式),4.关闭相机
在官网下载对应的win64的SDK,在库文件中运行demo.exe确保硬件连接正常。
在程序中导入库
1. 连接相机
使用到的主要SDK接口:NET_DVR_Init(初始化SDK),NET_DVR_Login(用户登录)
Q_INVOKABLE int camera_Connect(QString ip, int port = 8000, QString name = "admin", QString password = "*****");
private:
Ui::MainWidget *ui;
int m_userID=-10;
bool m_Connect = false;
LONG m_IRealPlayHandle=-100;
int MainWidget::camera_Connect(QString ip, int port, QString name, QString password)
{
char* ch_ip;
char* ch_name;
char* ch_password;
QByteArray ba_ip = ip.toLatin1();
QByteArray ba_name = name.toLatin1();
QByteArray ba_password = password.toLatin1();
ch_ip = ba_ip.data();
ch_name = ba_name.data();
ch_password = ba_password.data();
bool isok = NET_DVR_Init();
if (isok == false)
{
qDebug() << "NET_DVR_Init error;error number is " << NET_DVR_GetLastError();
return -1;
}
NET_DVR_SetConnectTime(2000, 1);
NET_DVR_SetReconnect(10000, true);
NET_DVR_DEVICEINFO DeviceInfoTmp;
long userID = NET_DVR_Login(ch_ip, port, ch_name, ch_password, &DeviceInfoTmp);
if (userID < 0)
{
qDebug() << "NET_DVR_Login error;" << "error number is " << NET_DVR_GetLastError();
return -2;
}
qDebug() << "Login userID:" << userID;
m_Connect = true;
m_userID = userID;
return userID;
}
2. 实时显示
需要调用实时显示之后,后续才能保存图片成功
使用到的主要的SDK接口:NET_DVR_RealPlay_V40
public:
Q_INVOKABLE bool camera_DisPlayCapture();
private:
long display_pic(long userID, HWND hWnd, NET_DVR_PREVIEWINFO struPlayInfo);
bool MainWidget::camera_DisPlayCapture()
{
if (m_userID >= 0)
{
HWND hWnd = (HWND)ui->label->winId();
NET_DVR_PREVIEWINFO struPlayInfo = { 0 };
long userID = m_userID;
long IRealPlayHandle = display_pic(userID, hWnd, struPlayInfo);
qDebug() << "camera_DisPlayCapture:IRealPlayHandle=" << IRealPlayHandle;
m_IRealPlayHandle = IRealPlayHandle;
return true;
}
else
{
qDebug() << ("HKCAMERA->camera_DisPlayCapture:m_userID < 0,not Connect");
return false;
}
}
long MainWidget::display_pic(long userID, HWND hWnd, NET_DVR_PREVIEWINFO struPlayInfo)
{
struPlayInfo = { 0 };
struPlayInfo.hPlayWnd = hWnd;
struPlayInfo.lChannel = 1;
struPlayInfo.dwStreamType = 0;
struPlayInfo.dwLinkMode = 0;
struPlayInfo.bBlocked = 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();
NET_DVR_Logout(userID);
return 0;
}
else
return IRealPlayHandle;
}
3. 取图(bmp格式)
使用到的SDK接口:NET_DVR_CapturePicture
Q_INVOKABLE bool camera_SavePicture(QString filename, int IrealHandl);
bool MainWidget::camera_SavePicture(QString filename, int IrealHandl)
{
if (!m_Connect)
return false;
if (m_IRealPlayHandle < 0)
return false;
QString capImagePath = filename;
QDir m_capImagePath(capImagePath);
if (m_capImagePath.exists())
QFile::remove(capImagePath);
char* ch_filename;
QByteArray ba_filename = filename.toLatin1();
ch_filename = ba_filename.data();
bool pic_status = NET_DVR_CapturePicture(IrealHandl, ch_filename);
if (pic_status)
{
qDebug() << "camera_SavePicture save pic :" << filename << "success";
}
else
{
qDebug() << "camera_SavePicture save pic :" << filename << "false";
}
return pic_status;
}
4. 关闭相机
使用到的SDK的接口:NET_DVR_Logout,NET_DVR_Cleanup
bool MainWidget::camera_Disconnect()
{
m_Connect = false;
bool status1 = NET_DVR_Logout(m_userID);
bool status2 = NET_DVR_Cleanup();
return status1&& status2;
}
借鉴博客:源博客链接,借鉴此博客完成连接和实时显示的功能。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)