程序说明:
程序将海康摄像头开发需要进行的操作封面到类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 <opencv2\core\core.hpp>
#include <opencv2\highgui\highgui.hpp>
#include <opencv2\imgproc\imgproc.hpp>
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;
};
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
HK_camera.cpp
#include "HK_camera.h"
#include <iostream>
//全局变量
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");
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
main()函数调用方法:
#include "HK_camera.h"
#include <iostream>
#include <Windows.h>
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" <<endl;
camera.show();
}
else
{
cout << "login fail" << endl;
}
}
else
{
cout << "init fail" <<endl;
}
while(1)
{
}
return 0;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
补充:希望可以给大家开发带来便利和收获,谢谢您的阅读。