MyCamera.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. //#include "stdafx.h"
  2. #include "MyCamera.h"
  3. #include <string.h>
  4. CMyCamera::CMyCamera()
  5. {
  6. m_hDevHandle = NULL;
  7. }
  8. CMyCamera::~CMyCamera()
  9. {
  10. if (m_hDevHandle)
  11. {
  12. MV_CC_DestroyHandle(m_hDevHandle);
  13. m_hDevHandle = NULL;
  14. }
  15. }
  16. int CMyCamera::EnumDevices(MV_CC_DEVICE_INFO_LIST* pstDevList)
  17. {
  18. int nRet = MV_CC_EnumDevices(MV_GIGE_DEVICE | MV_USB_DEVICE, pstDevList);
  19. if (MV_OK != nRet)
  20. {
  21. return nRet;
  22. }
  23. return MV_OK;
  24. }
  25. // ch:打开设备 | en:Open Device
  26. int CMyCamera::Open(MV_CC_DEVICE_INFO* pstDeviceInfo)
  27. {
  28. if (NULL == pstDeviceInfo)
  29. {
  30. return MV_E_PARAMETER;
  31. }
  32. int nRet = MV_OK;
  33. if(m_hDevHandle == NULL)
  34. {
  35. nRet = MV_CC_CreateHandle(&m_hDevHandle, pstDeviceInfo);
  36. if (MV_OK != nRet)
  37. {
  38. return nRet;
  39. }
  40. }
  41. nRet = MV_CC_OpenDevice(m_hDevHandle);
  42. if (MV_OK != nRet)
  43. {
  44. MV_CC_DestroyHandle(m_hDevHandle);
  45. m_hDevHandle = NULL;
  46. return nRet;
  47. }
  48. return MV_OK;
  49. }
  50. // ch:关闭设备 | en:Close Device
  51. int CMyCamera::Close()
  52. {
  53. int nRet = MV_OK;
  54. if (NULL == m_hDevHandle)
  55. {
  56. return MV_E_PARAMETER;
  57. }
  58. MV_CC_CloseDevice(m_hDevHandle);
  59. nRet = MV_CC_DestroyHandle(m_hDevHandle);
  60. m_hDevHandle = NULL;
  61. return nRet;
  62. }
  63. // ch:开启抓图 | en:Start Grabbing
  64. int CMyCamera::StartGrabbing()
  65. {
  66. return MV_CC_StartGrabbing(m_hDevHandle);
  67. }
  68. // ch:停止抓图 | en:Stop Grabbing
  69. int CMyCamera::StopGrabbing()
  70. {
  71. return MV_CC_StopGrabbing(m_hDevHandle);
  72. }
  73. int CMyCamera::GetOneFrameTimeout(unsigned char* pData, unsigned int* pnDataLen, unsigned int nDataSize, MV_FRAME_OUT_INFO_EX* pFrameInfo, int nMsec)
  74. {
  75. if (NULL == pnDataLen)
  76. {
  77. return MV_E_PARAMETER;
  78. }
  79. int nRet = MV_OK;
  80. *pnDataLen = 0;
  81. nRet = MV_CC_GetOneFrameTimeout(m_hDevHandle, pData, nDataSize, pFrameInfo, nMsec);
  82. if (MV_OK != nRet)
  83. {
  84. return nRet;
  85. }
  86. *pnDataLen = pFrameInfo->nFrameLen;
  87. return nRet;
  88. }
  89. // ch:设置显示窗口句柄 | en:Set Display Window Handle
  90. int CMyCamera::Display(void* hWnd)
  91. {
  92. return MV_CC_Display(m_hDevHandle, hWnd);
  93. }
  94. int CMyCamera::SaveImage(MV_SAVE_IMAGE_PARAM_EX* pstParam)
  95. {
  96. if (NULL == pstParam)
  97. {
  98. return MV_E_PARAMETER;
  99. }
  100. return MV_CC_SaveImageEx2(m_hDevHandle, pstParam);
  101. }
  102. // ch:注册图像数据回调 | en:Register Image Data CallBack
  103. int CMyCamera::RegisterImageCallBack(void(__stdcall* cbOutput)(unsigned char * pData, MV_FRAME_OUT_INFO_EX* pFrameInfo,
  104. void* pUser),void* pUser)
  105. {
  106. return MV_CC_RegisterImageCallBackEx(m_hDevHandle, cbOutput, pUser);
  107. }
  108. // ch:注册消息异常回调 | en:Register Message Exception CallBack
  109. int CMyCamera::RegisterExceptionCallBack(void(__stdcall* cbException)(unsigned int nMsgType, void* pUser),void* pUser)
  110. {
  111. return MV_CC_RegisterExceptionCallBack(m_hDevHandle, cbException, pUser);
  112. }
  113. // ch:获取Int型参数,如 Width和Height,详细内容参考SDK安装目录下的 MvCameraNode.xlsx 文件
  114. // en:Get Int type parameters, such as Width and Height, for details please refer to MvCameraNode.xlsx file under SDK installation directory
  115. int CMyCamera::GetIntValue(IN const char* strKey, OUT unsigned int *pnValue)
  116. {
  117. if (NULL == strKey || NULL == pnValue)
  118. {
  119. return MV_E_PARAMETER;
  120. }
  121. MVCC_INTVALUE stParam;
  122. memset(&stParam, 0, sizeof(MVCC_INTVALUE));
  123. int nRet = MV_CC_GetIntValue(m_hDevHandle, strKey, &stParam);
  124. if (MV_OK != nRet)
  125. {
  126. return nRet;
  127. }
  128. *pnValue = stParam.nCurValue;
  129. return MV_OK;
  130. }
  131. // ch:设置Int型参数,如 Width和Height,详细内容参考SDK安装目录下的 MvCameraNode.xlsx 文件
  132. // en:Set Int type parameters, such as Width and Height, for details please refer to MvCameraNode.xlsx file under SDK installation directory
  133. int CMyCamera::SetIntValue(IN const char* strKey, IN unsigned int nValue)
  134. {
  135. if (NULL == strKey)
  136. {
  137. return MV_E_PARAMETER;
  138. }
  139. return MV_CC_SetIntValue(m_hDevHandle, strKey, nValue);
  140. }
  141. // ch:获取Float型参数,如 ExposureTime和Gain,详细内容参考SDK安装目录下的 MvCameraNode.xlsx 文件
  142. // en:Get Float type parameters, such as ExposureTime and Gain, for details please refer to MvCameraNode.xlsx file under SDK installation directory
  143. int CMyCamera::GetFloatValue(IN const char* strKey, OUT float *pfValue)
  144. {
  145. if (NULL == strKey || NULL == pfValue)
  146. {
  147. return MV_E_PARAMETER;
  148. }
  149. MVCC_FLOATVALUE stParam;
  150. memset(&stParam, 0, sizeof(MVCC_FLOATVALUE));
  151. int nRet = MV_CC_GetFloatValue(m_hDevHandle, strKey, &stParam);
  152. if (MV_OK != nRet)
  153. {
  154. return nRet;
  155. }
  156. *pfValue = stParam.fCurValue;
  157. return MV_OK;
  158. }
  159. // ch:设置Float型参数,如 ExposureTime和Gain,详细内容参考SDK安装目录下的 MvCameraNode.xlsx 文件
  160. // en:Set Float type parameters, such as ExposureTime and Gain, for details please refer to MvCameraNode.xlsx file under SDK installation directory
  161. int CMyCamera::SetFloatValue(IN const char* strKey, IN float fValue)
  162. {
  163. if (NULL == strKey)
  164. {
  165. return MV_E_PARAMETER;
  166. }
  167. return MV_CC_SetFloatValue(m_hDevHandle, strKey, fValue);
  168. }
  169. // ch:获取Enum型参数,如 PixelFormat,详细内容参考SDK安装目录下的 MvCameraNode.xlsx 文件
  170. // en:Get Enum type parameters, such as PixelFormat, for details please refer to MvCameraNode.xlsx file under SDK installation directory
  171. int CMyCamera::GetEnumValue(IN const char* strKey, OUT unsigned int *pnValue)
  172. {
  173. if (NULL == strKey || NULL == pnValue)
  174. {
  175. return MV_E_PARAMETER;
  176. }
  177. MVCC_ENUMVALUE stParam;
  178. memset(&stParam, 0, sizeof(MVCC_ENUMVALUE));
  179. int nRet = MV_CC_GetEnumValue(m_hDevHandle, strKey, &stParam);
  180. if (MV_OK != nRet)
  181. {
  182. return nRet;
  183. }
  184. *pnValue = stParam.nCurValue;
  185. return MV_OK;
  186. }
  187. // ch:设置Enum型参数,如 PixelFormat,详细内容参考SDK安装目录下的 MvCameraNode.xlsx 文件
  188. // en:Set Enum type parameters, such as PixelFormat, for details please refer to MvCameraNode.xlsx file under SDK installation directory
  189. int CMyCamera::SetEnumValue(IN const char* strKey, IN unsigned int nValue)
  190. {
  191. if (NULL == strKey)
  192. {
  193. return MV_E_PARAMETER;
  194. }
  195. return MV_CC_SetEnumValue(m_hDevHandle, strKey, nValue);
  196. }
  197. // ch:获取Bool型参数,如 ReverseX,详细内容参考SDK安装目录下的 MvCameraNode.xlsx 文件
  198. // en:Get Bool type parameters, such as ReverseX, for details please refer to MvCameraNode.xlsx file under SDK installation directory
  199. int CMyCamera::GetBoolValue(IN const char* strKey, OUT bool *pbValue)
  200. {
  201. if (NULL == strKey || NULL == pbValue)
  202. {
  203. return MV_E_PARAMETER;
  204. }
  205. return MV_CC_GetBoolValue(m_hDevHandle, strKey, pbValue);
  206. }
  207. // ch:设置Bool型参数,如 ReverseX,详细内容参考SDK安装目录下的 MvCameraNode.xlsx 文件
  208. // en:Set Bool type parameters, such as ReverseX, for details please refer to MvCameraNode.xlsx file under SDK installation directory
  209. int CMyCamera::SetBoolValue(IN const char* strKey, IN bool bValue)
  210. {
  211. if (NULL == strKey)
  212. {
  213. return MV_E_PARAMETER;
  214. }
  215. return MV_CC_SetBoolValue(m_hDevHandle, strKey, bValue);
  216. }
  217. // ch:获取String型参数,如 DeviceUserID,详细内容参考SDK安装目录下的 MvCameraNode.xlsx 文件UserSetSave
  218. // en:Get String type parameters, such as DeviceUserID, for details please refer to MvCameraNode.xlsx file under SDK installation directory
  219. int CMyCamera::GetStringValue(IN const char* strKey, IN OUT char* strValue, IN unsigned int nSize)
  220. {
  221. if (NULL == strKey || NULL == strValue)
  222. {
  223. return MV_E_PARAMETER;
  224. }
  225. MVCC_STRINGVALUE stParam;
  226. memset(&stParam, 0, sizeof(MVCC_STRINGVALUE));
  227. int nRet = MV_CC_GetStringValue(m_hDevHandle, strKey, &stParam);
  228. if (MV_OK != nRet)
  229. {
  230. return nRet;
  231. }
  232. strcpy_s(strValue, nSize, stParam.chCurValue);
  233. return MV_OK;
  234. }
  235. // ch:设置String型参数,如 DeviceUserID,详细内容参考SDK安装目录下的 MvCameraNode.xlsx 文件UserSetSave
  236. // en:Set String type parameters, such as DeviceUserID, for details please refer to MvCameraNode.xlsx file under SDK installation directory
  237. int CMyCamera::SetStringValue(IN const char* strKey, IN const char* strValue)
  238. {
  239. if (NULL == strKey)
  240. {
  241. return MV_E_PARAMETER;
  242. }
  243. return MV_CC_SetStringValue(m_hDevHandle, strKey, strValue);
  244. }
  245. // ch:执行一次Command型命令,如 UserSetSave,详细内容参考SDK安装目录下的 MvCameraNode.xlsx 文件
  246. // en:Execute Command once, such as UserSetSave, for details please refer to MvCameraNode.xlsx file under SDK installation directory
  247. int CMyCamera::CommandExecute(IN const char* strKey)
  248. {
  249. if (NULL == strKey)
  250. {
  251. return MV_E_PARAMETER;
  252. }
  253. return MV_CC_SetCommandValue(m_hDevHandle, strKey);
  254. }
  255. int CMyCamera::GetOptimalPacketSize()
  256. {
  257. return MV_CC_GetOptimalPacketSize(m_hDevHandle);
  258. }
  259. int CMyCamera::GetAllMatchInfo(OUT unsigned int *nLostFrame, OUT unsigned int *nFrameCount)
  260. {
  261. MV_CC_DEVICE_INFO stDevInfo = {0};
  262. int nRet = MV_CC_GetDeviceInfo(m_hDevHandle, &stDevInfo); //该接口u3暂不支持,用此方式来判断u3还是GigE
  263. if (MV_E_SUPPORT == nRet)
  264. {
  265. stDevInfo.nTLayerType = MV_USB_DEVICE;
  266. }
  267. if (MV_GIGE_DEVICE == stDevInfo.nTLayerType)
  268. {
  269. MV_ALL_MATCH_INFO struMatchInfo = {0};
  270. MV_MATCH_INFO_NET_DETECT stMatchInfoNetDetect;
  271. struMatchInfo.pInfo = &stMatchInfoNetDetect;
  272. struMatchInfo.nType = MV_MATCH_TYPE_NET_DETECT; // ch:网络流量和丢包信息 | en:Net flow and lsot packet information
  273. memset(struMatchInfo.pInfo, 0, sizeof(MV_MATCH_INFO_NET_DETECT));
  274. struMatchInfo.nInfoSize = sizeof(MV_MATCH_INFO_NET_DETECT);
  275. nRet = MV_CC_GetAllMatchInfo(m_hDevHandle, &struMatchInfo);
  276. if (MV_OK != nRet)
  277. {
  278. return nRet;
  279. }
  280. MV_MATCH_INFO_NET_DETECT *pInfo = (MV_MATCH_INFO_NET_DETECT*)struMatchInfo.pInfo;
  281. *nFrameCount = pInfo->nNetRecvFrameCount;
  282. *nLostFrame = stMatchInfoNetDetect.nLostFrameCount;
  283. }
  284. else if (MV_USB_DEVICE == stDevInfo.nTLayerType)
  285. {
  286. MV_ALL_MATCH_INFO struMatchInfo = {0};
  287. MV_MATCH_INFO_USB_DETECT stMatchInfoNetDetect;
  288. struMatchInfo.pInfo = &stMatchInfoNetDetect;
  289. struMatchInfo.nType = MV_MATCH_TYPE_USB_DETECT; // ch:网络流量和丢包信息 | en:Net flow and lsot packet information
  290. memset(struMatchInfo.pInfo, 0, sizeof(MV_MATCH_INFO_USB_DETECT));
  291. struMatchInfo.nInfoSize = sizeof(MV_MATCH_INFO_USB_DETECT);
  292. nRet = MV_CC_GetAllMatchInfo(m_hDevHandle, &struMatchInfo);
  293. if (MV_OK != nRet)
  294. {
  295. return nRet;
  296. }
  297. MV_MATCH_INFO_NET_DETECT *pInfo = (MV_MATCH_INFO_NET_DETECT*)struMatchInfo.pInfo;
  298. *nFrameCount = pInfo->nNetRecvFrameCount;
  299. *nLostFrame = stMatchInfoNetDetect.nErrorFrameCount;
  300. }
  301. return MV_OK;
  302. }