ClassFlowTakeImage.cpp 6.8 KB

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