ClassFlowTakeImage.cpp 19 KB

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