niujiuru пре 2 месеци
родитељ
комит
6b51bc4764
3 измењених фајлова са 38 додато и 6 уклоњено
  1. 1 1
      dh_takephoto/Makefile
  2. 32 1
      dh_takephoto/takephoto.c
  3. 5 4
      hk_takephoto/takephoto.c

+ 1 - 1
dh_takephoto/Makefile

@@ -17,7 +17,7 @@ CC := gcc
 CFLAGS := -Wall -fPIC -O2 -g
 DEFINS := -D_GNU_SOURCE
 
-target ?= x86_64
+target ?= armv7hf
 ifeq ($(target),armv7hf)
   CC := arm-linux-gnueabihf-gcc
 	AR := arm-linux-gnueabihf-ar

+ 32 - 1
dh_takephoto/takephoto.c

@@ -18,6 +18,7 @@ static const char * const setTriggerMode = "On"; /// 默认: 打开,  可关闭:
 #define AE_EXP_STABLE_FRAMES    4 // 相机处于自动曝光模式时, 累计曝光值稳定的帧数
 
 // 保存照片
+static int SavePhoto(HANDLE hCam, IMV_Frame *pFrame, DHImgType imgType, const char *imgFile);
 
 // 拍照回调
 typedef struct
@@ -48,11 +49,41 @@ static bool is_huaray_manu_alias(const char *name)
 
 static void OnFrameReceived(IMV_Frame *pFrame, void *pUser)             // 数据帧的回调, 完成一次拍照任务
 {
-  PthotoProcCtx *ctx = (PthotoProcCtx *)pUser;
+  PthotoProcCtx *ctx = (PthotoProcCtx *)pUser; int ret = IMV_OK;
   if(NULL == pFrame) return;
+
+  // 1, 打印该帧信息
   sw_log_debug("[%s] +++GetOneFrame+++, Width[%u], Height[%u], FrameNum[%llu], FrameLen[%u], PixelType[0x%08X]", MODULE_NAME,
                 pFrame->frameInfo.width, pFrame->frameInfo.height, pFrame->frameInfo.blockId, pFrame->frameInfo.size,
                 (unsigned int)pFrame->frameInfo.pixelFormat);
+
+  // 2, 自动曝光模式
+  if(ctx->isExposureAuto)
+  {
+    double curExpTime = 0.0;
+    ret = IMV_GetDoubleFeatureValue(ctx->hCam, "ExposureTime", &curExpTime);
+    if(IMV_OK != ret) goto endp;
+
+    struct timespec now; clock_gettime(CLOCK_MONOTONIC, &now);
+    long elapsed = (now.tv_sec - ctx->expTime0.tv_sec)*1000 + (now.tv_nsec-ctx->expTime0.tv_nsec)/(1000*1000); // 计算时间差, 单位: ms
+    sw_log_debug("[%s] +++GetOneFrame+++, 等待曝光完成, ExposureTime = %.2fus, ElapsedTime = %ldms", MODULE_NAME, curExpTime, elapsed);
+
+    if(fabsf(curExpTime - ctx->lastExpTime) <= AE_EXP_TIME_EPS_US) ctx->expStableCnt++;
+    else ctx->expStableCnt = 0;
+
+    if(ctx->expStableCnt < AE_EXP_STABLE_FRAMES && elapsed < AE_WAIT_MAX_MS)
+    { // 曝光未稳定, 也未超时, 则继续等下一帧
+      ctx->lastExpTime = curExpTime; if(0 == ctx->expStableCnt) ctx->expStableCnt = 1;
+      return;
+    }
+  }
+
+  // 3, 导出图像文件
+  ret = SavePhoto(ctx->hCam, pFrame, ctx->saveImgType, ctx->saveImgPath);
+
+  // 4, 设置拍照完成
+endp:
+  ctx->rCode = ret; sw_signal_give(ctx->hESig);
 }
 
 static int FrameSoftTrigger(unsigned long wParam, unsigned long lParam) // 线程回调函数, 触发一次拍照执行

+ 5 - 4
hk_takephoto/takephoto.c

@@ -72,8 +72,8 @@ static int PhotoProc(unsigned long wParam, unsigned long lParam) // 线程回调
     if(ctx->isExposureAuto)
     { // 自动曝光模式下, 等待曝光稳定或超时
       MVCC_FLOATVALUE stExposureTime = { 0 }; float curExpTime = 0;
-      ret = MV_CC_GetExposureTime(ctx->hCam, &stExposureTime);
-      if(MV_OK == ret) curExpTime = stExposureTime.fCurValue;
+      ret = MV_CC_GetExposureTime(ctx->hCam, &stExposureTime); if(MV_OK != ret) goto retp1;
+      curExpTime = stExposureTime.fCurValue;
 
       struct timespec now; clock_gettime(CLOCK_MONOTONIC, &now);
       long elapsed = (now.tv_sec - ctx->expTime0.tv_sec)*1000 + (now.tv_nsec-ctx->expTime0.tv_nsec)/(1000*1000); // 计算时间差, 单位: ms
@@ -86,7 +86,7 @@ static int PhotoProc(unsigned long wParam, unsigned long lParam) // 线程回调
       {
         MV_CC_FreeImageBuffer(ctx->hCam, &frame);
         ctx->lastExpTime = curExpTime; if(0 == ctx->expStableCnt) ctx->expStableCnt = 1;
-        ret = 1; goto retp; /// 继续取下一帧
+        ret = 1; goto retp2; // 继续取下一帧
       }
     }
 
@@ -95,6 +95,7 @@ static int PhotoProc(unsigned long wParam, unsigned long lParam) // 线程回调
   }
 
   // 4, 控制线程退出
+retp1:
   switch(ret)
   {
   case MV_E_NODATA: // 无数据时, 线程继续运行
@@ -104,7 +105,7 @@ static int PhotoProc(unsigned long wParam, unsigned long lParam) // 线程回调
     ctx->rCode = ret; sw_signal_give(ctx->hESig); ret = -1; break;
   }
 
-retp:
+retp2:
   return ret;
 }