ClassFlowTakeImage.cpp 8.3 KB

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