EntityCamera.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  1. #include "EntityCamera.h"
  2. /// 初始化相机环境
  3. bool ICamera::initEnv()
  4. {
  5. return true;
  6. }
  7. /// 关闭相机环境
  8. bool ICamera::closeEnv()
  9. {
  10. return true;
  11. }
  12. /// 获取所有相机序列号
  13. QVector<QString> ICamera::cameraSerial()
  14. {
  15. QVector<QString> Seriallist;
  16. Seriallist.clear();
  17. MV_CC_DEVICE_INFO_LIST m_stDevList;
  18. memset(&m_stDevList, 0, sizeof(MV_CC_DEVICE_INFO_LIST));
  19. // ch:枚举子网内所有设备 | en:Enumerate all devices within subnet
  20. int nRet = CMyCamera::EnumDevices(&m_stDevList);
  21. if (MV_OK != nRet)
  22. {
  23. return Seriallist;
  24. }
  25. int g_serverCount = Seriallist.size(); //得到采集设备的数量
  26. for (size_t i = 0; i < m_stDevList.nDeviceNum; i++)
  27. {
  28. MV_CC_DEVICE_INFO* pDeviceInfo = m_stDevList.pDeviceInfo[i];
  29. QString strSerialNumber = QString(QLatin1String((char*)pDeviceInfo->SpecialInfo.stGigEInfo.chSerialNumber));
  30. QString strUserName = QString(QLatin1String((char*)pDeviceInfo->SpecialInfo.stGigEInfo.chUserDefinedName));
  31. QString str = strSerialNumber;
  32. if (!strUserName.isEmpty())
  33. {
  34. str = "(" + strUserName + ")" + strSerialNumber;
  35. }
  36. Seriallist.push_back(str);
  37. }
  38. return Seriallist;
  39. }
  40. EntityCamera::EntityCamera() :
  41. m_pCamParamConfig(nullptr),
  42. m_pDialogCamPasram(nullptr)
  43. {
  44. m_nImageChannels = 1;
  45. //////////////////////////////////////////////////////////////////////////
  46. m_pcMyCamera = nullptr;
  47. }
  48. EntityCamera::~EntityCamera(void)
  49. {
  50. this->stopWork();
  51. if (m_pcMyCamera != NULL)
  52. {
  53. delete m_pcMyCamera;
  54. }
  55. bool bcloseCamera = this->closeCamera();
  56. if (nullptr != m_pDialogCamPasram)
  57. {
  58. delete m_pDialogCamPasram;
  59. m_pDialogCamPasram = NULL;
  60. }
  61. if (nullptr != m_pCamParamConfig)
  62. {
  63. delete m_pCamParamConfig;
  64. m_pCamParamConfig = nullptr;
  65. }
  66. }
  67. /// 创建ENTITY相机
  68. EntityCamera* EntityCamera::createCamera(QString serial)
  69. {
  70. EntityCamera* camera = nullptr;
  71. try
  72. {
  73. camera = new EntityCamera();
  74. if (camera)
  75. {
  76. /// 打开相机
  77. if (true == camera->openCamera(serial))
  78. {
  79. if (false == camera->initialize())
  80. {
  81. delete camera;
  82. camera = NULL;
  83. }
  84. }
  85. else
  86. {
  87. delete camera;
  88. camera = NULL;
  89. }
  90. }
  91. }
  92. catch (...)
  93. {
  94. delete camera;
  95. camera = NULL;
  96. }
  97. return camera;
  98. }
  99. /// 打开相机
  100. bool EntityCamera::openCamera(QString serial)
  101. {
  102. bool bret = true;
  103. m_strSerial = serial;
  104. MV_CC_DEVICE_INFO_LIST m_stDevList; // ch:设备信息列表结构体变量,用来存储设备列表 // ch:初始化设备信息列表 | en:Device Information List Initialization
  105. memset(&m_stDevList, 0, sizeof(MV_CC_DEVICE_INFO_LIST));
  106. // ch:枚举子网内所有设备 | en:Enumerate all devices within subnet
  107. int nRet = CMyCamera::EnumDevices(&m_stDevList);
  108. if (MV_OK != nRet)
  109. {
  110. return false;
  111. }
  112. QString str;
  113. MV_CC_DEVICE_INFO* pDeviceInfo = NULL;
  114. for (size_t i = 0; i < m_stDevList.nDeviceNum; i++)
  115. {
  116. pDeviceInfo = m_stDevList.pDeviceInfo[i];
  117. QString strSerialNumber = QString(QLatin1String((char*)pDeviceInfo->SpecialInfo.stGigEInfo.chSerialNumber));
  118. QString strUserName = QString(QLatin1String((char*)pDeviceInfo->SpecialInfo.stGigEInfo.chUserDefinedName));
  119. QString str = strSerialNumber;
  120. if (!strUserName.isEmpty())
  121. {
  122. str = "(" + strUserName + ")" + strSerialNumber;
  123. }
  124. if (serial.indexOf(str) != -1)
  125. {
  126. break;
  127. }
  128. }
  129. if (m_pcMyCamera == NULL)
  130. {
  131. m_pcMyCamera = new CMyCamera;
  132. }
  133. if (m_pcMyCamera != NULL)
  134. {
  135. int nRet = m_pcMyCamera->Open(pDeviceInfo);
  136. if (MV_OK != nRet)
  137. {
  138. delete m_pcMyCamera;
  139. m_pcMyCamera = NULL;
  140. return false;
  141. }
  142. }
  143. // ch:探测网络最佳包大小(只对GigE相机有效) | en:Detection network optimal package size(It only works for the GigE camera)
  144. if (pDeviceInfo->nTLayerType == MV_GIGE_DEVICE)
  145. {
  146. int nPacketSize = m_pcMyCamera->GetOptimalPacketSize();
  147. if (nPacketSize > 0)
  148. {
  149. nRet = m_pcMyCamera->SetIntValue("GevSCPSPacketSize", nPacketSize);
  150. if (nRet != MV_OK)
  151. {
  152. //MessageBox(_T("Warning: Set Packet Size fail!"));
  153. }
  154. }
  155. }
  156. imageHeight();
  157. imageWidth();
  158. imageChannels();
  159. // ch:注册抓图回调 | en:Register image callback
  160. m_pcMyCamera->RegisterImageCallBack(EntityCamera::ImageCallBackEx, this);
  161. return bret;
  162. }
  163. /// 关闭相机
  164. bool EntityCamera::closeCamera()
  165. {
  166. bool bret = true;
  167. if (NULL != m_pcMyCamera)
  168. {
  169. //断开设备
  170. m_pcMyCamera->Close();
  171. // delete m_pcMyCamera;
  172. // m_pcMyCamera = NULL;
  173. }
  174. return bret;
  175. }
  176. /// 初始化
  177. bool EntityCamera::initialize()
  178. {
  179. if (NULL == m_pDialogCamPasram)
  180. {
  181. m_pDialogCamPasram = new DialogCamPasram(this);
  182. }
  183. // if (imageChannels() == 3)
  184. // {
  185. // m_pColorconv->Enable(true, false);
  186. // m_pColorconv->SetOutputFormat(SapFormatRGB888);
  187. // m_pColorconv->SetAlign(SapColorConversion::AlignRGGB);
  188. // }
  189. return true;
  190. }
  191. /// 加载参数
  192. void EntityCamera::loadCamParam()
  193. {
  194. }
  195. /// 保存参数
  196. void EntityCamera::saveCamParam()
  197. {
  198. }
  199. /// 设置相机工作模式
  200. bool EntityCamera::setWorkMode(IBaseCamCommon::CamWorkMode mode, bool Running )
  201. {
  202. bool bret = true;
  203. this->stopWork();
  204. switch (mode)
  205. {
  206. case IBaseCamCommon::eContinu:
  207. //连续采集模式 相机一直工作采图
  208. this->setToFreeRunMode();
  209. break;
  210. case IBaseCamCommon::eHardware:
  211. //中断采集模式 相机接收到硬件中断中断命令采集图像
  212. this->setToHardwareTrigerMode();
  213. break;
  214. case IBaseCamCommon::eSoftware:
  215. //中断采集模式 相机接收到软件中断命令采集图像
  216. bret = this->setToSoftwareTrigerMode();
  217. break;
  218. case IBaseCamCommon::eSuspendCou:
  219. break;
  220. default:
  221. break;
  222. }
  223. mWorkMode = mode; //工作模式
  224. return bret;
  225. }
  226. /// 相机工作模式
  227. IBaseCamCommon::CamWorkMode EntityCamera::workMode()
  228. {
  229. return mWorkMode;
  230. }
  231. /// 相机开始工作
  232. bool EntityCamera::startWork()
  233. {
  234. bool bret = true;
  235. int nRet = MV_OK;
  236. if (NULL != m_pcMyCamera)
  237. {
  238. nRet = m_pcMyCamera->StartGrabbing();
  239. if (nRet == MV_OK)
  240. {
  241. }
  242. }
  243. return bret;
  244. }
  245. /// 相机停止工作
  246. bool EntityCamera::stopWork()
  247. {
  248. bool bret = true;
  249. int nRet = MV_OK;
  250. if (NULL != m_pcMyCamera)
  251. {
  252. nRet = m_pcMyCamera->StopGrabbing();
  253. }
  254. else
  255. {
  256. return false;
  257. }
  258. if (MV_OK != nRet)
  259. {
  260. return false;
  261. }
  262. return bret;
  263. }
  264. /// 相机序列号
  265. QString EntityCamera::serial()
  266. {
  267. return m_strSerial;
  268. }
  269. /// 重启相机
  270. bool EntityCamera::restartCamera()
  271. {
  272. return true;
  273. }
  274. /// 获取一桢图像
  275. bool EntityCamera::captureImage(void** buf, int size)
  276. {
  277. return true;
  278. }
  279. /// 获取图像宽
  280. int EntityCamera::imageWidth(void)
  281. {
  282. return 0;
  283. }
  284. /// 获取图像高
  285. int EntityCamera::imageHeight(void)
  286. {
  287. return 0;
  288. }
  289. /// 获取图像通道数
  290. int EntityCamera::imageChannels(void)
  291. {
  292. return m_nImageChannels;
  293. }
  294. /// 获取配置参数类
  295. IBaseCamParamConfig* EntityCamera::camParamConfig()
  296. {
  297. CamParamConfig* paramConfig = nullptr; //配置参数类
  298. if (nullptr == m_pCamParamConfig)
  299. {
  300. m_pCamParamConfig = new CamParamConfig(this);
  301. }
  302. paramConfig = m_pCamParamConfig;
  303. return paramConfig;
  304. }
  305. /// 显示设置参数窗口页面
  306. void EntityCamera::showParamConfigWindow(int mode )
  307. {
  308. if (m_pDialogCamPasram)
  309. {
  310. m_pDialogCamPasram->setModal(true);
  311. m_pDialogCamPasram->show();
  312. }
  313. }
  314. /// 获取相机最大分辨率
  315. bool EntityCamera::getMaxSize(int& w, int& h)
  316. {
  317. return true;
  318. }
  319. /// 设置相机分辨率
  320. bool EntityCamera::setResolution(int w, int h)
  321. {
  322. return true;
  323. }
  324. /// 获取已注册图像回调的数量
  325. int EntityCamera::getReceCallBackSIze()
  326. {
  327. return m_pImgCallBack.size();
  328. }
  329. ///设置接收图像数据回调函数
  330. void EntityCamera::setReceCallBack(IBaseCamCommon::receImgCallBack callBack, long long userParam, QString userID)
  331. {
  332. //this->m_pReceImgFunction = callBack;
  333. //this->mUserParam = userParam;
  334. IBaseCamCommon::ImgCallBack* pImgCallBack = new IBaseCamCommon::ImgCallBack;
  335. pImgCallBack->pReceFunction = callBack;
  336. pImgCallBack->pUserParam = userParam;
  337. pImgCallBack->strUserID = userID;
  338. m_pImgCallBack.push_back(pImgCallBack);
  339. }
  340. ///设置接收Log数据回调函数
  341. void EntityCamera::setReceLogCallBack(IBaseCamCommon::receLogCallBack callBack, long long userParam)
  342. {
  343. this->m_pReceLogFunction = callBack;
  344. this->mLogUserParam = userParam;
  345. }
  346. /// 执行回调函数
  347. void EntityCamera::execCallFunction(IBaseCamCommon::CamImgData* img, int frameCount)
  348. {
  349. for (int i = 0; i < m_pImgCallBack.size(); i++)
  350. {
  351. if (m_pImgCallBack[i]->pReceFunction != nullptr)
  352. {
  353. if (m_pImgCallBack[i]->bActivation || (m_pImgCallBack[i]->strUserID == "main"))
  354. {
  355. m_pImgCallBack[i]->pReceFunction(img, frameCount, m_strSerial, m_pImgCallBack[i]->pUserParam);
  356. //qDebug() << "execCallFunction " << m_pImgCallBack[i]->strUserID;
  357. }
  358. }
  359. }
  360. }
  361. /// 执行Log回调函数
  362. void EntityCamera::execLogCallFunction(QString serial, QString Massage, long long userParam)
  363. {
  364. // printf( "执行回调函数\n" );
  365. if (this->m_pReceLogFunction != NULL)
  366. {
  367. this->m_pReceLogFunction(serial, Massage, userParam);
  368. }
  369. }
  370. /// 软件触发采图
  371. void EntityCamera::softTriggerCap(QString userID)
  372. {
  373. int nRet = m_pcMyCamera->CommandExecute("TriggerSoftware");
  374. if (MV_OK != nRet)
  375. {
  376. qWarning() << "CommandExecute(TriggerSoftware) Error";
  377. }
  378. for (int i = 0; i < m_pImgCallBack.size(); i++)
  379. {
  380. if (m_pImgCallBack[i]->strUserID == userID)
  381. {
  382. m_pImgCallBack[i]->bActivation = true;
  383. }
  384. else
  385. {
  386. m_pImgCallBack[i]->bActivation = false;
  387. }
  388. }
  389. }
  390. //////////////////////////////////////////////////////////////////////////
  391. int RGB2BGR(unsigned char* pRgbData, unsigned int nWidth, unsigned int nHeight)
  392. {
  393. if (NULL == pRgbData)
  394. {
  395. return MV_E_PARAMETER;
  396. }
  397. for (unsigned int j = 0; j < nHeight; j++)
  398. {
  399. for (unsigned int i = 0; i < nWidth; i++)
  400. {
  401. unsigned char red = pRgbData[j * (nWidth * 3) + i * 3];
  402. pRgbData[j * (nWidth * 3) + i * 3] = pRgbData[j * (nWidth * 3) + i * 3 + 2];
  403. pRgbData[j * (nWidth * 3) + i * 3 + 2] = red;
  404. }
  405. }
  406. return MV_OK;
  407. }
  408. void __stdcall EntityCamera::ImageCallBackEx(unsigned char* pData, MV_FRAME_OUT_INFO_EX* pFrameInfo, void* pUser)
  409. {
  410. EntityCamera* pDlg = (EntityCamera*)pUser;
  411. int m_nImageWidth = pFrameInfo->nWidth;
  412. int m_nImageHeight = pFrameInfo->nHeight;
  413. if (pFrameInfo->enPixelType == PixelType_Gvsp_Mono8)
  414. {
  415. pDlg->mImgCapBuffer.channels = 1;
  416. ;
  417. pDlg->mImgCapBuffer.count = 1;
  418. pDlg->mImgCapBuffer.width = m_nImageWidth;
  419. pDlg->mImgCapBuffer.height = m_nImageHeight;
  420. pDlg->mImgCapBuffer.imgData = pData;
  421. pDlg->mImgCapBuffer.nTriggerIndex = pFrameInfo->nTriggerIndex;
  422. pDlg->mImgCapBuffer.nLostPacket = pFrameInfo->nLostPacket;
  423. pDlg->mImgCapBuffer.nFrameNum = pFrameInfo->nFrameNum;
  424. }
  425. else if (pFrameInfo->enPixelType == PixelType_Gvsp_RGB8_Packed)
  426. {
  427. RGB2BGR(pData, pFrameInfo->nWidth, pFrameInfo->nHeight);
  428. pDlg->mImgCapBuffer.count = 1;
  429. pDlg->mImgCapBuffer.channels = 3;
  430. pDlg->mImgCapBuffer.width = m_nImageWidth;
  431. pDlg->mImgCapBuffer.height = m_nImageHeight;
  432. pDlg->mImgCapBuffer.imgData = pData;
  433. }
  434. pDlg->execCallFunction(&pDlg->mImgCapBuffer, pDlg->mImgCapBuffer.count);
  435. }
  436. /// 设置连续采集模式
  437. bool EntityCamera::setToFreeRunMode()
  438. {
  439. int nRet = m_pcMyCamera->SetEnumValue("TriggerMode", 0);
  440. if (MV_OK != nRet)
  441. {
  442. return nRet;
  443. }
  444. return true;
  445. }
  446. /// 设置软件触发模式
  447. bool EntityCamera::setToSoftwareTrigerMode()
  448. {
  449. // 获取TriggerMode节点对象
  450. int nRet = m_pcMyCamera->SetEnumValue("TriggerMode", 1);
  451. if (MV_OK != nRet)
  452. {
  453. //return nRet;
  454. }
  455. nRet = m_pcMyCamera->SetEnumValue("TriggerSource", MV_TRIGGER_SOURCE_SOFTWARE);
  456. if (MV_OK != nRet)
  457. {
  458. return nRet;
  459. }
  460. return true;
  461. }
  462. /// 设置硬件触发模式
  463. bool EntityCamera::setToHardwareTrigerMode()
  464. {
  465. int nRet = m_pcMyCamera->SetEnumValue("TriggerMode", 1);
  466. if (MV_OK != nRet)
  467. {
  468. //return nRet;
  469. }
  470. nRet = m_pcMyCamera->SetEnumValue("TriggerSource", MV_TRIGGER_SOURCE_LINE0);
  471. if (MV_OK != nRet)
  472. {
  473. return nRet;
  474. }
  475. return true;
  476. }