ClassFlowTakeImage.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  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. this->imagesRetention = std::stod(splitted[1]);
  78. }
  79. else if ((toUpper(splitted[0]) == "SAVEALLFILES") && (splitted.size() > 1))
  80. {
  81. if (toUpper(splitted[1]) == "TRUE")
  82. {
  83. CCstatus.SaveAllFiles = 1;
  84. }
  85. else
  86. {
  87. CCstatus.SaveAllFiles = 0;
  88. }
  89. }
  90. else if ((toUpper(splitted[0]) == "WAITBEFORETAKINGPICTURE") && (splitted.size() > 1))
  91. {
  92. int _WaitBeforePicture = std::stoi(splitted[1]);
  93. if (_WaitBeforePicture != 0)
  94. {
  95. CCstatus.WaitBeforePicture = _WaitBeforePicture;
  96. }
  97. else
  98. {
  99. CCstatus.WaitBeforePicture = 2;
  100. }
  101. }
  102. else if ((toUpper(splitted[0]) == "CAMGAINCEILING") && (splitted.size() > 1))
  103. {
  104. std::string _ImageGainceiling = toUpper(splitted[1]);
  105. if (_ImageGainceiling == "X4")
  106. {
  107. CCstatus.ImageGainceiling = GAINCEILING_4X;
  108. }
  109. else if (_ImageGainceiling == "X8")
  110. {
  111. CCstatus.ImageGainceiling = GAINCEILING_8X;
  112. }
  113. else if (_ImageGainceiling == "X16")
  114. {
  115. CCstatus.ImageGainceiling = GAINCEILING_16X;
  116. }
  117. else if (_ImageGainceiling == "X32")
  118. {
  119. CCstatus.ImageGainceiling = GAINCEILING_32X;
  120. }
  121. else if (_ImageGainceiling == "X64")
  122. {
  123. CCstatus.ImageGainceiling = GAINCEILING_64X;
  124. }
  125. else if (_ImageGainceiling == "X128")
  126. {
  127. CCstatus.ImageGainceiling = GAINCEILING_128X;
  128. }
  129. else
  130. {
  131. CCstatus.ImageGainceiling = GAINCEILING_2X;
  132. }
  133. }
  134. else if ((toUpper(splitted[0]) == "CAMQUALITY") && (splitted.size() > 1))
  135. {
  136. int _ImageQuality = std::stoi(splitted[1]);
  137. if ((_ImageQuality >= 0) && (_ImageQuality <= 63))
  138. {
  139. CCstatus.ImageQuality = _ImageQuality;
  140. }
  141. }
  142. else if ((toUpper(splitted[0]) == "CAMBRIGHTNESS") && (splitted.size() > 1))
  143. {
  144. int _ImageBrightness = std::stoi(splitted[1]);
  145. if ((_ImageBrightness >= -2) && (_ImageBrightness <= 2))
  146. {
  147. CCstatus.ImageBrightness = _ImageBrightness;
  148. }
  149. }
  150. else if ((toUpper(splitted[0]) == "CAMCONTRAST") && (splitted.size() > 1))
  151. {
  152. int _ImageContrast = std::stoi(splitted[1]);
  153. if ((_ImageContrast >= -2) && (_ImageContrast <= 2))
  154. {
  155. CCstatus.ImageContrast = _ImageContrast;
  156. }
  157. }
  158. else if ((toUpper(splitted[0]) == "CAMSATURATION") && (splitted.size() > 1))
  159. {
  160. int _ImageSaturation = std::stoi(splitted[1]);
  161. if ((_ImageSaturation >= -2) && (_ImageSaturation <= 2))
  162. {
  163. CCstatus.ImageSaturation = _ImageSaturation;
  164. }
  165. }
  166. else if ((toUpper(splitted[0]) == "CAMSHARPNESS") && (splitted.size() > 1))
  167. {
  168. int _ImageSharpness = std::stoi(splitted[1]);
  169. if ((_ImageSharpness >= -2) && (_ImageSharpness <= 2))
  170. {
  171. CCstatus.ImageSharpness = _ImageSharpness;
  172. }
  173. }
  174. else if ((toUpper(splitted[0]) == "CAMAUTOSHARPNESS") && (splitted.size() > 1))
  175. {
  176. if (toUpper(splitted[1]) == "TRUE")
  177. {
  178. CCstatus.ImageAutoSharpness = 1;
  179. }
  180. else
  181. {
  182. CCstatus.ImageAutoSharpness = 0;
  183. }
  184. }
  185. else if ((toUpper(splitted[0]) == "CAMSPECIALEFFECT") && (splitted.size() > 1))
  186. {
  187. std::string _ImageSpecialEffect = toUpper(splitted[1]);
  188. if (_ImageSpecialEffect == "NEGATIVE")
  189. {
  190. CCstatus.ImageSpecialEffect = 1;
  191. }
  192. else if (_ImageSpecialEffect == "GRAYSCALE")
  193. {
  194. CCstatus.ImageSpecialEffect = 2;
  195. }
  196. else if (_ImageSpecialEffect == "RED")
  197. {
  198. CCstatus.ImageSpecialEffect = 3;
  199. }
  200. else if (_ImageSpecialEffect == "GREEN")
  201. {
  202. CCstatus.ImageSpecialEffect = 4;
  203. }
  204. else if (_ImageSpecialEffect == "BLUE")
  205. {
  206. CCstatus.ImageSpecialEffect = 5;
  207. }
  208. else if (_ImageSpecialEffect == "RETRO")
  209. {
  210. CCstatus.ImageSpecialEffect = 6;
  211. }
  212. else
  213. {
  214. CCstatus.ImageSpecialEffect = 0;
  215. }
  216. }
  217. else if ((toUpper(splitted[0]) == "CAMWBMODE") && (splitted.size() > 1))
  218. {
  219. std::string _ImageWbMode = toUpper(splitted[1]);
  220. if (_ImageWbMode == "SUNNY")
  221. {
  222. CCstatus.ImageWbMode = 1;
  223. }
  224. else if (_ImageWbMode == "CLOUDY")
  225. {
  226. CCstatus.ImageWbMode = 2;
  227. }
  228. else if (_ImageWbMode == "OFFICE")
  229. {
  230. CCstatus.ImageWbMode = 3;
  231. }
  232. else if (_ImageWbMode == "HOME")
  233. {
  234. CCstatus.ImageWbMode = 4;
  235. }
  236. else
  237. {
  238. CCstatus.ImageWbMode = 0;
  239. }
  240. }
  241. else if ((toUpper(splitted[0]) == "CAMAWB") && (splitted.size() > 1))
  242. {
  243. if (toUpper(splitted[1]) == "TRUE")
  244. {
  245. CCstatus.ImageAwb = 1;
  246. }
  247. else
  248. {
  249. CCstatus.ImageAwb = 0;
  250. }
  251. }
  252. else if ((toUpper(splitted[0]) == "CAMAWBGAIN") && (splitted.size() > 1))
  253. {
  254. if (toUpper(splitted[1]) == "TRUE")
  255. {
  256. CCstatus.ImageAwbGain = 1;
  257. }
  258. else
  259. {
  260. CCstatus.ImageAwbGain = 0;
  261. }
  262. }
  263. else if ((toUpper(splitted[0]) == "CAMAEC") && (splitted.size() > 1))
  264. {
  265. if (toUpper(splitted[1]) == "TRUE")
  266. {
  267. CCstatus.ImageAec = 1;
  268. }
  269. else
  270. {
  271. CCstatus.ImageAec = 0;
  272. }
  273. }
  274. else if ((toUpper(splitted[0]) == "CAMAEC2") && (splitted.size() > 1))
  275. {
  276. if (toUpper(splitted[1]) == "TRUE")
  277. {
  278. CCstatus.ImageAec2 = 1;
  279. }
  280. else
  281. {
  282. CCstatus.ImageAec2 = 0;
  283. }
  284. }
  285. else if ((toUpper(splitted[0]) == "CAMAELEVEL") && (splitted.size() > 1))
  286. {
  287. int _ImageAeLevel = std::stoi(splitted[1]);
  288. if ((_ImageAeLevel >= -2) && (_ImageAeLevel <= 2))
  289. {
  290. CCstatus.ImageAeLevel = _ImageAeLevel;
  291. }
  292. }
  293. else if ((toUpper(splitted[0]) == "CAMAECVALUE") && (splitted.size() > 1))
  294. {
  295. int _ImageAecValue = std::stoi(splitted[1]);
  296. if ((_ImageAecValue >= 0) && (_ImageAecValue <= 1200))
  297. {
  298. CCstatus.ImageAecValue = _ImageAecValue;
  299. }
  300. }
  301. else if ((toUpper(splitted[0]) == "CAMAGC") && (splitted.size() > 1))
  302. {
  303. if (toUpper(splitted[1]) == "TRUE")
  304. {
  305. CCstatus.ImageAgc = 1;
  306. }
  307. else
  308. {
  309. CCstatus.ImageAgc = 0;
  310. }
  311. }
  312. else if ((toUpper(splitted[0]) == "CAMAGCGAIN") && (splitted.size() > 1))
  313. {
  314. int _ImageAgcGain = std::stoi(splitted[1]);
  315. if ((_ImageAgcGain >= 0) && (_ImageAgcGain <= 30))
  316. {
  317. CCstatus.ImageAgcGain = _ImageAgcGain;
  318. }
  319. }
  320. else if ((toUpper(splitted[0]) == "CAMBPC") && (splitted.size() > 1))
  321. {
  322. if (toUpper(splitted[1]) == "TRUE")
  323. {
  324. CCstatus.ImageBpc = 1;
  325. }
  326. else
  327. {
  328. CCstatus.ImageBpc = 0;
  329. }
  330. }
  331. else if ((toUpper(splitted[0]) == "CAMWPC") && (splitted.size() > 1))
  332. {
  333. if (toUpper(splitted[1]) == "TRUE")
  334. {
  335. CCstatus.ImageWpc = 1;
  336. }
  337. else
  338. {
  339. CCstatus.ImageWpc = 0;
  340. }
  341. }
  342. else if ((toUpper(splitted[0]) == "CAMRAWGMA") && (splitted.size() > 1))
  343. {
  344. if (toUpper(splitted[1]) == "TRUE")
  345. {
  346. CCstatus.ImageRawGma = 1;
  347. }
  348. else
  349. {
  350. CCstatus.ImageRawGma = 0;
  351. }
  352. }
  353. else if ((toUpper(splitted[0]) == "CAMLENC") && (splitted.size() > 1))
  354. {
  355. if (toUpper(splitted[1]) == "TRUE")
  356. {
  357. CCstatus.ImageLenc = 1;
  358. }
  359. else
  360. {
  361. CCstatus.ImageLenc = 0;
  362. }
  363. }
  364. else if ((toUpper(splitted[0]) == "CAMHMIRROR") && (splitted.size() > 1))
  365. {
  366. if (toUpper(splitted[1]) == "TRUE")
  367. {
  368. CCstatus.ImageHmirror = 1;
  369. }
  370. else
  371. {
  372. CCstatus.ImageHmirror = 0;
  373. }
  374. }
  375. else if ((toUpper(splitted[0]) == "CAMVFLIP") && (splitted.size() > 1))
  376. {
  377. if (toUpper(splitted[1]) == "TRUE")
  378. {
  379. CCstatus.ImageVflip = 1;
  380. }
  381. else
  382. {
  383. CCstatus.ImageVflip = 0;
  384. }
  385. }
  386. else if ((toUpper(splitted[0]) == "CAMDCW") && (splitted.size() > 1))
  387. {
  388. if (toUpper(splitted[1]) == "TRUE")
  389. {
  390. CCstatus.ImageDcw = 1;
  391. }
  392. else
  393. {
  394. CCstatus.ImageDcw = 0;
  395. }
  396. }
  397. else if ((toUpper(splitted[0]) == "CAMZOOM") && (splitted.size() > 1))
  398. {
  399. if (toUpper(splitted[1]) == "TRUE")
  400. {
  401. CCstatus.ImageZoomEnabled = 1;
  402. }
  403. else
  404. {
  405. CCstatus.ImageZoomEnabled = 0;
  406. }
  407. }
  408. else if ((toUpper(splitted[0]) == "CAMZOOMOFFSETX") && (splitted.size() > 1))
  409. {
  410. CCstatus.ImageZoomOffsetX = std::stoi(splitted[1]);
  411. }
  412. else if ((toUpper(splitted[0]) == "CAMZOOMOFFSETY") && (splitted.size() > 1))
  413. {
  414. CCstatus.ImageZoomOffsetY = std::stoi(splitted[1]);
  415. }
  416. else if ((toUpper(splitted[0]) == "CAMZOOMSIZE") && (splitted.size() > 1))
  417. {
  418. int _ImageZoomSize = std::stoi(splitted[1]);
  419. if (_ImageZoomSize >= 0)
  420. {
  421. CCstatus.ImageZoomSize = _ImageZoomSize;
  422. }
  423. }
  424. else if ((toUpper(splitted[0]) == "LEDINTENSITY") && (splitted.size() > 1))
  425. {
  426. float ledintensity = std::stof(splitted[1]);
  427. Camera.SetLEDIntensity(ledintensity);
  428. }
  429. else if ((toUpper(splitted[0]) == "DEMO") && (splitted.size() > 1))
  430. {
  431. if (toUpper(splitted[1]) == "TRUE")
  432. {
  433. CCstatus.DemoMode = true;
  434. Camera.useDemoMode();
  435. }
  436. else
  437. {
  438. CCstatus.DemoMode = false;
  439. }
  440. }
  441. }
  442. Camera.setSensorDatenFromCCstatus(); // CCstatus >>> Kamera
  443. Camera.SetQualityZoomSize(CCstatus.ImageQuality, CCstatus.ImageFrameSize, CCstatus.ImageZoomEnabled, CCstatus.ImageZoomOffsetX, CCstatus.ImageZoomOffsetY, CCstatus.ImageZoomSize);
  444. rawImage = new CImageBasis("rawImage");
  445. rawImage->CreateEmptyImage(CCstatus.ImageWidth, CCstatus.ImageHeight, 3);
  446. return true;
  447. }
  448. ClassFlowTakeImage::ClassFlowTakeImage(std::vector<ClassFlow *> *lfc) : ClassFlowImage(lfc, TAG)
  449. {
  450. imagesLocation = "/log/source";
  451. imagesRetention = 5;
  452. SetInitialParameter();
  453. }
  454. string ClassFlowTakeImage::getHTMLSingleStep(string host)
  455. {
  456. string result;
  457. result = "Raw Image: <br>\n<img src=\"" + host + "/img_tmp/raw.jpg\">\n";
  458. return result;
  459. }
  460. // wird bei jeder Auswertrunde aufgerufen
  461. bool ClassFlowTakeImage::doFlow(string zwtime)
  462. {
  463. psram_init_shared_memory_for_take_image_step();
  464. string logPath = CreateLogFolder(zwtime);
  465. int flash_duration = (int)(CCstatus.WaitBeforePicture * 1000);
  466. #ifdef DEBUG_DETAIL_ON
  467. LogFile.WriteHeapInfo("ClassFlowTakeImage::doFlow - Before takePictureWithFlash");
  468. #endif
  469. #ifdef WIFITURNOFF
  470. esp_wifi_stop(); // to save power usage and
  471. #endif
  472. // wenn die Kameraeinstellungen durch Erstellen eines neuen Referenzbildes verändert wurden, müssen sie neu gesetzt werden
  473. if (CFstatus.changedCameraSettings)
  474. {
  475. Camera.setSensorDatenFromCCstatus(); // CCstatus >>> Kamera
  476. Camera.SetQualityZoomSize(CCstatus.ImageQuality, CCstatus.ImageFrameSize, CCstatus.ImageZoomEnabled, CCstatus.ImageZoomOffsetX, CCstatus.ImageZoomOffsetY, CCstatus.ImageZoomSize);
  477. CFstatus.changedCameraSettings = false;
  478. }
  479. takePictureWithFlash(flash_duration);
  480. #ifdef WIFITURNOFF
  481. esp_wifi_start();
  482. #endif
  483. #ifdef DEBUG_DETAIL_ON
  484. LogFile.WriteHeapInfo("ClassFlowTakeImage::doFlow - After takePictureWithFlash");
  485. #endif
  486. LogImage(logPath, "raw", NULL, NULL, zwtime, rawImage);
  487. RemoveOldLogs();
  488. #ifdef DEBUG_DETAIL_ON
  489. LogFile.WriteHeapInfo("ClassFlowTakeImage::doFlow - After RemoveOldLogs");
  490. #endif
  491. psram_deinit_shared_memory_for_take_image_step();
  492. return true;
  493. }
  494. esp_err_t ClassFlowTakeImage::SendRawJPG(httpd_req_t *req)
  495. {
  496. int flash_duration = (int)(CCstatus.WaitBeforePicture * 1000);
  497. time(&TimeImageTaken);
  498. localtime(&TimeImageTaken);
  499. return Camera.CaptureToHTTP(req, flash_duration);
  500. }
  501. ImageData *ClassFlowTakeImage::SendRawImage(void)
  502. {
  503. CImageBasis *zw = new CImageBasis("SendRawImage", rawImage);
  504. ImageData *id;
  505. int flash_duration = (int)(CCstatus.WaitBeforePicture * 1000);
  506. Camera.CaptureToBasisImage(zw, flash_duration);
  507. time(&TimeImageTaken);
  508. localtime(&TimeImageTaken);
  509. id = zw->writeToMemoryAsJPG();
  510. delete zw;
  511. return id;
  512. }
  513. time_t ClassFlowTakeImage::getTimeImageTaken(void)
  514. {
  515. return TimeImageTaken;
  516. }
  517. ClassFlowTakeImage::~ClassFlowTakeImage(void)
  518. {
  519. delete rawImage;
  520. }