ClassFlowTakeImage.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <regex>
  5. #include "ClassFlowTakeImage.h"
  6. #include "Helper.h"
  7. #include "ClassLogFile.h"
  8. #include "CImageBasis.h"
  9. #include "ClassControllCamera.h"
  10. #include "MainFlowControl.h"
  11. #include "esp_wifi.h"
  12. #include "esp_log.h"
  13. #include "../../include/defines.h"
  14. #include "psram.h"
  15. #include <time.h>
  16. // #define DEBUG_DETAIL_ON
  17. // #define WIFITURNOFF
  18. static const char *TAG = "TAKEIMAGE";
  19. esp_err_t ClassFlowTakeImage::camera_capture(void)
  20. {
  21. string nm = namerawimage;
  22. Camera.CaptureToFile(nm);
  23. time(&TimeImageTaken);
  24. localtime(&TimeImageTaken);
  25. return ESP_OK;
  26. }
  27. void ClassFlowTakeImage::takePictureWithFlash(int flash_duration)
  28. {
  29. // in case the image is flipped, it must be reset here //
  30. rawImage->width = CCstatus.ImageWidth;
  31. rawImage->height = CCstatus.ImageHeight;
  32. ESP_LOGD(TAG, "flash_duration: %d", flash_duration);
  33. Camera.CaptureToBasisImage(rawImage, flash_duration);
  34. time(&TimeImageTaken);
  35. localtime(&TimeImageTaken);
  36. if (CCstatus.SaveAllFiles)
  37. {
  38. rawImage->SaveToFile(namerawimage);
  39. }
  40. }
  41. void ClassFlowTakeImage::SetInitialParameter(void)
  42. {
  43. TimeImageTaken = 0;
  44. rawImage = NULL;
  45. disabled = false;
  46. namerawimage = "/sdcard/img_tmp/raw.jpg";
  47. }
  48. // auslesen der Kameraeinstellungen aus der config.ini
  49. // wird beim Start aufgerufen
  50. bool ClassFlowTakeImage::ReadParameter(FILE *pfile, string &aktparamgraph)
  51. {
  52. Camera.getSensorDatenToCCstatus(); // Kamera >>> CCstatus
  53. std::vector<string> splitted;
  54. aktparamgraph = trim(aktparamgraph);
  55. if (aktparamgraph.size() == 0)
  56. {
  57. if (!this->GetNextParagraph(pfile, aktparamgraph))
  58. {
  59. return false;
  60. }
  61. }
  62. if (aktparamgraph.compare("[TakeImage]") != 0)
  63. {
  64. // Paragraph does not fit TakeImage
  65. return false;
  66. }
  67. while (this->getNextLine(pfile, &aktparamgraph) && !this->isNewParagraph(aktparamgraph))
  68. {
  69. splitted = ZerlegeZeile(aktparamgraph);
  70. if ((toUpper(splitted[0]) == "RAWIMAGESLOCATION") && (splitted.size() > 1))
  71. {
  72. imagesLocation = "/sdcard" + splitted[1];
  73. isLogImage = true;
  74. }
  75. else if ((toUpper(splitted[0]) == "RAWIMAGESRETENTION") && (splitted.size() > 1))
  76. {
  77. if (isStringNumeric(splitted[1]))
  78. {
  79. this->imagesRetention = std::stod(splitted[1]);
  80. }
  81. }
  82. else if ((toUpper(splitted[0]) == "SAVEALLFILES") && (splitted.size() > 1))
  83. {
  84. CCstatus.SaveAllFiles = alphanumericToBoolean(splitted[1]);
  85. }
  86. else if ((toUpper(splitted[0]) == "WAITBEFORETAKINGPICTURE") && (splitted.size() > 1))
  87. {
  88. if (isStringNumeric(splitted[1]))
  89. {
  90. int _WaitBeforePicture = std::stoi(splitted[1]);
  91. if (_WaitBeforePicture != 0)
  92. {
  93. CCstatus.WaitBeforePicture = _WaitBeforePicture;
  94. }
  95. else
  96. {
  97. CCstatus.WaitBeforePicture = 2;
  98. }
  99. }
  100. }
  101. else if ((toUpper(splitted[0]) == "CAMGAINCEILING") && (splitted.size() > 1))
  102. {
  103. std::string _ImageGainceiling = toUpper(splitted[1]);
  104. if (_ImageGainceiling == "X4")
  105. {
  106. CCstatus.ImageGainceiling = GAINCEILING_4X;
  107. }
  108. else if (_ImageGainceiling == "X8")
  109. {
  110. CCstatus.ImageGainceiling = GAINCEILING_8X;
  111. }
  112. else if (_ImageGainceiling == "X16")
  113. {
  114. CCstatus.ImageGainceiling = GAINCEILING_16X;
  115. }
  116. else if (_ImageGainceiling == "X32")
  117. {
  118. CCstatus.ImageGainceiling = GAINCEILING_32X;
  119. }
  120. else if (_ImageGainceiling == "X64")
  121. {
  122. CCstatus.ImageGainceiling = GAINCEILING_64X;
  123. }
  124. else if (_ImageGainceiling == "X128")
  125. {
  126. CCstatus.ImageGainceiling = GAINCEILING_128X;
  127. }
  128. else
  129. {
  130. CCstatus.ImageGainceiling = GAINCEILING_2X;
  131. }
  132. }
  133. else if ((toUpper(splitted[0]) == "CAMQUALITY") && (splitted.size() > 1))
  134. {
  135. if (isStringNumeric(splitted[1]))
  136. {
  137. int _ImageQuality = std::stoi(splitted[1]);
  138. CCstatus.ImageQuality = clipInt(_ImageQuality, 63, 6);
  139. }
  140. }
  141. else if ((toUpper(splitted[0]) == "CAMBRIGHTNESS") && (splitted.size() > 1))
  142. {
  143. if (isStringNumeric(splitted[1]))
  144. {
  145. int _ImageBrightness = std::stoi(splitted[1]);
  146. CCstatus.ImageBrightness = clipInt(_ImageBrightness, 2, -2);
  147. }
  148. }
  149. else if ((toUpper(splitted[0]) == "CAMCONTRAST") && (splitted.size() > 1))
  150. {
  151. if (isStringNumeric(splitted[1]))
  152. {
  153. int _ImageContrast = std::stoi(splitted[1]);
  154. CCstatus.ImageContrast = clipInt(_ImageContrast, 2, -2);
  155. }
  156. }
  157. else if ((toUpper(splitted[0]) == "CAMSATURATION") && (splitted.size() > 1))
  158. {
  159. if (isStringNumeric(splitted[1]))
  160. {
  161. int _ImageSaturation = std::stoi(splitted[1]);
  162. CCstatus.ImageSaturation = clipInt(_ImageSaturation, 2, -2);
  163. }
  164. }
  165. else if ((toUpper(splitted[0]) == "CAMSHARPNESS") && (splitted.size() > 1))
  166. {
  167. if (isStringNumeric(splitted[1]))
  168. {
  169. int _ImageSharpness = std::stoi(splitted[1]);
  170. CCstatus.ImageSharpness = clipInt(_ImageSharpness, 2, -2);
  171. }
  172. }
  173. else if ((toUpper(splitted[0]) == "CAMAUTOSHARPNESS") && (splitted.size() > 1))
  174. {
  175. CCstatus.ImageAutoSharpness = alphanumericToBoolean(splitted[1]);
  176. }
  177. else if ((toUpper(splitted[0]) == "CAMSPECIALEFFECT") && (splitted.size() > 1))
  178. {
  179. std::string _ImageSpecialEffect = toUpper(splitted[1]);
  180. if (_ImageSpecialEffect == "NEGATIVE")
  181. {
  182. CCstatus.ImageSpecialEffect = 1;
  183. }
  184. else if (_ImageSpecialEffect == "GRAYSCALE")
  185. {
  186. CCstatus.ImageSpecialEffect = 2;
  187. }
  188. else if (_ImageSpecialEffect == "RED")
  189. {
  190. CCstatus.ImageSpecialEffect = 3;
  191. }
  192. else if (_ImageSpecialEffect == "GREEN")
  193. {
  194. CCstatus.ImageSpecialEffect = 4;
  195. }
  196. else if (_ImageSpecialEffect == "BLUE")
  197. {
  198. CCstatus.ImageSpecialEffect = 5;
  199. }
  200. else if (_ImageSpecialEffect == "RETRO")
  201. {
  202. CCstatus.ImageSpecialEffect = 6;
  203. }
  204. else
  205. {
  206. CCstatus.ImageSpecialEffect = 0;
  207. }
  208. }
  209. else if ((toUpper(splitted[0]) == "CAMWBMODE") && (splitted.size() > 1))
  210. {
  211. std::string _ImageWbMode = toUpper(splitted[1]);
  212. if (_ImageWbMode == "SUNNY")
  213. {
  214. CCstatus.ImageWbMode = 1;
  215. }
  216. else if (_ImageWbMode == "CLOUDY")
  217. {
  218. CCstatus.ImageWbMode = 2;
  219. }
  220. else if (_ImageWbMode == "OFFICE")
  221. {
  222. CCstatus.ImageWbMode = 3;
  223. }
  224. else if (_ImageWbMode == "HOME")
  225. {
  226. CCstatus.ImageWbMode = 4;
  227. }
  228. else
  229. {
  230. CCstatus.ImageWbMode = 0;
  231. }
  232. }
  233. else if ((toUpper(splitted[0]) == "CAMAWB") && (splitted.size() > 1))
  234. {
  235. CCstatus.ImageAwb = alphanumericToBoolean(splitted[1]);
  236. }
  237. else if ((toUpper(splitted[0]) == "CAMAWBGAIN") && (splitted.size() > 1))
  238. {
  239. CCstatus.ImageAwbGain = alphanumericToBoolean(splitted[1]);
  240. }
  241. else if ((toUpper(splitted[0]) == "CAMAEC") && (splitted.size() > 1))
  242. {
  243. CCstatus.ImageAec = alphanumericToBoolean(splitted[1]);
  244. }
  245. else if ((toUpper(splitted[0]) == "CAMAEC2") && (splitted.size() > 1))
  246. {
  247. CCstatus.ImageAec2 = alphanumericToBoolean(splitted[1]);
  248. }
  249. else if ((toUpper(splitted[0]) == "CAMAELEVEL") && (splitted.size() > 1))
  250. {
  251. if (isStringNumeric(splitted[1]))
  252. {
  253. int _ImageAeLevel = std::stoi(splitted[1]);
  254. CCstatus.ImageAeLevel = clipInt(_ImageAeLevel, 2, -2);
  255. }
  256. }
  257. else if ((toUpper(splitted[0]) == "CAMAECVALUE") && (splitted.size() > 1))
  258. {
  259. if (isStringNumeric(splitted[1]))
  260. {
  261. int _ImageAecValue = std::stoi(splitted[1]);
  262. CCstatus.ImageAecValue = clipInt(_ImageAecValue, 1200, 0);
  263. }
  264. }
  265. else if ((toUpper(splitted[0]) == "CAMAGC") && (splitted.size() > 1))
  266. {
  267. CCstatus.ImageAgc = alphanumericToBoolean(splitted[1]);
  268. }
  269. else if ((toUpper(splitted[0]) == "CAMAGCGAIN") && (splitted.size() > 1))
  270. {
  271. if (isStringNumeric(splitted[1]))
  272. {
  273. int _ImageAgcGain = std::stoi(splitted[1]);
  274. CCstatus.ImageAgcGain = clipInt(_ImageAgcGain, 30, 0);
  275. }
  276. }
  277. else if ((toUpper(splitted[0]) == "CAMBPC") && (splitted.size() > 1))
  278. {
  279. CCstatus.ImageBpc = alphanumericToBoolean(splitted[1]);
  280. }
  281. else if ((toUpper(splitted[0]) == "CAMWPC") && (splitted.size() > 1))
  282. {
  283. CCstatus.ImageWpc = alphanumericToBoolean(splitted[1]);
  284. }
  285. else if ((toUpper(splitted[0]) == "CAMRAWGMA") && (splitted.size() > 1))
  286. {
  287. CCstatus.ImageRawGma = alphanumericToBoolean(splitted[1]);
  288. }
  289. else if ((toUpper(splitted[0]) == "CAMLENC") && (splitted.size() > 1))
  290. {
  291. CCstatus.ImageLenc = alphanumericToBoolean(splitted[1]);
  292. }
  293. else if ((toUpper(splitted[0]) == "CAMHMIRROR") && (splitted.size() > 1))
  294. {
  295. CCstatus.ImageHmirror = alphanumericToBoolean(splitted[1]);
  296. }
  297. else if ((toUpper(splitted[0]) == "CAMVFLIP") && (splitted.size() > 1))
  298. {
  299. CCstatus.ImageVflip = alphanumericToBoolean(splitted[1]);
  300. }
  301. else if ((toUpper(splitted[0]) == "CAMDCW") && (splitted.size() > 1))
  302. {
  303. CCstatus.ImageDcw = alphanumericToBoolean(splitted[1]);
  304. }
  305. else if ((toUpper(splitted[0]) == "CAMZOOM") && (splitted.size() > 1))
  306. {
  307. CCstatus.ImageZoomEnabled = alphanumericToBoolean(splitted[1]);
  308. }
  309. else if ((toUpper(splitted[0]) == "CAMZOOMOFFSETX") && (splitted.size() > 1))
  310. {
  311. if (isStringNumeric(splitted[1]))
  312. {
  313. CCstatus.ImageZoomOffsetX = std::stoi(splitted[1]);
  314. }
  315. }
  316. else if ((toUpper(splitted[0]) == "CAMZOOMOFFSETY") && (splitted.size() > 1))
  317. {
  318. if (isStringNumeric(splitted[1]))
  319. {
  320. CCstatus.ImageZoomOffsetY = std::stoi(splitted[1]);
  321. }
  322. }
  323. else if ((toUpper(splitted[0]) == "CAMZOOMSIZE") && (splitted.size() > 1))
  324. {
  325. if (isStringNumeric(splitted[1]))
  326. {
  327. CCstatus.ImageZoomSize = std::stoi(splitted[1]);
  328. }
  329. }
  330. else if ((toUpper(splitted[0]) == "LEDINTENSITY") && (splitted.size() > 1))
  331. {
  332. if (isStringNumeric(splitted[1]))
  333. {
  334. float ledintensity = std::stof(splitted[1]);
  335. Camera.SetLEDIntensity(ledintensity);
  336. }
  337. }
  338. else if ((toUpper(splitted[0]) == "DEMO") && (splitted.size() > 1))
  339. {
  340. CCstatus.DemoMode = alphanumericToBoolean(splitted[1]);
  341. if (CCstatus.DemoMode == true)
  342. {
  343. Camera.useDemoMode();
  344. }
  345. }
  346. }
  347. Camera.setSensorDatenFromCCstatus(); // CCstatus >>> Kamera
  348. Camera.SetQualityZoomSize(CCstatus.ImageQuality, CCstatus.ImageFrameSize, CCstatus.ImageZoomEnabled, CCstatus.ImageZoomOffsetX, CCstatus.ImageZoomOffsetY, CCstatus.ImageZoomSize);
  349. rawImage = new CImageBasis("rawImage");
  350. rawImage->CreateEmptyImage(CCstatus.ImageWidth, CCstatus.ImageHeight, 3);
  351. return true;
  352. }
  353. ClassFlowTakeImage::ClassFlowTakeImage(std::vector<ClassFlow *> *lfc) : ClassFlowImage(lfc, TAG)
  354. {
  355. imagesLocation = "/log/source";
  356. imagesRetention = 5;
  357. SetInitialParameter();
  358. }
  359. string ClassFlowTakeImage::getHTMLSingleStep(string host)
  360. {
  361. string result;
  362. result = "Raw Image: <br>\n<img src=\"" + host + "/img_tmp/raw.jpg\">\n";
  363. return result;
  364. }
  365. // wird bei jeder Auswertrunde aufgerufen
  366. bool ClassFlowTakeImage::doFlow(string zwtime)
  367. {
  368. psram_init_shared_memory_for_take_image_step();
  369. string logPath = CreateLogFolder(zwtime);
  370. int flash_duration = (int)(CCstatus.WaitBeforePicture * 1000);
  371. #ifdef DEBUG_DETAIL_ON
  372. LogFile.WriteHeapInfo("ClassFlowTakeImage::doFlow - Before takePictureWithFlash");
  373. #endif
  374. #ifdef WIFITURNOFF
  375. esp_wifi_stop(); // to save power usage and
  376. #endif
  377. // wenn die Kameraeinstellungen durch Erstellen eines neuen Referenzbildes verändert wurden, müssen sie neu gesetzt werden
  378. if (CFstatus.changedCameraSettings)
  379. {
  380. Camera.setSensorDatenFromCCstatus(); // CCstatus >>> Kamera
  381. Camera.SetQualityZoomSize(CCstatus.ImageQuality, CCstatus.ImageFrameSize, CCstatus.ImageZoomEnabled, CCstatus.ImageZoomOffsetX, CCstatus.ImageZoomOffsetY, CCstatus.ImageZoomSize);
  382. CFstatus.changedCameraSettings = false;
  383. }
  384. takePictureWithFlash(flash_duration);
  385. #ifdef WIFITURNOFF
  386. esp_wifi_start();
  387. #endif
  388. #ifdef DEBUG_DETAIL_ON
  389. LogFile.WriteHeapInfo("ClassFlowTakeImage::doFlow - After takePictureWithFlash");
  390. #endif
  391. LogImage(logPath, "raw", NULL, NULL, zwtime, rawImage);
  392. RemoveOldLogs();
  393. #ifdef DEBUG_DETAIL_ON
  394. LogFile.WriteHeapInfo("ClassFlowTakeImage::doFlow - After RemoveOldLogs");
  395. #endif
  396. psram_deinit_shared_memory_for_take_image_step();
  397. return true;
  398. }
  399. esp_err_t ClassFlowTakeImage::SendRawJPG(httpd_req_t *req)
  400. {
  401. int flash_duration = (int)(CCstatus.WaitBeforePicture * 1000);
  402. time(&TimeImageTaken);
  403. localtime(&TimeImageTaken);
  404. return Camera.CaptureToHTTP(req, flash_duration);
  405. }
  406. ImageData *ClassFlowTakeImage::SendRawImage(void)
  407. {
  408. CImageBasis *zw = new CImageBasis("SendRawImage", rawImage);
  409. ImageData *id;
  410. int flash_duration = (int)(CCstatus.WaitBeforePicture * 1000);
  411. Camera.CaptureToBasisImage(zw, flash_duration);
  412. time(&TimeImageTaken);
  413. localtime(&TimeImageTaken);
  414. id = zw->writeToMemoryAsJPG();
  415. delete zw;
  416. return id;
  417. }
  418. time_t ClassFlowTakeImage::getTimeImageTaken(void)
  419. {
  420. return TimeImageTaken;
  421. }
  422. ClassFlowTakeImage::~ClassFlowTakeImage(void)
  423. {
  424. delete rawImage;
  425. }