ClassFlowTakeImage.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. #include "ClassFlowTakeImage.h"
  2. #include "Helper.h"
  3. #include "ClassLogFile.h"
  4. #include "CImageBasis.h"
  5. #include "ClassControllCamera.h"
  6. #include "esp_wifi.h"
  7. #include "esp_log.h"
  8. #include "../../include/defines.h"
  9. #include "psram.h"
  10. #include <time.h>
  11. // #define DEBUG_DETAIL_ON
  12. // #define WIFITURNOFF
  13. static const char* TAG = "TAKEIMAGE";
  14. esp_err_t ClassFlowTakeImage::camera_capture(){
  15. string nm = namerawimage;
  16. Camera.CaptureToFile(nm);
  17. time(&TimeImageTaken);
  18. localtime(&TimeImageTaken);
  19. return ESP_OK;
  20. }
  21. void ClassFlowTakeImage::takePictureWithFlash(int flash_duration)
  22. {
  23. // in case the image is flipped, it must be reset here //
  24. rawImage->width = image_width;
  25. rawImage->height = image_height;
  26. /////////////////////////////////////////////////////////////////////////////////////
  27. ESP_LOGD(TAG, "flash_duration: %d", flash_duration);
  28. Camera.CaptureToBasisImage(rawImage, flash_duration);
  29. time(&TimeImageTaken);
  30. localtime(&TimeImageTaken);
  31. if (SaveAllFiles) rawImage->SaveToFile(namerawimage);
  32. }
  33. void ClassFlowTakeImage::SetInitialParameter(void)
  34. {
  35. waitbeforepicture = 5;
  36. isImageSize = false;
  37. ImageQuality = -1;
  38. TimeImageTaken = 0;
  39. ImageQuality = 5;
  40. rawImage = NULL;
  41. ImageSize = FRAMESIZE_VGA;
  42. ZoomEnabled = false;
  43. ZoomMode = 0;
  44. zoomOffsetX = 0;
  45. zoomOffsetY = 0;
  46. ImageNegative = false;
  47. ImageAec2 = false;
  48. #ifdef GRAYSCALE_AS_DEFAULT
  49. ImageGrayscale = true;
  50. #else
  51. ImageGrayscale = false;
  52. #endif
  53. SaveAllFiles = false;
  54. disabled = false;
  55. FixedExposure = false;
  56. namerawimage = "/sdcard/img_tmp/raw.jpg";
  57. }
  58. ClassFlowTakeImage::ClassFlowTakeImage(std::vector<ClassFlow*>* lfc) : ClassFlowImage(lfc, TAG)
  59. {
  60. imagesLocation = "/log/source";
  61. imagesRetention = 5;
  62. SetInitialParameter();
  63. }
  64. bool ClassFlowTakeImage::ReadParameter(FILE* pfile, string& aktparamgraph)
  65. {
  66. std::vector<string> splitted;
  67. aktparamgraph = trim(aktparamgraph);
  68. int _brightness = 0;
  69. int _contrast = 0;
  70. int _saturation = 0;
  71. int _sharpness = 0;
  72. int _autoExposureLevel = 0;
  73. if (aktparamgraph.size() == 0)
  74. if (!this->GetNextParagraph(pfile, aktparamgraph))
  75. return false;
  76. if (aktparamgraph.compare("[TakeImage]") != 0) // Paragraph does not fit TakeImage
  77. return false;
  78. while (this->getNextLine(pfile, &aktparamgraph) && !this->isNewParagraph(aktparamgraph))
  79. {
  80. splitted = ZerlegeZeile(aktparamgraph);
  81. if ((toUpper(splitted[0]) == "RAWIMAGESLOCATION") && (splitted.size() > 1))
  82. {
  83. imagesLocation = "/sdcard" + splitted[1];
  84. isLogImage = true;
  85. }
  86. if ((toUpper(splitted[0]) == "IMAGEQUALITY") && (splitted.size() > 1))
  87. ImageQuality = std::stod(splitted[1]);
  88. if ((toUpper(splitted[0]) == "ZOOM") && (splitted.size() > 1))
  89. {
  90. if (toUpper(splitted[1]) == "TRUE")
  91. ZoomEnabled = true;
  92. else if (toUpper(splitted[1]) == "FALSE")
  93. ZoomEnabled = false;
  94. }
  95. if ((toUpper(splitted[0]) == "ZOOMMODE") && (splitted.size() > 1))
  96. ZoomMode = std::stod(splitted[1]);
  97. if ((toUpper(splitted[0]) == "ZOOMOFFSETX") && (splitted.size() > 1))
  98. zoomOffsetX = std::stod(splitted[1]);
  99. if ((toUpper(splitted[0]) == "ZOOMOFFSETY") && (splitted.size() > 1))
  100. zoomOffsetY = std::stod(splitted[1]);
  101. if ((toUpper(splitted[0]) == "GRAYSCALE") && (splitted.size() > 1))
  102. {
  103. if (toUpper(splitted[1]) == "TRUE")
  104. ImageGrayscale = true;
  105. else if (toUpper(splitted[1]) == "FALSE")
  106. ImageGrayscale = false;
  107. }
  108. if ((toUpper(splitted[0]) == "NEGATIVE") && (splitted.size() > 1))
  109. {
  110. if (toUpper(splitted[1]) == "TRUE")
  111. ImageNegative = true;
  112. else if (toUpper(splitted[1]) == "FALSE")
  113. ImageNegative = false;
  114. }
  115. if ((toUpper(splitted[0]) == "AEC2") && (splitted.size() > 1))
  116. {
  117. if (toUpper(splitted[1]) == "TRUE")
  118. ImageAec2 = true;
  119. else if (toUpper(splitted[1]) == "FALSE")
  120. ImageAec2 = false;
  121. }
  122. if ((toUpper(splitted[0]) == "AUTOEXPOSURELEVEL") && (splitted.size() > 1))
  123. _autoExposureLevel = std::stod(splitted[1]);
  124. if ((toUpper(splitted[0]) == "IMAGESIZE") && (splitted.size() > 1))
  125. {
  126. ImageSize = Camera.TextToFramesize(splitted[1].c_str());
  127. isImageSize = true;
  128. }
  129. if ((toUpper(splitted[0]) == "SAVEALLFILES") && (splitted.size() > 1))
  130. {
  131. if (toUpper(splitted[1]) == "TRUE")
  132. SaveAllFiles = true;
  133. }
  134. if ((toUpper(splitted[0]) == "WAITBEFORETAKINGPICTURE") && (splitted.size() > 1))
  135. {
  136. waitbeforepicture = stoi(splitted[1]);
  137. }
  138. if ((toUpper(splitted[0]) == "RAWIMAGESRETENTION") && (splitted.size() > 1))
  139. {
  140. this->imagesRetention = std::stoi(splitted[1]);
  141. }
  142. if ((toUpper(splitted[0]) == "BRIGHTNESS") && (splitted.size() > 1))
  143. {
  144. _brightness = stoi(splitted[1]);
  145. }
  146. if ((toUpper(splitted[0]) == "CONTRAST") && (splitted.size() > 1))
  147. {
  148. _contrast = stoi(splitted[1]);
  149. }
  150. if ((toUpper(splitted[0]) == "SATURATION") && (splitted.size() > 1))
  151. {
  152. _saturation = stoi(splitted[1]);
  153. }
  154. if ((toUpper(splitted[0]) == "SHARPNESS") && (splitted.size() > 1))
  155. {
  156. _sharpness = stoi(splitted[1]);
  157. }
  158. if ((toUpper(splitted[0]) == "FIXEDEXPOSURE") && (splitted.size() > 1))
  159. {
  160. if (toUpper(splitted[1]) == "TRUE")
  161. FixedExposure = true;
  162. }
  163. if ((toUpper(splitted[0]) == "LEDINTENSITY") && (splitted.size() > 1))
  164. {
  165. float ledintensity = stof(splitted[1]);
  166. ledintensity = min((float) 100, ledintensity);
  167. ledintensity = max((float) 0, ledintensity);
  168. Camera.SetLEDIntensity(ledintensity);
  169. }
  170. if ((toUpper(splitted[0]) == "DEMO") && (splitted.size() > 1))
  171. {
  172. if (toUpper(splitted[1]) == "TRUE")
  173. Camera.useDemoMode();
  174. }
  175. }
  176. Camera.SetBrightnessContrastSaturation(_brightness, _contrast, _saturation, _autoExposureLevel, ImageGrayscale, ImageNegative, ImageAec2, _sharpness);
  177. Camera.SetQualitySize(ImageQuality, ImageSize, ZoomEnabled, ZoomMode, zoomOffsetX, zoomOffsetY);
  178. image_width = Camera.image_width;
  179. image_height = Camera.image_height;
  180. rawImage = new CImageBasis("rawImage");
  181. rawImage->CreateEmptyImage(image_width, image_height, 3);
  182. waitbeforepicture_store = waitbeforepicture;
  183. if (FixedExposure && (waitbeforepicture > 0))
  184. {
  185. // ESP_LOGD(TAG, "Fixed Exposure enabled!");
  186. int flash_duration = (int) (waitbeforepicture * 1000);
  187. Camera.EnableAutoExposure(flash_duration);
  188. waitbeforepicture = 0.2;
  189. // flash_duration = (int) (waitbeforepicture * 1000);
  190. // takePictureWithFlash(flash_duration);
  191. // rawImage->SaveToFile("/sdcard/init2.jpg");
  192. }
  193. return true;
  194. }
  195. string ClassFlowTakeImage::getHTMLSingleStep(string host)
  196. {
  197. string result;
  198. result = "Raw Image: <br>\n<img src=\"" + host + "/img_tmp/raw.jpg\">\n";
  199. return result;
  200. }
  201. bool ClassFlowTakeImage::doFlow(string zwtime)
  202. {
  203. psram_init_shared_memory_for_take_image_step();
  204. string logPath = CreateLogFolder(zwtime);
  205. int flash_duration = (int) (waitbeforepicture * 1000);
  206. #ifdef DEBUG_DETAIL_ON
  207. LogFile.WriteHeapInfo("ClassFlowTakeImage::doFlow - Before takePictureWithFlash");
  208. #endif
  209. #ifdef WIFITURNOFF
  210. esp_wifi_stop(); // to save power usage and
  211. #endif
  212. takePictureWithFlash(flash_duration);
  213. #ifdef WIFITURNOFF
  214. esp_wifi_start();
  215. #endif
  216. #ifdef DEBUG_DETAIL_ON
  217. LogFile.WriteHeapInfo("ClassFlowTakeImage::doFlow - After takePictureWithFlash");
  218. #endif
  219. LogImage(logPath, "raw", NULL, NULL, zwtime, rawImage);
  220. RemoveOldLogs();
  221. #ifdef DEBUG_DETAIL_ON
  222. LogFile.WriteHeapInfo("ClassFlowTakeImage::doFlow - After RemoveOldLogs");
  223. #endif
  224. psram_deinit_shared_memory_for_take_image_step();
  225. return true;
  226. }
  227. esp_err_t ClassFlowTakeImage::SendRawJPG(httpd_req_t *req)
  228. {
  229. int flash_duration = (int) (waitbeforepicture * 1000);
  230. time(&TimeImageTaken);
  231. localtime(&TimeImageTaken);
  232. return Camera.CaptureToHTTP(req, flash_duration);
  233. }
  234. ImageData* ClassFlowTakeImage::SendRawImage()
  235. {
  236. CImageBasis *zw = new CImageBasis("SendRawImage", rawImage);
  237. ImageData *id;
  238. int flash_duration = (int) (waitbeforepicture * 1000);
  239. Camera.CaptureToBasisImage(zw, flash_duration);
  240. time(&TimeImageTaken);
  241. localtime(&TimeImageTaken);
  242. id = zw->writeToMemoryAsJPG();
  243. delete zw;
  244. return id;
  245. }
  246. time_t ClassFlowTakeImage::getTimeImageTaken()
  247. {
  248. return TimeImageTaken;
  249. }
  250. ClassFlowTakeImage::~ClassFlowTakeImage(void)
  251. {
  252. delete rawImage;
  253. }