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 " <         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 " <         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 " <         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 " <         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";
    }
}

你可能感兴趣的:(QT,测控,qt,开发语言)