海康摄像头实时读取+opencv显示

程序说明:
程序将海康摄像头开发需要进行的操作封面到类HK_camera中,使用只需要在mian()函数中实例化HK_camera,将摄像头的IP,用户名,密码给对象即可使用。
程序调用接口的流程都是参考SDK开发文档,并做了简单的修改,有不明白或者觉得不对的地方希望可以给我留言,谢谢。


环境:
摄像机型号:CS-C1-11WPFR
电脑环境:32位
opencv版本:opencv 2.4.9
网络SDK开发包版本:CH-HCNetSDK(Windows32)V5.2.7.5_build20170217
播放SDK开发包版本:PlayCtrl_V7.3.2.50_win32_Build20160816


SDK说明:
播放和网络SDK都是我在官网上下载的,文件里有关于库的说明文档,还有实例程序的说明文档。


程序:

HK_camera.h

#pragma once

#include "include\HCNetSDK.h"
#include "include\PlayM4.h"
#include "include\plaympeg4.h"
#include 
#include 
#include 

using namespace cv;
using namespace std;

class HK_camera
{
public:
    HK_camera(void);
    ~HK_camera(void);

public:
    bool Init();                  //初始化
    bool Login(char* sDeviceAddress, char* sUserName, char* sPassword, WORD wPort);            //登陆
    void show();                  //显示图像

private:
    LONG lUserID;

};

HK_camera.cpp

#include "HK_camera.h"
#include 

//全局变量
LONG g_nPort;
Mat g_BGRImage;
void CALLBACK DecCBFun(long nPort, char* pBuf, long nSize, FRAME_INFO* pFrameInfo, long nUser, long nReserved2)
{
    if(pFrameInfo->nType == T_YV12)
    {
        std::cout << "the frame infomation is T_YV12" << std::endl;
        if(g_BGRImage.empty())
        {
            g_BGRImage.create(pFrameInfo->nHeight, pFrameInfo->nWidth, CV_8UC3);
        }
        Mat YUVImage(pFrameInfo->nHeight + pFrameInfo->nHeight/2, pFrameInfo->nWidth, CV_8UC1, (unsigned char*)pBuf);

        cvtColor(YUVImage, g_BGRImage, COLOR_YUV2BGR_YV12);
        imshow("RGBImage", g_BGRImage);
        waitKey(15);

        YUVImage.~Mat();
    }
}

void CALLBACK g_RealDataCallBack_V30(LONG lPlayHandle, DWORD dwDataType, BYTE *pBuffer, DWORD dwBufSize, void* pUser)
{
    if(dwDataType == NET_DVR_STREAMDATA)
    {
        if(dwBufSize > 0 && g_nPort != -1)
        {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                if(!PlayM4_InputData(g_nPort, pBuffer, dwBufSize))
            {
                std::cout << "fail input data" << std::endl;
            }
            else
            {
                std::cout << "success input data" << std::endl;
            }

        }
    }
}
HK_camera::HK_camera(void)
{

}

HK_camera::~HK_camera(void)
{
}

bool HK_camera::Init()
{
    if(NET_DVR_Init())
    {
        return true;
    }
    else
    {
        return false;
    }
}

bool HK_camera::Login(char* sDeviceAddress, char* sUserName, char* sPassword, WORD wPort)
{
    NET_DVR_USER_LOGIN_INFO pLoginInfo = {0};
    NET_DVR_DEVICEINFO_V40 lpDeviceInfo = {0};

    pLoginInfo.bUseAsynLogin = 0;     //同步登录方式
    strcpy(pLoginInfo.sDeviceAddress, sDeviceAddress);
    strcpy(pLoginInfo.sUserName, sUserName);
    strcpy(pLoginInfo.sPassword, sPassword);
    pLoginInfo.wPort = wPort;

    lUserID = NET_DVR_Login_V40(&pLoginInfo, &lpDeviceInfo);

    if(lUserID < 0)
    {
        return false;
    }
    else
    {
        return true;
    }
}

void HK_camera::show()
{
    if(PlayM4_GetPort(&g_nPort))            //获取播放库通道号
    {
        if(PlayM4_SetStreamOpenMode(g_nPort, STREAME_REALTIME))      //设置流模式
        {
            if(PlayM4_OpenStream(g_nPort, NULL, 0, 1024*1024))         //打开流
            {
                if(PlayM4_SetDecCallBackExMend(g_nPort, DecCBFun, NULL, 0, NULL))
                {
                     if(PlayM4_Play(g_nPort, NULL))
                     {
                         std::cout << "success to set play mode" << std::endl;
                     }
                     else
                     {
                         std::cout << "fail to set play mode" << std::endl;
                     }
                }
                else
                {
                     std::cout << "fail to set dec callback " << std::endl;
                }
            } 
            else
            {
                std::cout << "fail to open stream" << std::endl;
            }
        }
        else
        {
            std::cout << "fail to set stream open mode" << std::endl;
        }
    }
    else
    {
        std::cout << "fail to get port" << std::endl;
    }

    NET_DVR_PREVIEWINFO struPlayInfo = {0};
    struPlayInfo.hPlayWnd = NULL;
    struPlayInfo.lChannel = 1;
    struPlayInfo.dwStreamType = 0;
    struPlayInfo.dwLinkMode = 0;
    struPlayInfo.bBlocked = 1;

    if(NET_DVR_RealPlay_V40(lUserID, &struPlayInfo, g_RealDataCallBack_V30, NULL))
    {
        namedWindow("RGBImage");
    }
}

main()函数调用方法:

#include "HK_camera.h"
#include 
#include 

using namespace std;

int main()
{
    HK_camera camera;
    if(camera.Init())
    {
        cout << "init success" << endl;
        if(camera.Login("192.168.0.15", "admin", "ULCGUX", 8000))
        {
            cout << "login success" <else
        {
            cout << "login fail" << endl;
        }
    }
    else
    {
        cout << "init fail" <while(1)
    {

    }
    return 0;
}

补充:希望可以给大家开发带来便利和收获,谢谢您的阅读。

你可能感兴趣的:(摄像头SDK开发)