海康相机SDK的C++对应的接口

suoniao 2021-04-23
需要:0索币

我这里所使用的都是C++的,所以就需要海康相机SDK的C++对应的接口

    所以就需要找到对应的lib文件,include文件,以及对应的dll文件,聚齐这三者就可以对其进行相应的开发,如果有不知道怎么找的可以直接在公众号下留言,或者直接加我微信,我帮你来配置,要在对应的VS环境下配置,在这里就不必多讲这块,下面直接来SDK的二次开发源码

    头文件

#ifndef _DAHUA_SDK_GRAB_IMAGE_H_#define _DAHUA_SDK_GRAB_IMAGE_H_/******大华相机SDK抓取图像类****要考虑多个相机才有如下封装****/#include "GenICam/System.h"#include "GenICam/Camera.h"#include "GenICam/GigE/GigECamera.h"#include "GenICam/GigE/GigEInterface.h"#include "Infra/PrintLog.h"#include "Memory/SharedPtr.h"#include "afxmt.h"#include "afxtempl.h"#include "CMImage.h"#include <vector>

#define CAMERA_NUM_MAX 10    //支持的相机最大个数
class AFX_EXT_CLASS CDaHuaSDKGrabImage{public: CDaHuaSDKGrabImage(); virtual ~CDaHuaSDKGrabImage();
struct tagCameraInfo { CString m_szVendorName; //相机制造厂商名称 CString m_szModelName; //相机型号 CString m_szDeviceVersion; //固件版本号 CString m_szSerialNumber; //相机序列号 long m_lCameraID; //相机ID };
static long GetCameraTotal();//获得在线相机总数
public: /****************相机数据定义****************************/ void GetImageSize(long &width, long &height); //获取图片尺寸
QZ_Vision::CQtrImage &GetCurImage(); //获取当前图片信息
/*****************相机采图操作***************************/ bool InitDevice(); //初始化所有在线的设备
bool SetCurrentIndex(long vNum); //按编号设置当前工作相机
bool StartGrabbing(); //开始数据流
bool GraphOneImage(); //抓取一幅图的buffer
bool StopGrabbing(); //结束数据流
bool CloseDevice(); //关闭设备
void StartLiveImages(); //开启实时采集图像的buffer
void StopLiveImages(); //停止实时采集图像的buffer
/*****************相机信息获取***************************/ bool SetCameraID(CString szSerialNumber); //通过相机SN来为相机设置编号
bool GetCameraInfo(long vCamID, tagCameraInfo&info); //获取某个相机的信息
long GetGray(long vCamID, double vX, double vY); //获取某个位置的灰度值
/******************相机参数设置获取**********************/ bool SetAcquistionMode(); //设置采集模式,连续采集模式
bool SetTriggerMode(); //设置触发模式,软触发,触发时间
bool SetExposureTime(long vCamID, double vExpouseTime); //设置曝光时间,单位μs
bool SetGain(long vCamID, double vGain); //设置增益
bool GetExposureTime(long vCamID, double &rExpouseTime); //读取曝光时间,单位μs
bool GetExposureTimeMinMax(long vCamID, double &rMinExp, double &rMaxExp);//读取最小最大曝光时间,单位μs
bool SetPacketSizeTime(long vCamID, int vSize, int vDelayTime); //设置数据包大小和延时
bool GetPacketSizeTime(long vCamID, unsigned int &rSize, unsigned int &rDelayTime); //获取数据包大小和延时
/*************参数保存读取*******************/ void WriteIni(CString szFileName); //Ini文件保存
void ReadIni(CString szFileName); //Ini文件读取
bool WriteParaByC(FILE *pFile); //C文件保存
bool ReadParaByC(FILE *pFile); //C文件读取
/*************图片变换***********************/ void SetHorMirror(bool bEnable); //水平镜像开关
void SetVerMirror(bool bEnable); //垂直镜像开关
void SetTransPose(bool bEnable); //旋转180度protected: QZ_Vision::CQtrImage m_MyGraphImage[CAMERA_NUM_MAX]; //输入图像
tagCameraInfo m_DahuaCameraInfo[CAMERA_NUM_MAX]; //获取到的相机信息
long m_nOnlineNum; //相机连接个数
long m_lCurrentIndex; //当前工作的相机编号
Dahua::GenICam::IStreamSourcePtr m_streamSptr[CAMERA_NUM_MAX]; //数据流
Dahua::GenICam::ICameraPtr m_pMVCamera[CAMERA_NUM_MAX]; //相机接口
BOOL m_bStartGrabbing[CAMERA_NUM_MAX]; //是否开始抓图
bool DeviceHorMirror(bool bMirror); //相机水平镜像
bool DeviceVerMirror(bool bMirror); //相机垂直镜像
bool m_bTransPose; //旋转180度开关
bool OpenDevice(); //打开所有在线的设备
bool m_bThreadStatus; //线程状态
bool m_bThreadStart; //线程开启标志
  static UINT GrabberMethod(LPVOID pParam);              //grabber thread};
#endif

源文件

#include "stdafx.h"#include "Include\DaHuaSDKGrabImage.h"#include "FileOper.h"#include "ImageProcessing.h"

const CString gszDahuaCameraLog = _T("..\\Log\\DahuaCameraLog\\");
CDaHuaSDKGrabImage::CDaHuaSDKGrabImage(){ for (int i = 0; i < CAMERA_NUM_MAX; i++) { m_bStartGrabbing[i] = false; m_MyGraphImage[i].Initial(2592, 1944, QZ_Vision::ImageByte); m_MyGraphImage[i].SetBitmapInfo(); m_DahuaCameraInfo[i].m_lCameraID = 0; m_DahuaCameraInfo[i].m_szVendorName = _T(""); m_DahuaCameraInfo[i].m_szModelName = _T(""); m_DahuaCameraInfo[i].m_szDeviceVersion = _T(""); m_DahuaCameraInfo[i].m_szSerialNumber = _T(""); } m_lCurrentIndex = -1; //当前工作相机序号 默认设为-1 m_nOnlineNum = 0; //相机连接个数 m_bTransPose = false; m_bThreadStart = false; m_bThreadStatus = false;}

CDaHuaSDKGrabImage::~CDaHuaSDKGrabImage(){ StopLiveImages(); StopGrabbing(); CloseDevice();}
//获取当前在线设备总数long CDaHuaSDKGrabImage::GetCameraTotal(){ //发现设备 Dahua::GenICam::CSystem &systemObj = Dahua::GenICam::CSystem::getInstance(); Dahua::Infra::TVector<Dahua::GenICam::ICameraPtr> vCameraPtrList; bool isDiscoverySuccess = systemObj.discovery(vCameraPtrList); if (!isDiscoverySuccess) { return 0; }
return vCameraPtrList.size();}
//获取图片尺寸void CDaHuaSDKGrabImage::GetImageSize(long &width, long &height){ if (m_lCurrentIndex < 0) { width = m_MyGraphImage[0].GetWidth(); height = m_MyGraphImage[0].GetHeight(); return; } width = m_MyGraphImage[m_lCurrentIndex].GetWidth(); height = m_MyGraphImage[m_lCurrentIndex].GetHeight();}
//获取当前图片信息QZ_Vision::CQtrImage &CDaHuaSDKGrabImage::GetCurImage(){ if (m_lCurrentIndex < 0) { return m_MyGraphImage[0]; } return m_MyGraphImage[m_lCurrentIndex];}
//初始化所有在线的设备bool CDaHuaSDKGrabImage::InitDevice(){ if (m_nOnlineNum > 0) { return false; }
//开启设备 if (!OpenDevice()) { CloseDevice(); return false; }
//设置采集模式 if (!SetAcquistionMode()) { CloseDevice(); return false; }
//设置软触发 if (!SetTriggerMode()) { CloseDevice(); return false; }
//开始拉流 if (!StartGrabbing()) { CloseDevice(); return false; }
return true;}
//按编号设置当前工作相机bool CDaHuaSDKGrabImage::SetCurrentIndex(long vNum){ if (vNum > -1 && vNum < m_nOnlineNum) { m_lCurrentIndex = vNum; return true; }
return false;}
//开始数据流bool CDaHuaSDKGrabImage::StartGrabbing(){ Dahua::GenICam::CSystem &systemObj = Dahua::GenICam::CSystem::getInstance(); for (long i = 0; i < m_nOnlineNum; i++) { m_bStartGrabbing[i] = false; if (m_pMVCamera[i]) { //创建流对象 m_streamSptr[i] = systemObj.createStreamSource(m_pMVCamera[i]); if (NULL != m_streamSptr[i]) { bool isStartGrabbingSuccess = m_streamSptr[i]->startGrabbing(); if (isStartGrabbingSuccess) { m_bStartGrabbing[i] = true; } } } }
return true;}
//抓取一幅图的bufferbool CDaHuaSDKGrabImage::GraphOneImage(){ if (m_lCurrentIndex < 0 || m_nOnlineNum < 1 || m_lCurrentIndex >= m_nOnlineNum) { return false; } if (!m_streamSptr[m_lCurrentIndex] || !m_bStartGrabbing[m_lCurrentIndex]) { return false; }
//获取一帧 超时时间1000ms Dahua::GenICam::CFrame frame; bool isSuccess = m_streamSptr[m_lCurrentIndex]->getFrame(frame, 1000); if (isSuccess) { //判断帧的有效性 bool isValid = frame.valid(); if (isValid) { //m_CSMyGrabImage.Lock(); //{ if (m_MyGraphImage[m_lCurrentIndex].GetWidth() != frame.getImageWidth() || m_MyGraphImage[m_lCurrentIndex].GetHeight() != frame.getImageHeight()) { m_MyGraphImage[m_lCurrentIndex].Initial(frame.getImageWidth(), frame.getImageHeight(), QZ_Vision::ImageByte); m_MyGraphImage[m_lCurrentIndex].SetBitmapInfo(); } void *pBuffer = const_cast<void *>(frame.getImage()); if (m_bTransPose) { m_MyGraphImage[m_lCurrentIndex].ImgTransposing(pBuffer); } else { m_MyGraphImage[m_lCurrentIndex].SetData(pBuffer); } //} //m_CSMyGrabImage.Unlock(); return true; } }
return false;}
//结束数据流 bool CDaHuaSDKGrabImage::StopGrabbing(){ bool bRetStatus = true; for (long i = 0; i < m_nOnlineNum; i++) { m_bStartGrabbing[i] = false; if (NULL != m_streamSptr[i]) { bool bRet = m_streamSptr[i]->stopGrabbing(); if (!bRet) { bRetStatus = false; } } } return bRetStatus;}
//关闭设备bool CDaHuaSDKGrabImage::CloseDevice(){ for (int i = 0; i < CAMERA_NUM_MAX; i++) { if (m_pMVCamera[i]) { if (m_pMVCamera[i]->isConnected()) { m_pMVCamera[i]->disConnect(); } } } m_lCurrentIndex = -1; //当前工作相机序号 默认设为-1 m_nOnlineNum = 0; //相机连接个数
return true;}
//开启实时采集图像的buffervoid CDaHuaSDKGrabImage::StartLiveImages(){ if (!m_bThreadStatus) { m_bThreadStart = true; AfxBeginThread((AFX_THREADPROC)GrabberMethod, (LPVOID)this); }}
//停止实时采集图像的buffervoid CDaHuaSDKGrabImage::StopLiveImages(){ if (m_bThreadStart) { m_bThreadStart = false; while (m_bThreadStatus) { MSG msg; while (::PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) { ::TranslateMessage(&msg); ::DispatchMessage(&msg); } } }}
//通过相机SN来为相机设置编号bool CDaHuaSDKGrabImage::SetCameraID(CString szSerialNumber){ for (long i = 0; i < m_nOnlineNum; i++) { if (m_DahuaCameraInfo[i].m_szSerialNumber == szSerialNumber) { m_DahuaCameraInfo[i].m_lCameraID = i; } } return true;}
//获取某个相机的信息bool CDaHuaSDKGrabImage::GetCameraInfo(long vCamID, tagCameraInfo&info){ if (vCamID < 0 || vCamID >= m_nOnlineNum) { vCamID = 0; } info = m_DahuaCameraInfo[vCamID]; return true;}
//获取某个位置的灰度值long CDaHuaSDKGrabImage::GetGray(long vCamID, double vX, double vY){ if (vCamID < 0 || vCamID >= m_nOnlineNum) { vCamID = 0; } return m_MyGraphImage[vCamID].GetGray(vX, vY);}
//设置采集模式,连续采集模式bool CDaHuaSDKGrabImage::SetAcquistionMode(){ for (long i = 0; i < m_nOnlineNum; i++) { //设置连续采集模式 Dahua::GenICam::CSystem &systemObj = Dahua::GenICam::CSystem::getInstance(); Dahua::GenICam::IAcquisitionControlPtr sptrAcquisitionControl = systemObj.createAcquisitionControl(m_pMVCamera[i]); if (NULL == sptrAcquisitionControl.get()) { continue; } Dahua::GenICam::CEnumNode acquisitionMode = sptrAcquisitionControl->acquisitionMode(); bool bRet = acquisitionMode.setValueBySymbol("Continuous"); if (!bRet) { return false; } } return true;}
//设置触发模式,软触发,触发时间bool CDaHuaSDKGrabImage::SetTriggerMode(){ for (long i = 0; i < m_nOnlineNum; i++) { Dahua::GenICam::CSystem &systemObj = Dahua::GenICam::CSystem::getInstance(); Dahua::GenICam::IAcquisitionControlPtr sptrAcquisitionControl = systemObj.createAcquisitionControl(m_pMVCamera[i]); //设置触发源为软触发 Dahua::GenICam::CEnumNode enumNode = sptrAcquisitionControl->triggerSource(); bool bRet = enumNode.setValueBySymbol("Software"); if (!bRet) { return false; } //设置触发器 enumNode = sptrAcquisitionControl->triggerSelector(); bRet = enumNode.setValueBySymbol("FrameStart"); if (!bRet) { return false; } //设置触发模式 enumNode = sptrAcquisitionControl->triggerMode(); bRet = enumNode.setValueBySymbol("Off"); if (!bRet) { return false; } //设置心跳时间 Dahua::GenICam::CIntNode node(m_pMVCamera[i], "GevHeartbeatTimeout"); node.setValue(60000); } return true;}
//设置曝光时间,单位μsbool CDaHuaSDKGrabImage::SetExposureTime(long vCamID, double vExpouseTime){ if (vCamID < 0 || vCamID >= m_nOnlineNum) { return false; } if (!m_pMVCamera[vCamID]) { return false; } Dahua::GenICam::CSystem &systemObj = Dahua::GenICam::CSystem::getInstance(); Dahua::GenICam::IAcquisitionControlPtr sptrAcquisitionControl = systemObj.createAcquisitionControl(m_pMVCamera[vCamID]); Dahua::GenICam::CDoubleNode exposureTime = sptrAcquisitionControl->exposureTime(); bool bResult = exposureTime.setValue(vExpouseTime); return bResult;}