#include "hkcamera.h" #include using namespace std; hkcamera::hkcamera(QObject *parent) : QObject(parent) { } BOOL hkcamera::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(); //初始化SDK bool isok = NET_DVR_Init(); if (isok == false) { std::cout << "NET_DVR_Init error;error number is " << NET_DVR_GetLastError(); return false; } //设置连接时间与重连时间 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); //long userID = NET_DVR_Login("192.168.1.64", 8000, "admin", "a12345678", &DeviceInfoTmp); if (userID < 0) { std::cout << "NET_DVR_Login error;" << "error number is " << NET_DVR_GetLastError(); return false; } std::cout << "Login userID:" << userID; m_userid = userID; return true; } BOOL hkcamera::displaypic(HWND hWnd) { NET_DVR_PREVIEWINFO 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(m_userid, &struPlayInfo, NULL, NULL); if (IRealPlayHandle < 0) { std::cout << "NET_DVR_RealPlay_V40 error;error number " << NET_DVR_GetLastError(); NET_DVR_Logout(m_userid); m_userid = 0; return false; } else { m_playhandle = IRealPlayHandle; return true; } } void hkcamera::stopdisplaypic() { NET_DVR_StopRealPlay(m_playhandle); return; } BOOL hkcamera::savepic(QString filename) { if(-1 == m_playhandle) { return false; } QString capImagePath = filename; QDir m_capImagePath(capImagePath); if (m_capImagePath.exists()) { QFile::remove(capImagePath); } char* savefilename; QByteArray bytefilename = filename.toLatin1(); savefilename = bytefilename.data(); //保存为jpg格式 // NET_DVR_SetCapturePictureMode(JPEG_MODE);//BMP_MODE NET_DVR_SetCapturePictureMode(BMP_MODE); bool pic_status = NET_DVR_CapturePicture(m_playhandle, savefilename); if (pic_status) { std::cout << "camera_SavePicture save pic :" << savefilename << "success"; } else { std::cout << "camera_SavePicture save pic :" << savefilename << "false"; } return pic_status; } void hkcamera::logout() { if(0 != m_userid) { NET_DVR_Logout(m_userid); m_userid = 0; m_playhandle = -1; } }