Blame view

src/reprocessing_module/save_snapshot_reprocessing.cpp 3.66 KB
09c2d08c   Hu Chunming   arm交付版
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
  #include "save_snapshot_reprocessing.h"
  #include <thread>
  
  #include "opencv2/opencv.hpp"
  #include <opencv2/imgproc/types_c.h>
  #include <algorithm>
  #include "../common/logger.hpp"
  #include "../util/vpc_util.h"
  
  const bool DRAW_ON_IMG = false;
  
  int save_img_thread_process(void* param) {
    save_snapshot_reprocessing *pThreadParam = (save_snapshot_reprocessing *)param;
    if (pThreadParam != nullptr){
      pThreadParam->save_img_process();
    }
    return 0;
  }
  
  // 初始化快照保存模块 开启图片保存线程
  save_snapshot_reprocessing::save_snapshot_reprocessing(int devId) {
  #ifdef POST_USE_RABBITMQ
    callback_ = nullptr;
  #endif
  
    bFinish = false;
    m_save_img_thread = std::thread(save_img_thread_process, this);
  
    m_devId = devId;
    jpegUtil.jpeg_init(m_devId);
  }
  
  save_snapshot_reprocessing::~save_snapshot_reprocessing(){
    // 结束线程
    bFinish = true;
    m_save_img_thread.join();
    jpegUtil.jpeg_release();
  }
  
  // 释放资源
  void save_snapshot_reprocessing::save_snapshot_reprocessing_release() {
  
    std::unique_lock<std::mutex> l(waitforsave_img_queue_mutex);
  
    while (!waitforsave_img_queue.empty()) {
      ImgSaveInfo cur_image = waitforsave_img_queue.front();
      waitforsave_img_queue.pop();
  
      if(!cur_image.file_path.empty()){
        VPCUtil::vpc_img_release(cur_image.img_info);
      }
    }
  
    l.unlock();
  }
  
  #ifdef POST_USE_RABBITMQ
  
  // 设置MQ返回回调函数 方便内部调用MQ推送结果
  void save_snapshot_reprocessing::set_callback(callback_t cb) {
    callback_ = cb;
  }
  
  #endif // #ifdef POST_USE_RABBITMQ
  
  
d8ebfce2   Zhao Shuaihua   调整抓拍图保存
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
  void save_snapshot_reprocessing::reprocessing_process_wo_locus(ImgSaveInfo saveInfo) {
    if (!bFinish) {
        bool bSaved = false;
        if(!saveInfo.file_path.empty()){
          bSaved = jpegUtil.jpeg_encode(saveInfo.img_info.pic_desc, saveInfo.file_path);
        }
        VPCUtil::vpc_img_release(saveInfo.img_info);
  
  #ifdef POST_USE_RABBITMQ
        // LOG_INFO("mq publish process in: {}", saveInfo.json_str);
        if (bSaved && callback_ != nullptr && saveInfo.json_str.length() > 0) {
          // LOG_INFO("mq publish process begin");
          callback_(saveInfo.json_str.c_str());
          LOG_INFO("mq publish process end: {} ", saveInfo.json_str);
        }
  #endif
    }
  
  }
  
09c2d08c   Hu Chunming   arm交付版
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
  void save_snapshot_reprocessing::reprocessing_process_wo_locus_async(ImgSaveInfo saveInfo){
  
     while(!bFinish){
      waitforsave_img_queue_mutex.lock();
      if(waitforsave_img_queue.size() > 100){
        waitforsave_img_queue_mutex.unlock();
        std::this_thread::sleep_for(std::chrono::milliseconds(5));
        continue;
      }
      waitforsave_img_queue.push(saveInfo);
      waitforsave_img_queue_mutex.unlock();
      break;
    }
    
  }
  
  void save_snapshot_reprocessing::save_img_process() {
    while (true) {
      if (bFinish){
        break;
      }
  
      std::unique_lock<std::mutex> l(waitforsave_img_queue_mutex);
      if (!waitforsave_img_queue.empty()) {
        LOG_DEBUG("waitforsave_image_queue size: {}", waitforsave_img_queue.size());
        ImgSaveInfo cur_image = waitforsave_img_queue.front();
        waitforsave_img_queue.pop();
        l.unlock();
  
28ca1c66   Zhao Shuaihua   代码优化
116
        bool bSaved = false;
09c2d08c   Hu Chunming   arm交付版
117
        if(!cur_image.file_path.empty()){
28ca1c66   Zhao Shuaihua   代码优化
118
          bSaved = jpegUtil.jpeg_encode(cur_image.img_info.pic_desc, cur_image.file_path);
09c2d08c   Hu Chunming   arm交付版
119
120
121
122
123
        }
        VPCUtil::vpc_img_release(cur_image.img_info);
  
  #ifdef POST_USE_RABBITMQ
        // LOG_INFO("mq publish process in: {}", cur_image.json_str);
28ca1c66   Zhao Shuaihua   代码优化
124
        if (bSaved && callback_ != nullptr && cur_image.json_str.length() > 0) {
09c2d08c   Hu Chunming   arm交付版
125
126
127
128
129
130
131
132
133
134
135
136
          // LOG_INFO("mq publish process begin");
          callback_(cur_image.json_str.c_str());
          LOG_INFO("mq publish process end: {} ", cur_image.json_str);
        }
  #endif
  
      } else {
        l.unlock();
        std::this_thread::sleep_for(std::chrono::milliseconds(2));
      }
  
    }
d8ebfce2   Zhao Shuaihua   调整抓拍图保存
137
  }