ClassFlowTakeImage.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  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. if (CCstatus.CamSensor_id == OV2640_PID)
  171. {
  172. CCstatus.ImageSharpness = clipInt(_ImageSharpness, 2, -2);
  173. }
  174. else
  175. {
  176. CCstatus.ImageSharpness = clipInt(_ImageSharpness, 3, -3);
  177. }
  178. }
  179. }
  180. else if ((toUpper(splitted[0]) == "CAMAUTOSHARPNESS") && (splitted.size() > 1))
  181. {
  182. CCstatus.ImageAutoSharpness = alphanumericToBoolean(splitted[1]);
  183. }
  184. else if ((toUpper(splitted[0]) == "CAMSPECIALEFFECT") && (splitted.size() > 1))
  185. {
  186. std::string _ImageSpecialEffect = toUpper(splitted[1]);
  187. if (_ImageSpecialEffect == "NEGATIVE")
  188. {
  189. CCstatus.ImageSpecialEffect = 1;
  190. }
  191. else if (_ImageSpecialEffect == "GRAYSCALE")
  192. {
  193. CCstatus.ImageSpecialEffect = 2;
  194. }
  195. else if (_ImageSpecialEffect == "RED")
  196. {
  197. CCstatus.ImageSpecialEffect = 3;
  198. }
  199. else if (_ImageSpecialEffect == "GREEN")
  200. {
  201. CCstatus.ImageSpecialEffect = 4;
  202. }
  203. else if (_ImageSpecialEffect == "BLUE")
  204. {
  205. CCstatus.ImageSpecialEffect = 5;
  206. }
  207. else if (_ImageSpecialEffect == "RETRO")
  208. {
  209. CCstatus.ImageSpecialEffect = 6;
  210. }
  211. else
  212. {
  213. CCstatus.ImageSpecialEffect = 0;
  214. }
  215. }
  216. else if ((toUpper(splitted[0]) == "CAMWBMODE") && (splitted.size() > 1))
  217. {
  218. std::string _ImageWbMode = toUpper(splitted[1]);
  219. if (_ImageWbMode == "SUNNY")
  220. {
  221. CCstatus.ImageWbMode = 1;
  222. }
  223. else if (_ImageWbMode == "CLOUDY")
  224. {
  225. CCstatus.ImageWbMode = 2;
  226. }
  227. else if (_ImageWbMode == "OFFICE")
  228. {
  229. CCstatus.ImageWbMode = 3;
  230. }
  231. else if (_ImageWbMode == "HOME")
  232. {
  233. CCstatus.ImageWbMode = 4;
  234. }
  235. else
  236. {
  237. CCstatus.ImageWbMode = 0;
  238. }
  239. }
  240. else if ((toUpper(splitted[0]) == "CAMAWB") && (splitted.size() > 1))
  241. {
  242. CCstatus.ImageAwb = alphanumericToBoolean(splitted[1]);
  243. }
  244. else if ((toUpper(splitted[0]) == "CAMAWBGAIN") && (splitted.size() > 1))
  245. {
  246. CCstatus.ImageAwbGain = alphanumericToBoolean(splitted[1]);
  247. }
  248. else if ((toUpper(splitted[0]) == "CAMAEC") && (splitted.size() > 1))
  249. {
  250. CCstatus.ImageAec = alphanumericToBoolean(splitted[1]);
  251. }
  252. else if ((toUpper(splitted[0]) == "CAMAEC2") && (splitted.size() > 1))
  253. {
  254. CCstatus.ImageAec2 = alphanumericToBoolean(splitted[1]);
  255. }
  256. else if ((toUpper(splitted[0]) == "CAMAELEVEL") && (splitted.size() > 1))
  257. {
  258. if (isStringNumeric(splitted[1]))
  259. {
  260. int _ImageAeLevel = std::stoi(splitted[1]);
  261. if (CCstatus.CamSensor_id == OV2640_PID)
  262. {
  263. CCstatus.ImageAeLevel = clipInt(_ImageAeLevel, 2, -2);
  264. }
  265. else
  266. {
  267. CCstatus.ImageAeLevel = clipInt(_ImageAeLevel, 5, -5);
  268. }
  269. }
  270. }
  271. else if ((toUpper(splitted[0]) == "CAMAECVALUE") && (splitted.size() > 1))
  272. {
  273. if (isStringNumeric(splitted[1]))
  274. {
  275. int _ImageAecValue = std::stoi(splitted[1]);
  276. CCstatus.ImageAecValue = clipInt(_ImageAecValue, 1200, 0);
  277. }
  278. }
  279. else if ((toUpper(splitted[0]) == "CAMAGC") && (splitted.size() > 1))
  280. {
  281. CCstatus.ImageAgc = alphanumericToBoolean(splitted[1]);
  282. }
  283. else if ((toUpper(splitted[0]) == "CAMAGCGAIN") && (splitted.size() > 1))
  284. {
  285. if (isStringNumeric(splitted[1]))
  286. {
  287. int _ImageAgcGain = std::stoi(splitted[1]);
  288. CCstatus.ImageAgcGain = clipInt(_ImageAgcGain, 30, 0);
  289. }
  290. }
  291. else if ((toUpper(splitted[0]) == "CAMBPC") && (splitted.size() > 1))
  292. {
  293. CCstatus.ImageBpc = alphanumericToBoolean(splitted[1]);
  294. }
  295. else if ((toUpper(splitted[0]) == "CAMWPC") && (splitted.size() > 1))
  296. {
  297. CCstatus.ImageWpc = alphanumericToBoolean(splitted[1]);
  298. }
  299. else if ((toUpper(splitted[0]) == "CAMRAWGMA") && (splitted.size() > 1))
  300. {
  301. CCstatus.ImageRawGma = alphanumericToBoolean(splitted[1]);
  302. }
  303. else if ((toUpper(splitted[0]) == "CAMLENC") && (splitted.size() > 1))
  304. {
  305. CCstatus.ImageLenc = alphanumericToBoolean(splitted[1]);
  306. }
  307. else if ((toUpper(splitted[0]) == "CAMHMIRROR") && (splitted.size() > 1))
  308. {
  309. CCstatus.ImageHmirror = alphanumericToBoolean(splitted[1]);
  310. }
  311. else if ((toUpper(splitted[0]) == "CAMVFLIP") && (splitted.size() > 1))
  312. {
  313. CCstatus.ImageVflip = alphanumericToBoolean(splitted[1]);
  314. }
  315. else if ((toUpper(splitted[0]) == "CAMDCW") && (splitted.size() > 1))
  316. {
  317. CCstatus.ImageDcw = alphanumericToBoolean(splitted[1]);
  318. }
  319. else if ((toUpper(splitted[0]) == "CAMDENOISE") && (splitted.size() > 1))
  320. {
  321. if (isStringNumeric(splitted[1]))
  322. {
  323. int _ImageDenoiseLevel = std::stoi(splitted[1]);
  324. if (CCstatus.CamSensor_id == OV2640_PID)
  325. {
  326. CCstatus.ImageDenoiseLevel = 0;
  327. }
  328. else
  329. {
  330. CCstatus.ImageDenoiseLevel = clipInt(_ImageDenoiseLevel, 8, 0);
  331. }
  332. }
  333. }
  334. else if ((toUpper(splitted[0]) == "CAMZOOM") && (splitted.size() > 1))
  335. {
  336. CCstatus.ImageZoomEnabled = alphanumericToBoolean(splitted[1]);
  337. }
  338. else if ((toUpper(splitted[0]) == "CAMZOOMOFFSETX") && (splitted.size() > 1))
  339. {
  340. if (isStringNumeric(splitted[1]))
  341. {
  342. int _ImageZoomOffsetX = std::stoi(splitted[1]);
  343. if (CCstatus.CamSensor_id == OV2640_PID)
  344. {
  345. CCstatus.ImageZoomOffsetX = clipInt(_ImageZoomOffsetX, 480, -480);
  346. }
  347. else if (CCstatus.CamSensor_id == OV3660_PID)
  348. {
  349. CCstatus.ImageZoomOffsetX = clipInt(_ImageZoomOffsetX, 704, -704);
  350. }
  351. else if (CCstatus.CamSensor_id == OV5640_PID)
  352. {
  353. CCstatus.ImageZoomOffsetX = clipInt(_ImageZoomOffsetX, 960, -960);
  354. }
  355. }
  356. }
  357. else if ((toUpper(splitted[0]) == "CAMZOOMOFFSETY") && (splitted.size() > 1))
  358. {
  359. if (isStringNumeric(splitted[1]))
  360. {
  361. int _ImageZoomOffsetY = std::stoi(splitted[1]);
  362. if (CCstatus.CamSensor_id == OV2640_PID)
  363. {
  364. CCstatus.ImageZoomOffsetY = clipInt(_ImageZoomOffsetY, 360, -360);
  365. }
  366. else if (CCstatus.CamSensor_id == OV3660_PID)
  367. {
  368. CCstatus.ImageZoomOffsetY = clipInt(_ImageZoomOffsetY, 528, -528);
  369. }
  370. else if (CCstatus.CamSensor_id == OV5640_PID)
  371. {
  372. CCstatus.ImageZoomOffsetY = clipInt(_ImageZoomOffsetY, 720, -720);
  373. }
  374. }
  375. }
  376. else if ((toUpper(splitted[0]) == "CAMZOOMSIZE") && (splitted.size() > 1))
  377. {
  378. if (isStringNumeric(splitted[1]))
  379. {
  380. int _ImageZoomSize = std::stoi(splitted[1]);
  381. if (CCstatus.CamSensor_id == OV2640_PID)
  382. {
  383. CCstatus.ImageZoomSize = clipInt(_ImageZoomSize, 29, 0);
  384. }
  385. else if (CCstatus.CamSensor_id == OV3660_PID)
  386. {
  387. CCstatus.ImageZoomSize = clipInt(_ImageZoomSize, 43, 0);
  388. }
  389. else if (CCstatus.CamSensor_id == OV5640_PID)
  390. {
  391. CCstatus.ImageZoomSize = clipInt(_ImageZoomSize, 59, 0);
  392. }
  393. }
  394. }
  395. else if ((toUpper(splitted[0]) == "LEDINTENSITY") && (splitted.size() > 1))
  396. {
  397. if (isStringNumeric(splitted[1]))
  398. {
  399. float ledintensity = std::stof(splitted[1]);
  400. Camera.SetLEDIntensity(ledintensity);
  401. }
  402. }
  403. else if ((toUpper(splitted[0]) == "DEMO") && (splitted.size() > 1))
  404. {
  405. CCstatus.DemoMode = alphanumericToBoolean(splitted[1]);
  406. if (CCstatus.DemoMode == true)
  407. {
  408. Camera.useDemoMode();
  409. }
  410. }
  411. }
  412. Camera.setSensorDatenFromCCstatus(); // CCstatus >>> Kamera
  413. Camera.SetQualityZoomSize(CCstatus.ImageQuality, CCstatus.ImageFrameSize, CCstatus.ImageZoomEnabled, CCstatus.ImageZoomOffsetX, CCstatus.ImageZoomOffsetY, CCstatus.ImageZoomSize, CCstatus.ImageVflip);
  414. rawImage = new CImageBasis("rawImage");
  415. rawImage->CreateEmptyImage(CCstatus.ImageWidth, CCstatus.ImageHeight, 3);
  416. return true;
  417. }
  418. ClassFlowTakeImage::ClassFlowTakeImage(std::vector<ClassFlow *> *lfc) : ClassFlowImage(lfc, TAG)
  419. {
  420. imagesLocation = "/log/source";
  421. imagesRetention = 5;
  422. SetInitialParameter();
  423. }
  424. string ClassFlowTakeImage::getHTMLSingleStep(string host)
  425. {
  426. string result;
  427. result = "Raw Image: <br>\n<img src=\"" + host + "/img_tmp/raw.jpg\">\n";
  428. return result;
  429. }
  430. // wird bei jeder Auswertrunde aufgerufen
  431. bool ClassFlowTakeImage::doFlow(string zwtime)
  432. {
  433. psram_init_shared_memory_for_take_image_step();
  434. string logPath = CreateLogFolder(zwtime);
  435. int flash_duration = (int)(CCstatus.WaitBeforePicture * 1000);
  436. #ifdef DEBUG_DETAIL_ON
  437. LogFile.WriteHeapInfo("ClassFlowTakeImage::doFlow - Before takePictureWithFlash");
  438. #endif
  439. #ifdef WIFITURNOFF
  440. esp_wifi_stop(); // to save power usage and
  441. #endif
  442. // wenn die Kameraeinstellungen durch Erstellen eines neuen Referenzbildes verändert wurden, müssen sie neu gesetzt werden
  443. if (CFstatus.changedCameraSettings)
  444. {
  445. Camera.setSensorDatenFromCCstatus(); // CCstatus >>> Kamera
  446. Camera.SetQualityZoomSize(CCstatus.ImageQuality, CCstatus.ImageFrameSize, CCstatus.ImageZoomEnabled, CCstatus.ImageZoomOffsetX, CCstatus.ImageZoomOffsetY, CCstatus.ImageZoomSize, CCstatus.ImageVflip);
  447. CFstatus.changedCameraSettings = false;
  448. }
  449. takePictureWithFlash(flash_duration);
  450. #ifdef WIFITURNOFF
  451. esp_wifi_start();
  452. #endif
  453. #ifdef DEBUG_DETAIL_ON
  454. LogFile.WriteHeapInfo("ClassFlowTakeImage::doFlow - After takePictureWithFlash");
  455. #endif
  456. LogImage(logPath, "raw", NULL, NULL, zwtime, rawImage);
  457. RemoveOldLogs();
  458. #ifdef DEBUG_DETAIL_ON
  459. LogFile.WriteHeapInfo("ClassFlowTakeImage::doFlow - After RemoveOldLogs");
  460. #endif
  461. psram_deinit_shared_memory_for_take_image_step();
  462. return true;
  463. }
  464. esp_err_t ClassFlowTakeImage::SendRawJPG(httpd_req_t *req)
  465. {
  466. int flash_duration = (int)(CCstatus.WaitBeforePicture * 1000);
  467. time(&TimeImageTaken);
  468. localtime(&TimeImageTaken);
  469. return Camera.CaptureToHTTP(req, flash_duration);
  470. }
  471. ImageData *ClassFlowTakeImage::SendRawImage(void)
  472. {
  473. CImageBasis *zw = new CImageBasis("SendRawImage", rawImage);
  474. ImageData *id;
  475. int flash_duration = (int)(CCstatus.WaitBeforePicture * 1000);
  476. Camera.CaptureToBasisImage(zw, flash_duration);
  477. time(&TimeImageTaken);
  478. localtime(&TimeImageTaken);
  479. id = zw->writeToMemoryAsJPG();
  480. delete zw;
  481. return id;
  482. }
  483. time_t ClassFlowTakeImage::getTimeImageTaken(void)
  484. {
  485. return TimeImageTaken;
  486. }
  487. ClassFlowTakeImage::~ClassFlowTakeImage(void)
  488. {
  489. delete rawImage;
  490. }