• 海康相机SDK二次开发C++程序


    海康相机在VS中如何进行连接调用呢(软触发调用)
    1.查找海康相机网口;
    2.调用海康相机提供的接口,连接海康相机;
    3.初始化相机;
    (安装海康相机软件)

    #pragma once
    #include "CameraParams.h"
    
    
    
    #include "halcon\include\halconcpp\HalconCpp.h"
    
    #define MAX_CAMERA 10
    
    class Ts_HiKCamera
    {
    public:
    	Ts_HiKCamera(void);
    	~Ts_HiKCamera(void);
    
    public:
    	bool PreInit();
    	bool Terminate();
    	LPVOID GetGigEInstantCameraForUserDefinedName(_In_ int index);
    
    
    public:
    	bool GrabImage(_In_ int index, _Out_ HalconCpp::HObject* ho_image);
    	void SetExposureTime(_In_ int index, _In_ const int& expouser_time);
    	int GetExposureTime(_In_ int index);
    
    	// 录像相关
    	bool StartCapture(_In_ int index);
    	bool StopCapture(_In_ int index);
    	bool GetIsCrabbing(_In_ int index);
    	bool GetIsOpen(_In_ int index);
    
    	// 标志是否正在录像的接口 还有一组标志位
    
    private:
    	// 海康相机列表
    	MV_CC_DEVICE_INFO_LIST list_mv_cc_device_info_list_;
    
    	std::map<CString,LPVOID>map_dev_handle_hik_;
    	int m_is_init_[MAX_CAMERA];
    	bool m_is_grabbing[MAX_CAMERA];   // 正在录像
    
    public:
    	// 相机窗口参数
    	int m_iExp[MAX_CAMERA];
    	int m_iGain[MAX_CAMERA];
    	int m_iWidth[MAX_CAMERA];
    	int m_iHeight[MAX_CAMERA];
    	int m_DevDir[MAX_CAMERA];
    	int m_iFrame[MAX_CAMERA];
    	bool m_bFrame[MAX_CAMERA];
    	CString m_strSerialNumber[MAX_CAMERA];
    	int m_CameraList[MAX_CAMERA];
    	int m_CurCameraIndex;
    	int m_CameraNum;
    
    
    	//long	m_ImgBufLen[MAX_CAMERA];
    	BYTE	*m_pGraBuffer[MAX_CAMERA];
    	//BITMAPINFO *m_pBmpInfo[MAX_CAMERA];
    
    	// 录像时候的参数
    	uint32_t        m_buffSize;
    	unsigned char * m_pBuff;
    
    
    	CCriticalSection m_AutoLock;
    };
    
    
    
    • 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
    #include "StdAfx.h"
    #include "Ts_HiKCamera.h"
    #include "MvCameraControl.h"
    #include "TSCtrlSys.h"
    //#include "MyLock.h"
    //#include 
    
    #pragma comment (lib, "ws2_32")
    #pragma comment(lib, "MvCameraControl.lib")
    #pragma warning(disable: 4819)
    
    
    Ts_HiKCamera::Ts_HiKCamera(void)
    {
    }
    
    
    Ts_HiKCamera::~Ts_HiKCamera(void)
    {
    	Terminate();
    }
    
    
    
    bool Ts_HiKCamera::PreInit() {
    
    	//相机初始化
    	int nRet = MV_CC_EnumDevices(MV_GIGE_DEVICE | MV_USB_DEVICE,&list_mv_cc_device_info_list_);
    	if (MV_OK != nRet) {
    		return false;
    	}
    
    	if (0 == list_mv_cc_device_info_list_.nDeviceNum) {
    		return false;
    	}
    
    	//将值加入到信息列表框中并显示出来 | en:Add result to information list and
    	// display
    	for (unsigned int i = 0; i < list_mv_cc_device_info_list_.nDeviceNum; i++) {
    		MV_CC_DEVICE_INFO* pDeviceInfo =list_mv_cc_device_info_list_.pDeviceInfo[i];
    		if (NULL == pDeviceInfo) {
    			continue;
    		}
    		if (pDeviceInfo->nTLayerType == MV_USB_DEVICE ||pDeviceInfo->nTLayerType == MV_GIGE_DEVICE) {
    			char charUserName[256] = {0};
    			CString strUserName =(CString)pDeviceInfo->SpecialInfo.stGigEInfo.chUserDefinedName;
    			if (0 == strUserName.Find(_T("Dispcam"))) {
    				LPVOID dev_handle_hik = nullptr;
    				int nRet = MV_CC_CreateHandle(&dev_handle_hik, list_mv_cc_device_info_list_.pDeviceInfo[i]);
    				if (MV_OK != nRet) 
    				{
    					if (i == list_mv_cc_device_info_list_.nDeviceNum - 1) 
    					{
    						return false;
    					}
    					return false;
    				}
    
    				nRet = MV_CC_OpenDevice(dev_handle_hik);
    				if (MV_OK != nRet) {
    					MV_CC_DestroyHandle(&dev_handle_hik);
    					if (i == list_mv_cc_device_info_list_.nDeviceNum - 1) {
    						return false;
    					}
    					continue;
    				}
    				int OptimalPacketSize = MV_CC_GetOptimalPacketSize(dev_handle_hik);
    				MV_CC_SetIntValueEx(dev_handle_hik, "GevSCPSPacketSize", OptimalPacketSize);
    				map_dev_handle_hik_.insert(std::pair<CString, LPVOID>(strUserName, dev_handle_hik));
    				int ret = MV_OK;
    				//ret = MV_CC_StartGrabbing(dev_handle_hik);
    				MV_CC_SetEnumValue(dev_handle_hik, "TriggerMode", MV_TRIGGER_MODE_ON);
    				//软触发
    				MV_CC_SetEnumValue(dev_handle_hik, "TriggerSource",MV_TRIGGER_SOURCE_SOFTWARE);
    				// 0924 修改,AOI拍照显示照片为上一张照片
    				ret = MV_CC_StartGrabbing(dev_handle_hik);
    				if (ret == MV_OK) {
    					g_pFrm->m_Robot->AddMsg("相机初始化成功!");
    				} else {
    					if (i == list_mv_cc_device_info_list_.nDeviceNum - 1) {
    						return false;
    					}
    					continue;
    				}
    				m_is_init_[i] = true;
    			}
    		}
    	}
    	return true;
    }
    
    
    bool Ts_HiKCamera::Terminate() {
    	CString cam_param_file_path;
    	for (auto iter = map_dev_handle_hik_.begin();
    		iter != map_dev_handle_hik_.end(); iter++) {
    			if (nullptr != iter->second) {
    				MV_CC_StopGrabbing(iter->second);
    				MV_CC_CloseDevice(iter->second);
    				MV_CC_DestroyHandle(iter->second);
    				iter->second = nullptr;
    			}
    	}
    
    	map_dev_handle_hik_.erase(map_dev_handle_hik_.begin(),
    		map_dev_handle_hik_.end());
    	return false;
    	return true;
    }
    
    LPVOID Ts_HiKCamera::GetGigEInstantCameraForUserDefinedName(_In_ int index) {
    		CString user_defined_name;
    		user_defined_name.Format(_T("Dispcam%d"),index);
    		auto iter = map_dev_handle_hik_.find(user_defined_name);
    		if (iter != std::end(map_dev_handle_hik_)) {
    			return iter->second;
    		}
    		return nullptr;
    }
    
    
    bool Ts_HiKCamera::GrabImage(_In_ int index, _Out_ HalconCpp::HObject* ho_image) {
    	//未初始化
    	if (false == m_is_init_[index]) {
    		g_pFrm->m_Robot->AddMsg(_T("相机未初始化或未初始化成功!"));
    		return false;
    	}
    
    	CString camera_name;
    	camera_name.Format(_T("Dispcam%d"),index);
    	
    	//ASSERT(GetGigEInstantCameraForUserDefinedName(index));
    	if(GetGigEInstantCameraForUserDefinedName(index) == nullptr)
    	{
    		g_pFrm->m_Robot->AddMsg(_T("相机拍照失败!"));
    		return false;
    	}
    
    	MV_FRAME_OUT mvImageInfo;
    	::memset(&mvImageInfo, 0, sizeof(MV_FRAME_OUT_INFO));
    	try {
    		if (FALSE == map_dev_handle_hik_[camera_name]) {
    			return false;
    		}
    		unsigned int nDataLen = 0;
    
    		/*static std::mutex mutex_;
    		std::lock_guard lck(mutex_);*/
    		// 对拍照进行加锁
    		//AotoLock grab_lock(m_AutoLock);
    
    		int nRet;
    		nRet = MV_CC_SetCommandValue(map_dev_handle_hik_[camera_name], "TriggerSoftware");
    		nRet = MV_CC_GetImageBuffer(map_dev_handle_hik_[camera_name], &mvImageInfo, 1000);
    		if (MV_OK != nRet) {
    			MV_CC_FreeImageBuffer(map_dev_handle_hik_[camera_name], &mvImageInfo);
    			return false;
    		}
    		int nRes = mvImageInfo.stFrameInfo.enPixelType & MV_GVSP_PIX_CUSTOM;
    		if (nRes == MV_GVSP_PIX_CUSTOM) {
    			MV_CC_FreeImageBuffer(map_dev_handle_hik_[camera_name], &mvImageInfo);
    			return false;
    		}
    		nDataLen = mvImageInfo.stFrameInfo.nFrameLen;
    		unsigned char* pBufForDriver = mvImageInfo.pBufAddr;
    		GenImageConst(ho_image, "byte", 512, 512);
    		HalconCpp::GenImage1(ho_image, "byte", mvImageInfo.stFrameInfo.nWidth,
    			mvImageInfo.stFrameInfo.nHeight, (Hlong)pBufForDriver);
    		
    	}
    	catch (...) {
    		MV_CC_FreeImageBuffer(map_dev_handle_hik_[camera_name], &mvImageInfo);
    		return false;
    	}
    	MV_CC_FreeImageBuffer(map_dev_handle_hik_[camera_name], &mvImageInfo);
    	return true;
    }
    
    void Ts_HiKCamera::SetExposureTime(_In_ int index, const int& expouser_time) {
    	//未初始化
    	if (false == m_is_init_[index]) {
    		g_pFrm->m_Robot->AddMsg(_T("相机未初始化或未初始化成功!"));
    		return;
    	}
    
    	CString camera_name;
    	camera_name.Format(_T("Dispcam%d"),index);
    	//ASSERT(map_dev_handle_hik_[camera_name]);
    	if(GetGigEInstantCameraForUserDefinedName(index) == nullptr)
    	{
    		g_pFrm->m_Robot->AddMsg(_T("设置相机曝光失败!"));
    		return ;
    	}
    	//ASSERT(GetGigEInstantCameraForUserDefinedName(index));
    
    	/*int ret = MV_CC_SetEnumValue(map_dev_handle_hik_[camera_name], "ExposureMode",
    		MV_EXPOSURE_MODE_TIMED);
    	if (MV_OK != ret) {
    		return;
    	}
    
    	ret = MV_CC_SetEnumValue(map_dev_handle_hik_[camera_name], "ExposureMode",
    		MV_EXPOSURE_MODE_TIMED);*/
    	int ret = MV_CC_SetFloatValue(map_dev_handle_hik_[camera_name], "ExposureTime",
    		float(expouser_time));
    	if (MV_OK != ret) {
    		return;
    	}
    
    	return;
    }
    
    int Ts_HiKCamera::GetExposureTime(_In_ int index) {
    	//未初始化
    	if (false == m_is_init_[index]) {
    		g_pFrm->m_Robot->AddMsg(_T("相机未初始化或未初始化成功!"));
    		return -1;
    	}
    
    	CString camera_name;
    	camera_name.Format(_T("Dispcam%d"),index);
    	//ASSERT(map_dev_handle_hik_[camera_name]);
    	if(GetGigEInstantCameraForUserDefinedName(index) == nullptr)
    	{
    		g_pFrm->m_Robot->AddMsg(_T("获取相机曝光失败!"));
    		return -1;
    	}
    	//ASSERT(GetGigEInstantCameraForUserDefinedName(index));
    
    	MVCC_FLOATVALUE stParam;
    	::memset(&stParam, 0, sizeof(MVCC_FLOATVALUE));
    	int nRet = MV_CC_GetFloatValue(map_dev_handle_hik_[camera_name], "ExposureTime", &stParam);
    	if (MV_OK != nRet) {
    		return -1;
    	}
    
    	return (int)stParam.fCurValue;
    }
    
    bool Ts_HiKCamera::StartCapture(_In_ int index)
    {
    	/*  取流
    	MV_CAMCTRL_API int __stdcall MV_CC_StartGrabbing(IN void* handle);
    	*/
    	return true;  // 不开实时采集了
    	CString camera_name;
    	camera_name.Format(_T("Dispcam%d"),index);
    	//ASSERT(map_dev_handle_hik_[camera_name]);
    	ASSERT(GetGigEInstantCameraForUserDefinedName(index));
    
    	HalconCpp::HObject ho_image;
    	ho_image.GenEmptyObj();
    	MV_FRAME_OUT mvImageInfo;
    	::memset(&mvImageInfo, 0, sizeof(MV_FRAME_OUT_INFO));
    	try {
    		if (FALSE == map_dev_handle_hik_[camera_name]) {
    			return false;
    		}
    		unsigned int nDataLen = 0;
    
    		/*static std::mutex mutex_;
    		std::lock_guard lck(mutex_);*/
    		int nRet;
    		nRet = MV_CC_SetCommandValue(map_dev_handle_hik_[camera_name], "TriggerSoftware");
    		nRet = MV_CC_GetImageBuffer(map_dev_handle_hik_[camera_name], &mvImageInfo, 1000);
    		nRet = MV_CC_StartGrabbing(map_dev_handle_hik_[camera_name]);
    		if (MV_OK != nRet) {
    			MV_CC_FreeImageBuffer(map_dev_handle_hik_[camera_name], &mvImageInfo);
    			return false;
    		}
    		m_is_grabbing[index] = true;
    		if (MV_OK != nRet) {
    			m_is_grabbing[index] = false;
    			MV_CC_StopGrabbing(map_dev_handle_hik_[camera_name]);
    			return false;
    		}
    		int nRes = mvImageInfo.stFrameInfo.enPixelType & MV_GVSP_PIX_CUSTOM;
    		if (nRes == MV_GVSP_PIX_CUSTOM) {
    			MV_CC_StopGrabbing(map_dev_handle_hik_[camera_name]);
    			m_is_grabbing[index] = false;
    			return false;
    		}
    		nDataLen = mvImageInfo.stFrameInfo.nFrameLen;
    		unsigned char* pBufForDriver = mvImageInfo.pBufAddr;
    		GenImageConst(&ho_image, "byte", 512, 512);
    		HalconCpp::GenImage1(&ho_image, "byte", mvImageInfo.stFrameInfo.nWidth,
    			mvImageInfo.stFrameInfo.nHeight, (Hlong)pBufForDriver);
    		
    	}
    	catch (...) {
    		MV_CC_StopGrabbing(map_dev_handle_hik_[camera_name]);
    		m_is_grabbing[index] = false;
    		return false;
    	}
    	return true;
    }
    
    
    bool Ts_HiKCamera::StopCapture(_In_ int index)
    {
    	/*
    	停止取流
    	MV_CAMCTRL_API int __stdcall MV_CC_StopGrabbing(IN void* handle);
    	*/
    	return true;
    	if(index >= m_CameraNum)
    		return false;
    	CString camera_name;
    	camera_name.Format(_T("Dispcam%d"),index);
    	//ASSERT(map_dev_handle_hik_[camera_name]);
    
    	ASSERT(GetGigEInstantCameraForUserDefinedName(index));
    
    	MV_CC_StopGrabbing(map_dev_handle_hik_[camera_name]);
    	m_is_grabbing[index] = false;
    
    	return TRUE;
    }
    
    bool Ts_HiKCamera::GetIsCrabbing(_In_ int index)
    {
    	if(index < 0 || index > map_dev_handle_hik_.size())
    		return false;
    	return m_is_grabbing[index];
    }
    
    bool Ts_HiKCamera::GetIsOpen(_In_ int index)
    {
    	if(index < 0 || index > map_dev_handle_hik_.size())
    		return false;
    	return m_is_init_[index];
    }
    
    • 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
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
    • 257
    • 258
    • 259
    • 260
    • 261
    • 262
    • 263
    • 264
    • 265
    • 266
    • 267
    • 268
    • 269
    • 270
    • 271
    • 272
    • 273
    • 274
    • 275
    • 276
    • 277
    • 278
    • 279
    • 280
    • 281
    • 282
    • 283
    • 284
    • 285
    • 286
    • 287
    • 288
    • 289
    • 290
    • 291
    • 292
    • 293
    • 294
    • 295
    • 296
    • 297
    • 298
    • 299
    • 300
    • 301
    • 302
    • 303
    • 304
    • 305
    • 306
    • 307
    • 308
    • 309
    • 310
    • 311
    • 312
    • 313
    • 314
    • 315
    • 316
    • 317
    • 318
    • 319
    • 320
    • 321
    • 322
    • 323
    • 324
    • 325
    • 326
    • 327
    • 328
    • 329
    • 330
    • 331
    • 332

    海康相机驱动以及海康相机安装包链接:
    链接:https://pan.quark.cn/s/6be85627a97d
    提取码:ceH4

  • 相关阅读:
    【C++】特殊类的设计
    红黑树实现
    React - 受控组件与非受控组件(实践妙用-模糊查询)
    Redis 与 DB 的数据一致 / 双写一致性问题
    linux中用date命令获取昨天、明天或多天前后的日期
    浦东人大常委会副主任刘宇青一行莅临零数科技指导工作
    【北亚数据恢复】不认盘的移动硬盘恢复数据案例&解决方案
    从零开始探索C语言(八)----指针
    软件项目管理【UML介绍】
    【虹科干货】逻辑数据库可能已经无法满足需求了!
  • 原文地址:https://blog.csdn.net/Liang_ming_/article/details/132276481