ClassFlowTakeImage.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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 = -100;
  69. int _contrast = -100;
  70. int _saturation = -100;
  71. int _autoExposureLevel = 0;
  72. if (aktparamgraph.size() == 0)
  73. if (!this->GetNextParagraph(pfile, aktparamgraph))
  74. return false;
  75. if (aktparamgraph.compare("[TakeImage]") != 0) // Paragraph does not fit TakeImage
  76. return false;
  77. while (this->getNextLine(pfile, &aktparamgraph) && !this->isNewParagraph(aktparamgraph))
  78. {
  79. splitted = ZerlegeZeile(aktparamgraph);
  80. if ((toUpper(splitted[0]) == "RAWIMAGESLOCATION") && (splitted.size() > 1))
  81. {
  82. imagesLocation = "/sdcard" + splitted[1];
  83. isLogImage = true;
  84. }
  85. if ((toUpper(splitted[0]) == "IMAGEQUALITY") && (splitted.size() > 1))
  86. ImageQuality = std::stod(splitted[1]);
  87. if ((toUpper(splitted[0]) == "ZOOM") && (splitted.size() > 1))
  88. {
  89. if (toUpper(splitted[1]) == "TRUE")
  90. ZoomEnabled = true;
  91. else if (toUpper(splitted[1]) == "FALSE")
  92. ZoomEnabled = false;
  93. }
  94. if ((toUpper(splitted[0]) == "ZOOMMODE") && (splitted.size() > 1))
  95. ZoomMode = std::stod(splitted[1]);
  96. if ((toUpper(splitted[0]) == "ZOOMOFFSETX") && (splitted.size() > 1))
  97. zoomOffsetX = std::stod(splitted[1]);
  98. if ((toUpper(splitted[0]) == "ZOOMOFFSETY") && (splitted.size() > 1))
  99. zoomOffsetY = std::stod(splitted[1]);
  100. if ((toUpper(splitted[0]) == "GRAYSCALE") && (splitted.size() > 1))
  101. {
  102. if (toUpper(splitted[1]) == "TRUE")
  103. ImageGrayscale = true;
  104. else if (toUpper(splitted[1]) == "FALSE")
  105. ImageGrayscale = false;
  106. }
  107. if ((toUpper(splitted[0]) == "NEGATIVE") && (splitted.size() > 1))
  108. {
  109. if (toUpper(splitted[1]) == "TRUE")
  110. ImageNegative = true;
  111. else if (toUpper(splitted[1]) == "FALSE")
  112. ImageNegative = false;
  113. }
  114. if ((toUpper(splitted[0]) == "AEC2") && (splitted.size() > 1))
  115. {
  116. if (toUpper(splitted[1]) == "TRUE")
  117. ImageAec2 = true;
  118. else if (toUpper(splitted[1]) == "FALSE")
  119. ImageAec2 = false;
  120. }
  121. if ((toUpper(splitted[0]) == "AUTOEXPOSURELEVEL") && (splitted.size() > 1))
  122. _autoExposureLevel = std::stod(splitted[1]);
  123. if ((toUpper(splitted[0]) == "IMAGESIZE") && (splitted.size() > 1))
  124. {
  125. ImageSize = Camera.TextToFramesize(splitted[1].c_str());
  126. isImageSize = true;
  127. }
  128. if ((toUpper(splitted[0]) == "SAVEALLFILES") && (splitted.size() > 1))
  129. {
  130. if (toUpper(splitted[1]) == "TRUE")
  131. SaveAllFiles = true;
  132. }
  133. if ((toUpper(splitted[0]) == "WAITBEFORETAKINGPICTURE") && (splitted.size() > 1))
  134. {
  135. waitbeforepicture = stoi(splitted[1]);
  136. }
  137. if ((toUpper(splitted[0]) == "RAWIMAGESRETENTION") && (splitted.size() > 1))
  138. {
  139. this->imagesRetention = std::stoi(splitted[1]);
  140. }
  141. if ((toUpper(splitted[0]) == "BRIGHTNESS") && (splitted.size() > 1))
  142. {
  143. _brightness = stoi(splitted[1]);
  144. }
  145. if ((toUpper(splitted[0]) == "CONTRAST") && (splitted.size() > 1))
  146. {
  147. _contrast = stoi(splitted[1]);
  148. }
  149. if ((toUpper(splitted[0]) == "SATURATION") && (splitted.size() > 1))
  150. {
  151. _saturation = stoi(splitted[1]);
  152. }
  153. if ((toUpper(splitted[0]) == "FIXEDEXPOSURE") && (splitted.size() > 1))
  154. {
  155. if (toUpper(splitted[1]) == "TRUE")
  156. FixedExposure = true;
  157. }
  158. if ((toUpper(splitted[0]) == "LEDINTENSITY") && (splitted.size() > 1))
  159. {
  160. float ledintensity = stof(splitted[1]);
  161. ledintensity = min((float) 100, ledintensity);
  162. ledintensity = max((float) 0, ledintensity);
  163. Camera.SetLEDIntensity(ledintensity);
  164. }
  165. if ((toUpper(splitted[0]) == "DEMO") && (splitted.size() > 1))
  166. {
  167. if (toUpper(splitted[1]) == "TRUE")
  168. Camera.useDemoMode();
  169. }
  170. }
  171. Camera.SetBrightnessContrastSaturation(_brightness, _contrast, _saturation, _autoExposureLevel, ImageGrayscale, ImageNegative, ImageAec2);
  172. Camera.SetQualitySize(ImageQuality, ImageSize, ZoomEnabled, ZoomMode, zoomOffsetX, zoomOffsetY);
  173. image_width = Camera.image_width;
  174. image_height = Camera.image_height;
  175. rawImage = new CImageBasis("rawImage");
  176. rawImage->CreateEmptyImage(image_width, image_height, 3);
  177. waitbeforepicture_store = waitbeforepicture;
  178. if (FixedExposure && (waitbeforepicture > 0))
  179. {
  180. // ESP_LOGD(TAG, "Fixed Exposure enabled!");
  181. int flash_duration = (int) (waitbeforepicture * 1000);
  182. Camera.EnableAutoExposure(flash_duration);
  183. waitbeforepicture = 0.2;
  184. // flash_duration = (int) (waitbeforepicture * 1000);
  185. // takePictureWithFlash(flash_duration);
  186. // rawImage->SaveToFile("/sdcard/init2.jpg");
  187. }
  188. return true;
  189. }
  190. string ClassFlowTakeImage::getHTMLSingleStep(string host)
  191. {
  192. string result;
  193. result = "Raw Image: <br>\n<img src=\"" + host + "/img_tmp/raw.jpg\">\n";
  194. return result;
  195. }
  196. bool ClassFlowTakeImage::doFlow(string zwtime)
  197. {
  198. psram_init_shared_memory_for_take_image_step();
  199. string logPath = CreateLogFolder(zwtime);
  200. int flash_duration = (int) (waitbeforepicture * 1000);
  201. #ifdef DEBUG_DETAIL_ON
  202. LogFile.WriteHeapInfo("ClassFlowTakeImage::doFlow - Before takePictureWithFlash");
  203. #endif
  204. #ifdef WIFITURNOFF
  205. esp_wifi_stop(); // to save power usage and
  206. #endif
  207. takePictureWithFlash(flash_duration);
  208. #ifdef WIFITURNOFF
  209. esp_wifi_start();
  210. #endif
  211. #ifdef DEBUG_DETAIL_ON
  212. LogFile.WriteHeapInfo("ClassFlowTakeImage::doFlow - After takePictureWithFlash");
  213. #endif
  214. LogImage(logPath, "raw", NULL, NULL, zwtime, rawImage);
  215. RemoveOldLogs();
  216. #ifdef DEBUG_DETAIL_ON
  217. LogFile.WriteHeapInfo("ClassFlowTakeImage::doFlow - After RemoveOldLogs");
  218. #endif
  219. psram_deinit_shared_memory_for_take_image_step();
  220. return true;
  221. }
  222. esp_err_t ClassFlowTakeImage::SendRawJPG(httpd_req_t *req)
  223. {
  224. int flash_duration = (int) (waitbeforepicture * 1000);
  225. time(&TimeImageTaken);
  226. localtime(&TimeImageTaken);
  227. return Camera.CaptureToHTTP(req, flash_duration);
  228. }
  229. ImageData* ClassFlowTakeImage::SendRawImage()
  230. {
  231. CImageBasis *zw = new CImageBasis("SendRawImage", rawImage);
  232. ImageData *id;
  233. int flash_duration = (int) (waitbeforepicture * 1000);
  234. Camera.CaptureToBasisImage(zw, flash_duration);
  235. time(&TimeImageTaken);
  236. localtime(&TimeImageTaken);
  237. id = zw->writeToMemoryAsJPG();
  238. delete zw;
  239. return id;
  240. }
  241. time_t ClassFlowTakeImage::getTimeImageTaken()
  242. {
  243. return TimeImageTaken;
  244. }
  245. ClassFlowTakeImage::~ClassFlowTakeImage(void)
  246. {
  247. delete rawImage;
  248. }