takephoto.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. #include "takephoto.h"
  2. #include "stb_image_write.h"
  3. // 模块名称
  4. static const char MODULE_NAME[] = "HrTakePhoto";
  5. // 相机类型
  6. static unsigned int device_type = interfaceTypeGige | interfaceTypeUsb3;
  7. // 制造厂商
  8. static const char * const manufacturer = "Huaray Technology";
  9. // 触发模式
  10. static const char * const setTriggerMode = "Off"; /// 默认: "Off", 可选: "On"
  11. // 曝光取图
  12. #define AE_WAIT_MAX_MS 180000 // 最大曝光稳定等待的时间, 默认3分钟, 单位: ms
  13. #define AE_EXP_TIME_EPS_US 50 // 曝光时间稳定判定的误差, 防抖&区间, 单位: us
  14. #define AE_EXP_STABLE_FRAMES 4 // 相机处于自动曝光模式时, 累计曝光值稳定的帧数
  15. static int waitAEDone = 1; // 是否等待自动曝光完成: "1"-等待(默认), "0"-不等待//
  16. // 保存照片
  17. static int SavePhoto(HANDLE hCam, IMV_Frame *pFrame, DHImgType imgType, const char *imgPath);
  18. // 拍照环境
  19. typedef struct
  20. {
  21. HANDLE hCam; // 打开相机的句柄
  22. uint32_t camType; // 相机类型:U、网
  23. char manuName[MAX_LINE_CHARS]; //制造厂商名称
  24. DHImgType saveImgType; // 保存图像的类型
  25. const char *saveImgPath; // 保存图像的路径
  26. unsigned int imgWidth; // 拍照图像宽度px
  27. unsigned int imgHeight; // 拍照图像高度px
  28. bool isExposureAuto; // 自动曝光:是/否
  29. struct timespec expTime0; // 曝光开始的时间
  30. float lastExpTime; // 上次的曝光时长(us)
  31. int expStableCnt; // 连续曝光稳定帧计数
  32. HANDLE hESig; // 任务结束的通知
  33. int rCode; // 任务结束返回值
  34. void *pUser; // 可选的用户数据
  35. } PthotoProcCtx;
  36. // 非标准的"Huaray Technology"制造商名称, 但也可视为华睿的
  37. static const char * const huaray_manu_aliases[] = { "Machine Vision", NULL };
  38. static bool is_huaray_manu_alias(const char *name)
  39. {
  40. for(int i = 0; huaray_manu_aliases[i] != NULL; i++)
  41. {
  42. if(xstrcasecmp(name, huaray_manu_aliases[i]) == 0) return true;
  43. }
  44. return false;
  45. }
  46. static void OnFrameReceived(IMV_Frame *pFrame, void *pUser) // 数据帧的回调, 完成一次拍照任务
  47. {
  48. PthotoProcCtx *ctx = (PthotoProcCtx *)pUser; int ret = IMV_OK;
  49. if(NULL == pFrame) return;
  50. // 1, 打印该帧信息
  51. sw_log_info("[%s] +++GetOneFrame+++, Width[%u], Height[%u], FrameNum[%llu], FrameLen[%u], PixelType[0x%08X]", MODULE_NAME,
  52. pFrame->frameInfo.width, pFrame->frameInfo.height, pFrame->frameInfo.blockId, pFrame->frameInfo.size,
  53. (unsigned int)pFrame->frameInfo.pixelFormat);
  54. // 2, 自动曝光模式
  55. if(ctx->isExposureAuto && waitAEDone != 0)
  56. {
  57. double curExpTime = 0.0;
  58. ret = IMV_GetDoubleFeatureValue(ctx->hCam, "ExposureTime", &curExpTime);
  59. if(IMV_OK != ret) goto end_p;
  60. struct timespec now; clock_gettime(CLOCK_MONOTONIC, &now);
  61. long elapsed = (now.tv_sec - ctx->expTime0.tv_sec)*1000 + (now.tv_nsec-ctx->expTime0.tv_nsec)/(1000*1000); // 计算时间差, 单位: ms
  62. sw_log_info("[%s] +++GetOneFrame+++, 等待曝光完成, ExposureTime = %.2fus, ElapsedTime = %ldms", MODULE_NAME, curExpTime, elapsed);
  63. if(fabsf(curExpTime - ctx->lastExpTime) <= AE_EXP_TIME_EPS_US) ctx->expStableCnt++;
  64. else ctx->expStableCnt = 0;
  65. if(ctx->expStableCnt < AE_EXP_STABLE_FRAMES && elapsed < AE_WAIT_MAX_MS)
  66. { // 曝光未稳定, 也未超时, 则继续等下一帧
  67. ctx->lastExpTime = curExpTime; if(0 == ctx->expStableCnt) ctx->expStableCnt = 1;
  68. return;
  69. }
  70. }
  71. // 3, 导出图像文件
  72. if(ctx->pUser) { sw_thrd_destroy(ctx->pUser, WAITTHRD_SAFEEXIT_TIMEOUT); ctx->pUser = NULL; }
  73. ctx->imgWidth = pFrame->frameInfo.width; ctx->imgHeight = pFrame->frameInfo.height;
  74. ret = SavePhoto(ctx->hCam, pFrame, ctx->saveImgType, ctx->saveImgPath);
  75. // 4, 设置拍照完成
  76. end_p:
  77. ctx->rCode = ret; sw_signal_give(ctx->hESig);
  78. }
  79. static int FrameSoftTrigger(unsigned long wParam, unsigned long lParam) // 线程回调函数, 触发相机拍照
  80. {
  81. PthotoProcCtx *ctx = (PthotoProcCtx *)wParam;
  82. void *hThrd = ctx->pUser; int slot_ms = 100, tick_ms, ret;
  83. while(sw_thrd_isAlive(hThrd))
  84. {
  85. sw_log_info("[%s] 触发一次拍照", MODULE_NAME);
  86. ret = IMV_ExecuteCommandFeature(ctx->hCam, "TriggerSoftware");
  87. if(IMV_OK != ret) { sw_log_error("[%s] 触发拍照失败, errCode=%d!!", MODULE_NAME, ret); break; }
  88. else tick_ms = 0;
  89. while(sw_thrd_isAlive(hThrd) && tick_ms < 1000) { sw_thrd_delay(slot_ms); tick_ms += slot_ms; }
  90. }
  91. return -1;
  92. }
  93. // 单次拍照时, 如果相机处于自动曝光模式, 是否等待曝光稳定后再拍照
  94. int DH_SetWaitAEDoneForTakePhoto(int waitAE)
  95. {
  96. waitAEDone = (waitAE != 0);
  97. return 0;
  98. }
  99. // 单次执行相机拍照, 并保存到文件, 成功返回: 0值, 失败返回:非0值
  100. // "imgType" - 获取图像类型
  101. // "saveImgPath" - 保存的文件名
  102. // "timeout" - 等待超时时间, 单位:秒: < 0 表示无超时;
  103. // 无论超时怎么设置, 首次尝试拍照一定会执行
  104. // , 但时间不确定; 拍照成功、超时或发生错误
  105. // 时会自动结束任务(相机持续无数据的时间).
  106. // "pImgMark" - 输出本次拍照图像的水印信息, 可以设置NULL
  107. int DH_TakePhoto(DHImgType imgType, const char *saveImgPath, int timeout, DHImgMark *pImgMark)
  108. {
  109. int ret, index = 0; DHImgMark imgMark = { 0 }; HANDLE hCam = NULL; PthotoProcCtx ctx = { 0 };
  110. IMV_DeviceList devList = { 0 }; IMV_DeviceInfo *pDevInfo = NULL; IMV_String exposureMode = { 0 };
  111. // 1, 查找相机, 获取其设备信息
  112. ret = IMV_EnumDevices(&devList, device_type);
  113. if(IMV_OK != ret)
  114. {
  115. sw_log_error("[%s] 枚举相机,执行错误, errCode=%d!!", MODULE_NAME, ret);
  116. goto end_p;
  117. }
  118. if(devList.nDevNum < 1)
  119. {
  120. ret = -1;
  121. sw_log_error("[%s] 没有相机,数量=%u!!", MODULE_NAME, devList.nDevNum);
  122. goto end_p;
  123. }
  124. findp:
  125. pDevInfo = &devList.pDevInfo[index++];
  126. if(!pDevInfo)
  127. {
  128. ret = -2;
  129. sw_log_error("[%s] unexpected internal error, failed to obtain camera information!!", MODULE_NAME);
  130. goto end_p;
  131. }
  132. if(pDevInfo->nCameraType == typeU3vCamera) ctx.camType = interfaceTypeUsb3;
  133. else if(pDevInfo->nCameraType == typeGigeCamera) ctx.camType = interfaceTypeGige;
  134. else { // SDK出错, 我只枚举了U口和G口相机, 不可能出现其他类型
  135. ret = -3;
  136. sw_log_error("[%s] unexpected internal error, unknown camera type(%u)!!", MODULE_NAME, pDevInfo->nCameraType);
  137. goto end_p;
  138. }
  139. strcpy(ctx.manuName, pDevInfo->vendorName);
  140. strcpy(imgMark.camModelName, pDevInfo->modelName);
  141. strcpy(imgMark.camSerialNum, pDevInfo->serialNumber);
  142. if(xstrcasecmp(ctx.manuName, manufacturer) != 0 && !is_huaray_manu_alias(ctx.manuName))
  143. {
  144. if(index < devList.nDevNum) goto findp;
  145. ret = -4;
  146. sw_log_error("[%s] 没有找到 \"%s\" 的相机!!", MODULE_NAME, manufacturer);
  147. goto end_p;
  148. }
  149. // 2, 打开相机, 设置其触发模式
  150. unsigned int cameraIndex = (index - 1);
  151. ret = IMV_CreateHandle(&hCam, modeByIndex, (void*)&cameraIndex);
  152. if(IMV_OK == ret) ret = IMV_Open(hCam);
  153. if(IMV_OK != ret)
  154. {
  155. sw_log_error("[%s] 打开相机时发生错误, errCode=%d!!", MODULE_NAME, ret);
  156. goto end_p;
  157. }
  158. if(IMV_OK == ret && xstrcasecmp(setTriggerMode, "On") == 0) ret = IMV_SetEnumFeatureSymbol(hCam, "TriggerSelector", "FrameStart"); // 设置触发条件
  159. if(IMV_OK == ret && xstrcasecmp(setTriggerMode, "On") == 0) ret = IMV_SetEnumFeatureSymbol(hCam, "TriggerSource", "Software"); // 设置软件触发
  160. if(IMV_OK == ret) ret = IMV_SetEnumFeatureSymbol(hCam, "TriggerMode", setTriggerMode); // 设置触发模式
  161. if(IMV_OK != ret)
  162. {
  163. sw_log_error("[%s] 设置相机时发生错误, errCode=%d!!", MODULE_NAME, ret);
  164. goto end_p;
  165. }
  166. // 3, 开始拍照, 等待完成后输出
  167. ret = IMV_GetEnumFeatureSymbol(hCam, "ExposureAuto", &exposureMode);
  168. if(IMV_OK != ret)
  169. {
  170. sw_log_error("[%s] 获取曝光模式时出错, errCode=%d!!", MODULE_NAME, ret);
  171. goto end_p;
  172. }
  173. if(xstrcasecmp(exposureMode.str, "Off") != 0) { ctx.isExposureAuto = true; clock_gettime(CLOCK_MONOTONIC, &ctx.expTime0); }
  174. else ctx.isExposureAuto = false;
  175. ctx.hCam = hCam;
  176. ctx.saveImgType = imgType;
  177. ctx.saveImgPath = saveImgPath;
  178. ctx.hESig = sw_signal_create();
  179. if(!ctx.hESig)
  180. {
  181. ret = -5;
  182. sw_log_error("[%s] SIG信号量创建失败!!", MODULE_NAME);
  183. goto end_p;
  184. }
  185. ctx.rCode = IMV_OK;
  186. ret = IMV_AttachGrabbing(hCam, OnFrameReceived, (void *)&ctx);
  187. if(IMV_OK != ret)
  188. {
  189. sw_log_error("[%s] 注册回调时发生错误, errCode=%d!!", MODULE_NAME, ret);
  190. goto end_p;
  191. }
  192. ret = IMV_StartGrabbing(hCam);
  193. if(IMV_OK != ret)
  194. {
  195. sw_log_error("[%s] 相机取流时发生错误, errCode=%d!!", MODULE_NAME, ret);
  196. goto end_p;
  197. }
  198. HANDLE hThrd = NULL;
  199. if(0 != xstrcasecmp(setTriggerMode, "On")) goto waitp;
  200. hThrd = sw_thrd_create("PhotoProc", THREAD_DEFAULT_PRIORITY, THREAD_DEFAULT_STACK_SIZE, FrameSoftTrigger, (unsigned long)&ctx, 0);
  201. if(!hThrd)
  202. {
  203. ret = -6;
  204. sw_signal_destroy(ctx.hESig);
  205. sw_log_error("[%s] 触发线程-创建失败!!", MODULE_NAME);
  206. goto end_p;
  207. }
  208. ctx.pUser = (void *)hThrd; sw_thrd_resume(hThrd);
  209. waitp:
  210. ret = sw_signal_wait(ctx.hESig, timeout*1000); // 阻塞等待拍照任务结束或超时
  211. if(0 == ret) ret = ctx.rCode;
  212. else
  213. {
  214. ret = -7;
  215. sw_log_error("[%s] 拍照过程-等待超时!!", MODULE_NAME);
  216. }
  217. if(ctx.pUser) sw_thrd_destroy(ctx.pUser, WAITTHRD_SAFEEXIT_TIMEOUT);
  218. sw_signal_destroy(ctx.hESig);
  219. // 4, 成功拍照, 输出相机的信息
  220. if(IMV_OK == ret)
  221. {
  222. double curExpTime = 0.0;
  223. IMV_GetDoubleFeatureValue(hCam, "ExposureTime", &curExpTime);
  224. imgMark.imgExposureTime = curExpTime;
  225. imgMark.imgWidth = ctx.imgWidth; imgMark.imgHeight = ctx.imgHeight;
  226. if(pImgMark) memcpy(pImgMark, &imgMark, sizeof(DHImgMark));
  227. }
  228. // 5, 任务结束, 释放相关的资源
  229. end_p:
  230. if(hCam)
  231. {
  232. IMV_StopGrabbing(hCam);
  233. IMV_Close(hCam);
  234. IMV_DestroyHandle(hCam);
  235. }
  236. return ret;
  237. }
  238. // 保存给定的数据帧到BMP图像文件, 成功返回: 0值, 失败返回: 非0值
  239. static int SaveFrameToBmp(IMV_HANDLE hCam, IMV_Frame *pFrame, const char *path)
  240. {
  241. IMV_PixelConvertParam stPixelConvertParam; int ret = IMV_OK;
  242. unsigned char *pDstBuf = NULL; unsigned int nDstBufSize = 0;
  243. nDstBufSize = sizeof(unsigned char) * pFrame->frameInfo.width * pFrame->frameInfo.height * 3;
  244. pDstBuf = (unsigned char*)malloc(nDstBufSize);
  245. if(!pDstBuf) return -18;
  246. memset(&stPixelConvertParam, 0, sizeof(stPixelConvertParam));
  247. stPixelConvertParam.nWidth = pFrame->frameInfo.width;
  248. stPixelConvertParam.nHeight = pFrame->frameInfo.height;
  249. stPixelConvertParam.ePixelFormat = pFrame->frameInfo.pixelFormat;
  250. stPixelConvertParam.pSrcData = pFrame->pData;
  251. stPixelConvertParam.nSrcDataLen = pFrame->frameInfo.size;
  252. stPixelConvertParam.nPaddingX = pFrame->frameInfo.paddingX;
  253. stPixelConvertParam.nPaddingY = pFrame->frameInfo.paddingY;
  254. stPixelConvertParam.eBayerDemosaic = demosaicNearestNeighbor;
  255. stPixelConvertParam.eDstPixelFormat = gvspPixelRGB8;
  256. stPixelConvertParam.pDstBuf = pDstBuf;
  257. stPixelConvertParam.nDstBufSize = nDstBufSize;
  258. ret = IMV_PixelConvert(hCam, &stPixelConvertParam);
  259. if(IMV_OK != ret) goto ret_p;
  260. ret = stbi_write_bmp(path, pFrame->frameInfo.width, pFrame->frameInfo.height, 3, pDstBuf);
  261. if(1 == ret) ret = IMV_OK;
  262. else ret = -19;
  263. ret_p:
  264. if(pDstBuf) free(pDstBuf);
  265. return ret;
  266. }
  267. // 保存给定的数据帧到JPG图像文件, 成功返回: 0值, 失败返回: 非0值
  268. static int SaveFrameToJpg(IMV_HANDLE hCam, IMV_Frame *pFrame, const char *path)
  269. {
  270. IMV_PixelConvertParam stPixelConvertParam; int ret = IMV_OK;
  271. unsigned char *pDstBuf = NULL; unsigned int nDstBufSize = 0;
  272. nDstBufSize = sizeof(unsigned char) * pFrame->frameInfo.width * pFrame->frameInfo.height * 3;
  273. pDstBuf = (unsigned char*)malloc(nDstBufSize);
  274. if(!pDstBuf) return -18;
  275. memset(&stPixelConvertParam, 0, sizeof(stPixelConvertParam));
  276. stPixelConvertParam.nWidth = pFrame->frameInfo.width;
  277. stPixelConvertParam.nHeight = pFrame->frameInfo.height;
  278. stPixelConvertParam.ePixelFormat = pFrame->frameInfo.pixelFormat;
  279. stPixelConvertParam.pSrcData = pFrame->pData;
  280. stPixelConvertParam.nSrcDataLen = pFrame->frameInfo.size;
  281. stPixelConvertParam.nPaddingX = pFrame->frameInfo.paddingX;
  282. stPixelConvertParam.nPaddingY = pFrame->frameInfo.paddingY;
  283. stPixelConvertParam.eBayerDemosaic = demosaicNearestNeighbor;
  284. stPixelConvertParam.eDstPixelFormat = gvspPixelRGB8;
  285. stPixelConvertParam.pDstBuf = pDstBuf;
  286. stPixelConvertParam.nDstBufSize = nDstBufSize;
  287. ret = IMV_PixelConvert(hCam, &stPixelConvertParam);
  288. if(IMV_OK != ret) goto ret_p;
  289. ret = stbi_write_jpg(path, pFrame->frameInfo.width, pFrame->frameInfo.height, 3, pDstBuf, 90);
  290. if(1 == ret) ret = IMV_OK;
  291. else ret = -19;
  292. ret_p:
  293. if(pDstBuf) free(pDstBuf);
  294. return ret;
  295. }
  296. // 保存给定的数据帧到指定格式("BMP"/"JPG")的图像文件,成功返回0值
  297. static int SavePhoto(HANDLE hCam, IMV_Frame *pFrame, DHImgType imgType, const char *imgPath)
  298. {
  299. char *path1 = (char *)imgPath, *path2 = NULL; int ret = IMV_OK; char ext[5];
  300. if(!hCam || !pFrame || !path1 || strlen(path1) <= 0) return -15;
  301. switch(imgType)
  302. {
  303. case IMG_TYPE_BMP: // 保存为.bmp格式
  304. strcpy(ext, ".bmp");
  305. break;
  306. case IMG_TYPE_JPG: // 保存为.jpg格式
  307. strcpy(ext, ".jpg");
  308. break;
  309. default: return -16;
  310. }
  311. if(!xstrcasestr(path1, "right", ext))
  312. {
  313. path2 = (char *)sw_heap_malloc(strlen(path1) + sizeof(ext));
  314. if(path2) sprintf(path2, "%s%s", path1, ext);
  315. else return -17;
  316. if(imgType == IMG_TYPE_BMP) ret = SaveFrameToBmp(hCam, pFrame, path2);
  317. if(imgType == IMG_TYPE_JPG) ret = SaveFrameToJpg(hCam, pFrame, path2);
  318. sw_heap_free(path2);
  319. }
  320. else
  321. {
  322. if(imgType == IMG_TYPE_BMP) ret = SaveFrameToBmp(hCam, pFrame, path1);
  323. if(imgType == IMG_TYPE_JPG) ret = SaveFrameToJpg(hCam, pFrame, path1);
  324. }
  325. return ret;
  326. }
  327. // 获取系统当前USBFS内存大小(MB), 成功返回: >=0, 失败返回: <0值
  328. int DH_GetSysUsbfsMemCurrentSize()
  329. {
  330. const char *usbfsFile = "/sys/module/usbcore/parameters/usbfs_memory_mb";
  331. char buf[MAX_LINE_CHARS]; int val = -1;
  332. FILE *fp = fopen(usbfsFile, "r");
  333. if(fp)
  334. {
  335. if(fgets(buf, sizeof(buf), fp)) sscanf(buf, "%d", &val);
  336. fclose(fp);
  337. }
  338. return val;
  339. }
  340. // 设置系统新的USBFS内存大小(MB), 成功返回: 0值, 失败返回: <0值
  341. int DH_SetSysUsbfsMemSize(int val)
  342. {
  343. const char *usbfsFile = "/sys/module/usbcore/parameters/usbfs_memory_mb";
  344. char buf[MAX_LINE_CHARS]; sprintf(buf, "%d\n", val);
  345. if(val >= 0 && sw_file_update(usbfsFile, "w", buf, strlen(buf)) == strlen(buf)) return 0;
  346. else return -1;
  347. }
  348. // 获取当前已连接的海康相机的数量, 成功返回: >=0, 失败返回: <0值
  349. int DH_GetCameraCount()
  350. {
  351. IMV_DeviceList devList = { 0 };
  352. int ret;
  353. ret = IMV_EnumDevices(&devList, device_type);
  354. if(IMV_OK != ret)
  355. {
  356. sw_log_error("[%s] 枚举相机,执行错误, errCode=%d!!", MODULE_NAME, ret);
  357. return -1;
  358. }
  359. ret = devList.nDevNum;
  360. if(ret > 0)
  361. {
  362. int index = 0, cnt = 0; IMV_DeviceInfo *pDevInfo; char name[MAX_LINE_CHARS];
  363. findp:
  364. pDevInfo = &devList.pDevInfo[index++];
  365. name[0] = '\0';
  366. if(pDevInfo && (pDevInfo->nCameraType == typeU3vCamera || pDevInfo->nCameraType == typeGigeCamera)) strcpy(name, pDevInfo->vendorName);
  367. if(xstrcasecmp(name, manufacturer) == 0 || is_huaray_manu_alias(name)) cnt++;
  368. if(index < devList.nDevNum) goto findp;
  369. else ret = cnt;
  370. }
  371. return ret;
  372. }