海康相机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;
};


#include "StdAfx.h"
#include "Ts_HiKCamera.h"
#include "MvCameraControl.h"
#include "TSCtrlSys.h"
//#include "MyLock.h"
//#include <mutex>

#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<std::mutex> 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<std::mutex> 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];
}

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

猜你喜欢

转载自blog.csdn.net/Liang_ming_/article/details/132276481