ClassFlowTakeImage.cpp 7.0 KB

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