ClassFlowTakeImage.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <regex>
  5. #include "ClassFlowTakeImage.h"
  6. #include <new>
  7. #include "Helper.h"
  8. #include "ClassLogFile.h"
  9. #include "CImageBasis.h"
  10. #include "ClassControllCamera.h"
  11. #include "MainFlowControl.h"
  12. #include "esp_wifi.h"
  13. #include "esp_log.h"
  14. #include "../../include/defines.h"
  15. #include "psram.h"
  16. #include <time.h>
  17. // #define DEBUG_DETAIL_ON
  18. // #define WIFITURNOFF
  19. static const char *TAG = "TAKEIMAGE";
  20. esp_err_t ClassFlowTakeImage::camera_capture(void)
  21. {
  22. string nm = namerawimage;
  23. Camera.CaptureToFile(nm);
  24. time(&TimeImageTaken);
  25. localtime(&TimeImageTaken);
  26. return ESP_OK;
  27. }
  28. bool ClassFlowTakeImage::takePictureWithFlash(int flash_duration)
  29. {
  30. if (rawImage == NULL) {
  31. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "takePictureWithFlash: raw image object is unavailable");
  32. return false;
  33. }
  34. if (rawImage->width != CCstatus.ImageWidth || rawImage->height != CCstatus.ImageHeight ||
  35. rawImage->channels != 3) {
  36. rawImage->CreateEmptyImage(CCstatus.ImageWidth, CCstatus.ImageHeight, 3);
  37. }
  38. if (!rawImage->ImageOkay()) {
  39. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "takePictureWithFlash: raw image PSRAM buffer is unavailable");
  40. return false;
  41. }
  42. ESP_LOGD(TAG, "flash_duration: %d", flash_duration);
  43. if (Camera.CaptureToBasisImage(rawImage, flash_duration) != ESP_OK) return false;
  44. time(&TimeImageTaken);
  45. localtime(&TimeImageTaken);
  46. if (CCstatus.SaveAllFiles) rawImage->SaveToFile(namerawimage);
  47. return true;
  48. }
  49. void ClassFlowTakeImage::SetInitialParameter(void)
  50. {
  51. TimeImageTaken = 0;
  52. rawImage = NULL;
  53. disabled = false;
  54. namerawimage = "/sdcard/img_tmp/raw.jpg";
  55. }
  56. // auslesen der Kameraeinstellungen aus der config.ini
  57. // wird beim Start aufgerufen
  58. bool ClassFlowTakeImage::ReadParameter(FILE *pfile, string &aktparamgraph)
  59. {
  60. Camera.getSensorDatenToCCstatus(); // Kamera >>> CCstatus
  61. std::vector<string> splitted;
  62. aktparamgraph = trim(aktparamgraph);
  63. if (aktparamgraph.size() == 0)
  64. {
  65. if (!this->GetNextParagraph(pfile, aktparamgraph))
  66. {
  67. return false;
  68. }
  69. }
  70. if (aktparamgraph.compare("[TakeImage]") != 0)
  71. {
  72. // Paragraph does not fit TakeImage
  73. return false;
  74. }
  75. while (this->getNextLine(pfile, &aktparamgraph) && !this->isNewParagraph(aktparamgraph))
  76. {
  77. splitted = ZerlegeZeile(aktparamgraph);
  78. if ((toUpper(splitted[0]) == "RAWIMAGESLOCATION") && (splitted.size() > 1))
  79. {
  80. imagesLocation = "/sdcard" + splitted[1];
  81. isLogImage = true;
  82. }
  83. else if ((toUpper(splitted[0]) == "RAWIMAGESRETENTION") && (splitted.size() > 1))
  84. {
  85. if (isStringNumeric(splitted[1]))
  86. {
  87. this->imagesRetention = std::stod(splitted[1]);
  88. }
  89. }
  90. else if ((toUpper(splitted[0]) == "SAVEALLFILES") && (splitted.size() > 1))
  91. {
  92. CCstatus.SaveAllFiles = alphanumericToBoolean(splitted[1]);
  93. }
  94. else if ((toUpper(splitted[0]) == "WAITBEFORETAKINGPICTURE") && (splitted.size() > 1))
  95. {
  96. if (isStringNumeric(splitted[1]))
  97. {
  98. int _WaitBeforePicture = std::stoi(splitted[1]);
  99. if (_WaitBeforePicture != 0)
  100. {
  101. CCstatus.WaitBeforePicture = _WaitBeforePicture;
  102. }
  103. else
  104. {
  105. CCstatus.WaitBeforePicture = 2;
  106. }
  107. }
  108. }
  109. else if ((toUpper(splitted[0]) == "CAMGAINCEILING") && (splitted.size() > 1))
  110. {
  111. std::string _ImageGainceiling = toUpper(splitted[1]);
  112. if (isStringNumeric(_ImageGainceiling))
  113. {
  114. int _ImageGainceiling_ = std::stoi(_ImageGainceiling);
  115. switch (_ImageGainceiling_)
  116. {
  117. case 1:
  118. CCstatus.ImageGainceiling = GAINCEILING_4X;
  119. break;
  120. case 2:
  121. CCstatus.ImageGainceiling = GAINCEILING_8X;
  122. break;
  123. case 3:
  124. CCstatus.ImageGainceiling = GAINCEILING_16X;
  125. break;
  126. case 4:
  127. CCstatus.ImageGainceiling = GAINCEILING_32X;
  128. break;
  129. case 5:
  130. CCstatus.ImageGainceiling = GAINCEILING_64X;
  131. break;
  132. case 6:
  133. CCstatus.ImageGainceiling = GAINCEILING_128X;
  134. break;
  135. default:
  136. CCstatus.ImageGainceiling = GAINCEILING_2X;
  137. }
  138. }
  139. else
  140. {
  141. if (_ImageGainceiling == "X4")
  142. {
  143. CCstatus.ImageGainceiling = GAINCEILING_4X;
  144. }
  145. else if (_ImageGainceiling == "X8")
  146. {
  147. CCstatus.ImageGainceiling = GAINCEILING_8X;
  148. }
  149. else if (_ImageGainceiling == "X16")
  150. {
  151. CCstatus.ImageGainceiling = GAINCEILING_16X;
  152. }
  153. else if (_ImageGainceiling == "X32")
  154. {
  155. CCstatus.ImageGainceiling = GAINCEILING_32X;
  156. }
  157. else if (_ImageGainceiling == "X64")
  158. {
  159. CCstatus.ImageGainceiling = GAINCEILING_64X;
  160. }
  161. else if (_ImageGainceiling == "X128")
  162. {
  163. CCstatus.ImageGainceiling = GAINCEILING_128X;
  164. }
  165. else
  166. {
  167. CCstatus.ImageGainceiling = GAINCEILING_2X;
  168. }
  169. }
  170. }
  171. else if ((toUpper(splitted[0]) == "CAMQUALITY") && (splitted.size() > 1))
  172. {
  173. if (isStringNumeric(splitted[1]))
  174. {
  175. int _ImageQuality = std::stoi(splitted[1]);
  176. CCstatus.ImageQuality = clipInt(_ImageQuality, 63, 6);
  177. }
  178. }
  179. else if ((toUpper(splitted[0]) == "CAMBRIGHTNESS") && (splitted.size() > 1))
  180. {
  181. if (isStringNumeric(splitted[1]))
  182. {
  183. int _ImageBrightness = std::stoi(splitted[1]);
  184. CCstatus.ImageBrightness = clipInt(_ImageBrightness, 2, -2);
  185. }
  186. }
  187. else if ((toUpper(splitted[0]) == "CAMCONTRAST") && (splitted.size() > 1))
  188. {
  189. if (isStringNumeric(splitted[1]))
  190. {
  191. int _ImageContrast = std::stoi(splitted[1]);
  192. CCstatus.ImageContrast = clipInt(_ImageContrast, 2, -2);
  193. }
  194. }
  195. else if ((toUpper(splitted[0]) == "CAMSATURATION") && (splitted.size() > 1))
  196. {
  197. if (isStringNumeric(splitted[1]))
  198. {
  199. int _ImageSaturation = std::stoi(splitted[1]);
  200. CCstatus.ImageSaturation = clipInt(_ImageSaturation, 2, -2);
  201. }
  202. }
  203. else if ((toUpper(splitted[0]) == "CAMSHARPNESS") && (splitted.size() > 1))
  204. {
  205. if (isStringNumeric(splitted[1]))
  206. {
  207. int _ImageSharpness = std::stoi(splitted[1]);
  208. if (CCstatus.CamSensor_id == OV2640_PID)
  209. {
  210. CCstatus.ImageSharpness = clipInt(_ImageSharpness, 2, -2);
  211. }
  212. else
  213. {
  214. CCstatus.ImageSharpness = clipInt(_ImageSharpness, 3, -3);
  215. }
  216. }
  217. }
  218. else if ((toUpper(splitted[0]) == "CAMAUTOSHARPNESS") && (splitted.size() > 1))
  219. {
  220. CCstatus.ImageAutoSharpness = alphanumericToBoolean(splitted[1]);
  221. }
  222. else if ((toUpper(splitted[0]) == "CAMSPECIALEFFECT") && (splitted.size() > 1))
  223. {
  224. std::string _ImageSpecialEffect = toUpper(splitted[1]);
  225. if (isStringNumeric(_ImageSpecialEffect))
  226. {
  227. int _ImageSpecialEffect_ = std::stoi(_ImageSpecialEffect);
  228. CCstatus.ImageSpecialEffect = clipInt(_ImageSpecialEffect_, 6, 0);
  229. }
  230. else
  231. {
  232. if (_ImageSpecialEffect == "NEGATIVE")
  233. {
  234. CCstatus.ImageSpecialEffect = 1;
  235. }
  236. else if (_ImageSpecialEffect == "GRAYSCALE")
  237. {
  238. CCstatus.ImageSpecialEffect = 2;
  239. }
  240. else if (_ImageSpecialEffect == "RED")
  241. {
  242. CCstatus.ImageSpecialEffect = 3;
  243. }
  244. else if (_ImageSpecialEffect == "GREEN")
  245. {
  246. CCstatus.ImageSpecialEffect = 4;
  247. }
  248. else if (_ImageSpecialEffect == "BLUE")
  249. {
  250. CCstatus.ImageSpecialEffect = 5;
  251. }
  252. else if (_ImageSpecialEffect == "RETRO")
  253. {
  254. CCstatus.ImageSpecialEffect = 6;
  255. }
  256. else
  257. {
  258. CCstatus.ImageSpecialEffect = 0;
  259. }
  260. }
  261. }
  262. else if ((toUpper(splitted[0]) == "CAMWBMODE") && (splitted.size() > 1))
  263. {
  264. std::string _ImageWbMode = toUpper(splitted[1]);
  265. if (isStringNumeric(_ImageWbMode))
  266. {
  267. int _ImageWbMode_ = std::stoi(_ImageWbMode);
  268. CCstatus.ImageWbMode = clipInt(_ImageWbMode_, 4, 0);
  269. }
  270. else
  271. {
  272. if (_ImageWbMode == "SUNNY")
  273. {
  274. CCstatus.ImageWbMode = 1;
  275. }
  276. else if (_ImageWbMode == "CLOUDY")
  277. {
  278. CCstatus.ImageWbMode = 2;
  279. }
  280. else if (_ImageWbMode == "OFFICE")
  281. {
  282. CCstatus.ImageWbMode = 3;
  283. }
  284. else if (_ImageWbMode == "HOME")
  285. {
  286. CCstatus.ImageWbMode = 4;
  287. }
  288. else
  289. {
  290. CCstatus.ImageWbMode = 0;
  291. }
  292. }
  293. }
  294. else if ((toUpper(splitted[0]) == "CAMAWB") && (splitted.size() > 1))
  295. {
  296. CCstatus.ImageAwb = alphanumericToBoolean(splitted[1]);
  297. }
  298. else if ((toUpper(splitted[0]) == "CAMAWBGAIN") && (splitted.size() > 1))
  299. {
  300. CCstatus.ImageAwbGain = alphanumericToBoolean(splitted[1]);
  301. }
  302. else if ((toUpper(splitted[0]) == "CAMAEC") && (splitted.size() > 1))
  303. {
  304. CCstatus.ImageAec = alphanumericToBoolean(splitted[1]);
  305. }
  306. else if ((toUpper(splitted[0]) == "CAMAEC2") && (splitted.size() > 1))
  307. {
  308. CCstatus.ImageAec2 = alphanumericToBoolean(splitted[1]);
  309. }
  310. else if ((toUpper(splitted[0]) == "CAMAELEVEL") && (splitted.size() > 1))
  311. {
  312. if (isStringNumeric(splitted[1]))
  313. {
  314. int _ImageAeLevel = std::stoi(splitted[1]);
  315. if (CCstatus.CamSensor_id == OV2640_PID)
  316. {
  317. CCstatus.ImageAeLevel = clipInt(_ImageAeLevel, 2, -2);
  318. }
  319. else
  320. {
  321. CCstatus.ImageAeLevel = clipInt(_ImageAeLevel, 5, -5);
  322. }
  323. }
  324. }
  325. else if ((toUpper(splitted[0]) == "CAMAECVALUE") && (splitted.size() > 1))
  326. {
  327. if (isStringNumeric(splitted[1]))
  328. {
  329. int _ImageAecValue = std::stoi(splitted[1]);
  330. CCstatus.ImageAecValue = clipInt(_ImageAecValue, 1200, 0);
  331. }
  332. }
  333. else if ((toUpper(splitted[0]) == "CAMAGC") && (splitted.size() > 1))
  334. {
  335. CCstatus.ImageAgc = alphanumericToBoolean(splitted[1]);
  336. }
  337. else if ((toUpper(splitted[0]) == "CAMAGCGAIN") && (splitted.size() > 1))
  338. {
  339. if (isStringNumeric(splitted[1]))
  340. {
  341. int _ImageAgcGain = std::stoi(splitted[1]);
  342. CCstatus.ImageAgcGain = clipInt(_ImageAgcGain, 30, 0);
  343. }
  344. }
  345. else if ((toUpper(splitted[0]) == "CAMBPC") && (splitted.size() > 1))
  346. {
  347. CCstatus.ImageBpc = alphanumericToBoolean(splitted[1]);
  348. }
  349. else if ((toUpper(splitted[0]) == "CAMWPC") && (splitted.size() > 1))
  350. {
  351. CCstatus.ImageWpc = alphanumericToBoolean(splitted[1]);
  352. }
  353. else if ((toUpper(splitted[0]) == "CAMRAWGMA") && (splitted.size() > 1))
  354. {
  355. CCstatus.ImageRawGma = alphanumericToBoolean(splitted[1]);
  356. }
  357. else if ((toUpper(splitted[0]) == "CAMLENC") && (splitted.size() > 1))
  358. {
  359. CCstatus.ImageLenc = alphanumericToBoolean(splitted[1]);
  360. }
  361. else if ((toUpper(splitted[0]) == "CAMHMIRROR") && (splitted.size() > 1))
  362. {
  363. CCstatus.ImageHmirror = alphanumericToBoolean(splitted[1]);
  364. }
  365. else if ((toUpper(splitted[0]) == "CAMVFLIP") && (splitted.size() > 1))
  366. {
  367. CCstatus.ImageVflip = alphanumericToBoolean(splitted[1]);
  368. }
  369. else if ((toUpper(splitted[0]) == "CAMDCW") && (splitted.size() > 1))
  370. {
  371. CCstatus.ImageDcw = alphanumericToBoolean(splitted[1]);
  372. }
  373. else if ((toUpper(splitted[0]) == "CAMDENOISE") && (splitted.size() > 1))
  374. {
  375. if (isStringNumeric(splitted[1]))
  376. {
  377. int _ImageDenoiseLevel = std::stoi(splitted[1]);
  378. if (CCstatus.CamSensor_id == OV2640_PID)
  379. {
  380. CCstatus.ImageDenoiseLevel = 0;
  381. }
  382. else
  383. {
  384. CCstatus.ImageDenoiseLevel = clipInt(_ImageDenoiseLevel, 8, 0);
  385. }
  386. }
  387. }
  388. else if ((toUpper(splitted[0]) == "CAMZOOM") && (splitted.size() > 1))
  389. {
  390. CCstatus.ImageZoomEnabled = alphanumericToBoolean(splitted[1]);
  391. }
  392. else if ((toUpper(splitted[0]) == "CAMZOOMOFFSETX") && (splitted.size() > 1))
  393. {
  394. if (isStringNumeric(splitted[1]))
  395. {
  396. int _ImageZoomOffsetX = std::stoi(splitted[1]);
  397. if (CCstatus.CamSensor_id == OV2640_PID)
  398. {
  399. CCstatus.ImageZoomOffsetX = clipInt(_ImageZoomOffsetX, 480, -480);
  400. }
  401. else if (CCstatus.CamSensor_id == OV3660_PID)
  402. {
  403. CCstatus.ImageZoomOffsetX = clipInt(_ImageZoomOffsetX, 704, -704);
  404. }
  405. else if (CCstatus.CamSensor_id == OV5640_PID)
  406. {
  407. CCstatus.ImageZoomOffsetX = clipInt(_ImageZoomOffsetX, 960, -960);
  408. }
  409. }
  410. }
  411. else if ((toUpper(splitted[0]) == "CAMZOOMOFFSETY") && (splitted.size() > 1))
  412. {
  413. if (isStringNumeric(splitted[1]))
  414. {
  415. int _ImageZoomOffsetY = std::stoi(splitted[1]);
  416. if (CCstatus.CamSensor_id == OV2640_PID)
  417. {
  418. CCstatus.ImageZoomOffsetY = clipInt(_ImageZoomOffsetY, 360, -360);
  419. }
  420. else if (CCstatus.CamSensor_id == OV3660_PID)
  421. {
  422. CCstatus.ImageZoomOffsetY = clipInt(_ImageZoomOffsetY, 528, -528);
  423. }
  424. else if (CCstatus.CamSensor_id == OV5640_PID)
  425. {
  426. CCstatus.ImageZoomOffsetY = clipInt(_ImageZoomOffsetY, 720, -720);
  427. }
  428. }
  429. }
  430. else if ((toUpper(splitted[0]) == "CAMZOOMSIZE") && (splitted.size() > 1))
  431. {
  432. if (isStringNumeric(splitted[1]))
  433. {
  434. int _ImageZoomSize = std::stoi(splitted[1]);
  435. if (CCstatus.CamSensor_id == OV2640_PID)
  436. {
  437. CCstatus.ImageZoomSize = clipInt(_ImageZoomSize, 29, 0);
  438. }
  439. else if (CCstatus.CamSensor_id == OV3660_PID)
  440. {
  441. CCstatus.ImageZoomSize = clipInt(_ImageZoomSize, 43, 0);
  442. }
  443. else if (CCstatus.CamSensor_id == OV5640_PID)
  444. {
  445. CCstatus.ImageZoomSize = clipInt(_ImageZoomSize, 59, 0);
  446. }
  447. }
  448. }
  449. else if ((toUpper(splitted[0]) == "LEDINTENSITY") && (splitted.size() > 1))
  450. {
  451. if (isStringNumeric(splitted[1]))
  452. {
  453. int ledintensity = std::stoi(splitted[1]);
  454. CCstatus.ImageLedIntensity = Camera.SetLEDIntensity(ledintensity);
  455. }
  456. }
  457. else if ((toUpper(splitted[0]) == "DEMO") && (splitted.size() > 1))
  458. {
  459. CCstatus.DemoMode = alphanumericToBoolean(splitted[1]);
  460. if (CCstatus.DemoMode == true)
  461. {
  462. Camera.useDemoMode();
  463. }
  464. }
  465. }
  466. Camera.setSensorDatenFromCCstatus(); // CCstatus >>> Kamera
  467. Camera.SetQualityZoomSize(CCstatus.ImageQuality, CCstatus.ImageFrameSize, CCstatus.ImageZoomEnabled, CCstatus.ImageZoomOffsetX, CCstatus.ImageZoomOffsetY, CCstatus.ImageZoomSize, CCstatus.ImageVflip);
  468. delete rawImage;
  469. rawImage = new (std::nothrow) CImageBasis("rawImage");
  470. if (rawImage == NULL) {
  471. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Unable to allocate raw image object");
  472. return false;
  473. }
  474. rawImage->CreateEmptyImage(CCstatus.ImageWidth, CCstatus.ImageHeight, 3);
  475. if (!rawImage->ImageOkay()) {
  476. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Unable to allocate raw image PSRAM buffer");
  477. delete rawImage;
  478. rawImage = NULL;
  479. return false;
  480. }
  481. return true;
  482. }
  483. ClassFlowTakeImage::ClassFlowTakeImage(std::vector<ClassFlow *> *lfc) : ClassFlowImage(lfc, TAG)
  484. {
  485. imagesLocation = "/log/source";
  486. imagesRetention = 5;
  487. SetInitialParameter();
  488. }
  489. string ClassFlowTakeImage::getHTMLSingleStep(string host)
  490. {
  491. string result;
  492. result = "Raw Image: <br>\n<img src=\"" + host + "/img_tmp/raw.jpg\">\n";
  493. return result;
  494. }
  495. // wird bei jeder Auswertrunde aufgerufen
  496. bool ClassFlowTakeImage::doFlow(string zwtime)
  497. {
  498. const bool sharedMemoryLeaseAcquired = psram_init_shared_memory_for_take_image_step();
  499. if (!sharedMemoryLeaseAcquired) {
  500. LogFile.WriteToFile(ESP_LOG_WARN, TAG, "Shared PSRAM image lease unavailable; STBI will use independent PSRAM allocations");
  501. }
  502. string logPath = CreateLogFolder(zwtime);
  503. int flash_duration = (int)(CCstatus.WaitBeforePicture * 1000);
  504. #ifdef DEBUG_DETAIL_ON
  505. LogFile.WriteHeapInfo("ClassFlowTakeImage::doFlow - Before takePictureWithFlash");
  506. #endif
  507. #ifdef WIFITURNOFF
  508. esp_wifi_stop(); // to save power usage and
  509. #endif
  510. // wenn die Kameraeinstellungen durch Erstellen eines neuen Referenzbildes verändert wurden, müssen sie neu gesetzt werden
  511. if (CFstatus.changedCameraSettings)
  512. {
  513. Camera.setSensorDatenFromCCstatus(); // CCstatus >>> Kamera
  514. Camera.SetQualityZoomSize(CCstatus.ImageQuality, CCstatus.ImageFrameSize, CCstatus.ImageZoomEnabled, CCstatus.ImageZoomOffsetX, CCstatus.ImageZoomOffsetY, CCstatus.ImageZoomSize, CCstatus.ImageVflip);
  515. Camera.LedIntensity = CCstatus.ImageLedIntensity;
  516. CFstatus.changedCameraSettings = false;
  517. }
  518. const bool captured = takePictureWithFlash(flash_duration);
  519. #ifdef WIFITURNOFF
  520. esp_wifi_start();
  521. #endif
  522. #ifdef DEBUG_DETAIL_ON
  523. LogFile.WriteHeapInfo("ClassFlowTakeImage::doFlow - After takePictureWithFlash");
  524. #endif
  525. if (!captured) {
  526. if (sharedMemoryLeaseAcquired) psram_deinit_shared_memory_for_take_image_step();
  527. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Image capture failed; aborting this flow round");
  528. return false;
  529. }
  530. LogImage(logPath, "raw", NULL, NULL, zwtime, rawImage);
  531. RemoveOldLogs();
  532. #ifdef DEBUG_DETAIL_ON
  533. LogFile.WriteHeapInfo("ClassFlowTakeImage::doFlow - After RemoveOldLogs");
  534. #endif
  535. if (sharedMemoryLeaseAcquired) psram_deinit_shared_memory_for_take_image_step();
  536. return true;
  537. }
  538. esp_err_t ClassFlowTakeImage::SendRawJPG(httpd_req_t *req)
  539. {
  540. int flash_duration = (int)(CCstatus.WaitBeforePicture * 1000);
  541. time(&TimeImageTaken);
  542. localtime(&TimeImageTaken);
  543. return Camera.CaptureToHTTP(req, flash_duration);
  544. }
  545. ImageData *ClassFlowTakeImage::SendRawImage(void)
  546. {
  547. if (rawImage == NULL || !rawImage->ImageOkay()) return NULL;
  548. CImageBasis *zw = new (std::nothrow) CImageBasis("SendRawImage", rawImage);
  549. if (zw == NULL || !zw->ImageOkay()) {
  550. delete zw;
  551. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "SendRawImage: unable to allocate an image copy");
  552. return NULL;
  553. }
  554. const int flash_duration = static_cast<int>(CCstatus.WaitBeforePicture * 1000);
  555. if (Camera.CaptureToBasisImage(zw, flash_duration) != ESP_OK) {
  556. delete zw;
  557. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "SendRawImage: camera capture failed");
  558. return NULL;
  559. }
  560. time(&TimeImageTaken);
  561. localtime(&TimeImageTaken);
  562. ImageData *id = zw->writeToMemoryAsJPG();
  563. delete zw;
  564. // The caller owns the returned ImageData and must release it with delete.
  565. return id;
  566. }
  567. time_t ClassFlowTakeImage::getTimeImageTaken(void)
  568. {
  569. return TimeImageTaken;
  570. }
  571. ClassFlowTakeImage::~ClassFlowTakeImage(void)
  572. {
  573. delete rawImage;
  574. }