海康威视网络摄像头SDK二次开发(QT版本)

暂时使用到的主要功能有四个:1.连接相机,2.实时显示,3.取图(bmp格式),4.关闭相机

在官网下载对应的win64的SDK,在库文件中运行demo.exe确保硬件连接正常。
在程序中导入库

1. 连接相机

使用到的主要SDK接口:NET_DVR_Init(初始化SDK),NET_DVR_Login(用户登录)

    //连接相机,连接成功返回userID,返回-1为初始化SDK失败,返回-2为登录失败
    Q_INVOKABLE int camera_Connect(QString ip, int port = 8000, QString name = "admin", QString password = "*****");
    
    private:
    Ui::MainWidget *ui;
    int m_userID=-10;				//记录连接是否成功,成功则返回正确ID
    bool m_Connect = false;
    LONG m_IRealPlayHandle=-100;    //记录显示是否成功,成功则返回正确句柄


int MainWidget::camera_Connect(QString ip, int port, QString name, QString password)
{
    //QString 转 char * 
    // ip name 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)
    {
        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);
    //long userID = NET_DVR_Login("192.168.1.64", 8000, "admin", "a12345678", &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:
    //实时显示在控件上(hWnd传入控件ID)
    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();	//成功时,即可实时显示在该label上
        NET_DVR_PREVIEWINFO struPlayInfo = { 0 };   //初始化
        //此为登录时返回的ID
        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;//需要 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();
        NET_DVR_Logout(userID);
        return 0;
    }
    else
        return IRealPlayHandle;
}

3. 取图(bmp格式)

使用到的SDK接口:NET_DVR_CapturePicture

    //保存图片:第二个参数是显示时返回的m_IRealPlayHandle(也可能是连接时返回的m_userID,测试时这两个值都可行)
    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;
}

借鉴博客:源博客链接,借鉴此博客完成连接和实时显示的功能。

你可能感兴趣的:(#,QT,C++,qt,网络,后端)