| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570 |
- #include <iostream>
- #include <string>
- #include <vector>
- #include <regex>
- #include "ClassFlowTakeImage.h"
- #include "Helper.h"
- #include "ClassLogFile.h"
- #include "CImageBasis.h"
- #include "ClassControllCamera.h"
- #include "MainFlowControl.h"
- #include "esp_wifi.h"
- #include "esp_log.h"
- #include "../../include/defines.h"
- #include "psram.h"
- #include <time.h>
- // #define DEBUG_DETAIL_ON
- // #define WIFITURNOFF
- static const char *TAG = "TAKEIMAGE";
- esp_err_t ClassFlowTakeImage::camera_capture(void)
- {
- string nm = namerawimage;
- Camera.CaptureToFile(nm);
- time(&TimeImageTaken);
- localtime(&TimeImageTaken);
- return ESP_OK;
- }
- void ClassFlowTakeImage::takePictureWithFlash(int flash_duration)
- {
- // in case the image is flipped, it must be reset here //
- rawImage->width = CCstatus.ImageWidth;
- rawImage->height = CCstatus.ImageHeight;
- ESP_LOGD(TAG, "flash_duration: %d", flash_duration);
- Camera.CaptureToBasisImage(rawImage, flash_duration);
- time(&TimeImageTaken);
- localtime(&TimeImageTaken);
- if (CCstatus.SaveAllFiles)
- {
- rawImage->SaveToFile(namerawimage);
- }
- }
- void ClassFlowTakeImage::SetInitialParameter(void)
- {
- TimeImageTaken = 0;
- rawImage = NULL;
- disabled = false;
- namerawimage = "/sdcard/img_tmp/raw.jpg";
- }
- // auslesen der Kameraeinstellungen aus der config.ini
- // wird beim Start aufgerufen
- bool ClassFlowTakeImage::ReadParameter(FILE *pfile, string &aktparamgraph)
- {
- Camera.getSensorDatenToCCstatus(); // Kamera >>> CCstatus
- std::vector<string> splitted;
- aktparamgraph = trim(aktparamgraph);
- if (aktparamgraph.size() == 0)
- {
- if (!this->GetNextParagraph(pfile, aktparamgraph))
- {
- return false;
- }
- }
- if (aktparamgraph.compare("[TakeImage]") != 0)
- {
- // Paragraph does not fit TakeImage
- return false;
- }
- while (this->getNextLine(pfile, &aktparamgraph) && !this->isNewParagraph(aktparamgraph))
- {
- splitted = ZerlegeZeile(aktparamgraph);
- if ((toUpper(splitted[0]) == "RAWIMAGESLOCATION") && (splitted.size() > 1))
- {
- imagesLocation = "/sdcard" + splitted[1];
- isLogImage = true;
- }
- else if ((toUpper(splitted[0]) == "RAWIMAGESRETENTION") && (splitted.size() > 1))
- {
- if (isStringNumeric(splitted[1]))
- {
- this->imagesRetention = std::stod(splitted[1]);
- }
- }
- else if ((toUpper(splitted[0]) == "SAVEALLFILES") && (splitted.size() > 1))
- {
- CCstatus.SaveAllFiles = alphanumericToBoolean(splitted[1]);
- }
- else if ((toUpper(splitted[0]) == "WAITBEFORETAKINGPICTURE") && (splitted.size() > 1))
- {
- if (isStringNumeric(splitted[1]))
- {
- int _WaitBeforePicture = std::stoi(splitted[1]);
- if (_WaitBeforePicture != 0)
- {
- CCstatus.WaitBeforePicture = _WaitBeforePicture;
- }
- else
- {
- CCstatus.WaitBeforePicture = 2;
- }
- }
- }
- else if ((toUpper(splitted[0]) == "CAMGAINCEILING") && (splitted.size() > 1))
- {
- std::string _ImageGainceiling = toUpper(splitted[1]);
- if (_ImageGainceiling == "X4")
- {
- CCstatus.ImageGainceiling = GAINCEILING_4X;
- }
- else if (_ImageGainceiling == "X8")
- {
- CCstatus.ImageGainceiling = GAINCEILING_8X;
- }
- else if (_ImageGainceiling == "X16")
- {
- CCstatus.ImageGainceiling = GAINCEILING_16X;
- }
- else if (_ImageGainceiling == "X32")
- {
- CCstatus.ImageGainceiling = GAINCEILING_32X;
- }
- else if (_ImageGainceiling == "X64")
- {
- CCstatus.ImageGainceiling = GAINCEILING_64X;
- }
- else if (_ImageGainceiling == "X128")
- {
- CCstatus.ImageGainceiling = GAINCEILING_128X;
- }
- else
- {
- CCstatus.ImageGainceiling = GAINCEILING_2X;
- }
- }
- else if ((toUpper(splitted[0]) == "CAMQUALITY") && (splitted.size() > 1))
- {
- if (isStringNumeric(splitted[1]))
- {
- int _ImageQuality = std::stoi(splitted[1]);
- CCstatus.ImageQuality = clipInt(_ImageQuality, 63, 6);
- }
- }
- else if ((toUpper(splitted[0]) == "CAMBRIGHTNESS") && (splitted.size() > 1))
- {
- if (isStringNumeric(splitted[1]))
- {
- int _ImageBrightness = std::stoi(splitted[1]);
- CCstatus.ImageBrightness = clipInt(_ImageBrightness, 2, -2);
- }
- }
- else if ((toUpper(splitted[0]) == "CAMCONTRAST") && (splitted.size() > 1))
- {
- if (isStringNumeric(splitted[1]))
- {
- int _ImageContrast = std::stoi(splitted[1]);
- CCstatus.ImageContrast = clipInt(_ImageContrast, 2, -2);
- }
- }
- else if ((toUpper(splitted[0]) == "CAMSATURATION") && (splitted.size() > 1))
- {
- if (isStringNumeric(splitted[1]))
- {
- int _ImageSaturation = std::stoi(splitted[1]);
- CCstatus.ImageSaturation = clipInt(_ImageSaturation, 2, -2);
- }
- }
- else if ((toUpper(splitted[0]) == "CAMSHARPNESS") && (splitted.size() > 1))
- {
- if (isStringNumeric(splitted[1]))
- {
- int _ImageSharpness = std::stoi(splitted[1]);
- if (CCstatus.CamSensor_id == OV2640_PID)
- {
- CCstatus.ImageSharpness = clipInt(_ImageSharpness, 2, -2);
- }
- else
- {
- CCstatus.ImageSharpness = clipInt(_ImageSharpness, 3, -3);
- }
- }
- }
- else if ((toUpper(splitted[0]) == "CAMAUTOSHARPNESS") && (splitted.size() > 1))
- {
- CCstatus.ImageAutoSharpness = alphanumericToBoolean(splitted[1]);
- }
- else if ((toUpper(splitted[0]) == "CAMSPECIALEFFECT") && (splitted.size() > 1))
- {
- std::string _ImageSpecialEffect = toUpper(splitted[1]);
- if (_ImageSpecialEffect == "NEGATIVE")
- {
- CCstatus.ImageSpecialEffect = 1;
- }
- else if (_ImageSpecialEffect == "GRAYSCALE")
- {
- CCstatus.ImageSpecialEffect = 2;
- }
- else if (_ImageSpecialEffect == "RED")
- {
- CCstatus.ImageSpecialEffect = 3;
- }
- else if (_ImageSpecialEffect == "GREEN")
- {
- CCstatus.ImageSpecialEffect = 4;
- }
- else if (_ImageSpecialEffect == "BLUE")
- {
- CCstatus.ImageSpecialEffect = 5;
- }
- else if (_ImageSpecialEffect == "RETRO")
- {
- CCstatus.ImageSpecialEffect = 6;
- }
- else
- {
- CCstatus.ImageSpecialEffect = 0;
- }
- }
- else if ((toUpper(splitted[0]) == "CAMWBMODE") && (splitted.size() > 1))
- {
- std::string _ImageWbMode = toUpper(splitted[1]);
- if (_ImageWbMode == "SUNNY")
- {
- CCstatus.ImageWbMode = 1;
- }
- else if (_ImageWbMode == "CLOUDY")
- {
- CCstatus.ImageWbMode = 2;
- }
- else if (_ImageWbMode == "OFFICE")
- {
- CCstatus.ImageWbMode = 3;
- }
- else if (_ImageWbMode == "HOME")
- {
- CCstatus.ImageWbMode = 4;
- }
- else
- {
- CCstatus.ImageWbMode = 0;
- }
- }
- else if ((toUpper(splitted[0]) == "CAMAWB") && (splitted.size() > 1))
- {
- CCstatus.ImageAwb = alphanumericToBoolean(splitted[1]);
- }
- else if ((toUpper(splitted[0]) == "CAMAWBGAIN") && (splitted.size() > 1))
- {
- CCstatus.ImageAwbGain = alphanumericToBoolean(splitted[1]);
- }
- else if ((toUpper(splitted[0]) == "CAMAEC") && (splitted.size() > 1))
- {
- CCstatus.ImageAec = alphanumericToBoolean(splitted[1]);
- }
- else if ((toUpper(splitted[0]) == "CAMAEC2") && (splitted.size() > 1))
- {
- CCstatus.ImageAec2 = alphanumericToBoolean(splitted[1]);
- }
- else if ((toUpper(splitted[0]) == "CAMAELEVEL") && (splitted.size() > 1))
- {
- if (isStringNumeric(splitted[1]))
- {
- int _ImageAeLevel = std::stoi(splitted[1]);
- if (CCstatus.CamSensor_id == OV2640_PID)
- {
- CCstatus.ImageAeLevel = clipInt(_ImageAeLevel, 2, -2);
- }
- else
- {
- CCstatus.ImageAeLevel = clipInt(_ImageAeLevel, 5, -5);
- }
- }
- }
- else if ((toUpper(splitted[0]) == "CAMAECVALUE") && (splitted.size() > 1))
- {
- if (isStringNumeric(splitted[1]))
- {
- int _ImageAecValue = std::stoi(splitted[1]);
- CCstatus.ImageAecValue = clipInt(_ImageAecValue, 1200, 0);
- }
- }
- else if ((toUpper(splitted[0]) == "CAMAGC") && (splitted.size() > 1))
- {
- CCstatus.ImageAgc = alphanumericToBoolean(splitted[1]);
- }
- else if ((toUpper(splitted[0]) == "CAMAGCGAIN") && (splitted.size() > 1))
- {
- if (isStringNumeric(splitted[1]))
- {
- int _ImageAgcGain = std::stoi(splitted[1]);
- CCstatus.ImageAgcGain = clipInt(_ImageAgcGain, 30, 0);
- }
- }
- else if ((toUpper(splitted[0]) == "CAMBPC") && (splitted.size() > 1))
- {
- CCstatus.ImageBpc = alphanumericToBoolean(splitted[1]);
- }
- else if ((toUpper(splitted[0]) == "CAMWPC") && (splitted.size() > 1))
- {
- CCstatus.ImageWpc = alphanumericToBoolean(splitted[1]);
- }
- else if ((toUpper(splitted[0]) == "CAMRAWGMA") && (splitted.size() > 1))
- {
- CCstatus.ImageRawGma = alphanumericToBoolean(splitted[1]);
- }
- else if ((toUpper(splitted[0]) == "CAMLENC") && (splitted.size() > 1))
- {
- CCstatus.ImageLenc = alphanumericToBoolean(splitted[1]);
- }
- else if ((toUpper(splitted[0]) == "CAMHMIRROR") && (splitted.size() > 1))
- {
- CCstatus.ImageHmirror = alphanumericToBoolean(splitted[1]);
- }
- else if ((toUpper(splitted[0]) == "CAMVFLIP") && (splitted.size() > 1))
- {
- CCstatus.ImageVflip = alphanumericToBoolean(splitted[1]);
- }
- else if ((toUpper(splitted[0]) == "CAMDCW") && (splitted.size() > 1))
- {
- CCstatus.ImageDcw = alphanumericToBoolean(splitted[1]);
- }
- else if ((toUpper(splitted[0]) == "CAMDENOISE") && (splitted.size() > 1))
- {
- if (isStringNumeric(splitted[1]))
- {
- int _ImageDenoiseLevel = std::stoi(splitted[1]);
- if (CCstatus.CamSensor_id == OV2640_PID)
- {
- CCstatus.ImageDenoiseLevel = 0;
- }
- else
- {
- CCstatus.ImageDenoiseLevel = clipInt(_ImageDenoiseLevel, 8, 0);
- }
- }
- }
- else if ((toUpper(splitted[0]) == "CAMZOOM") && (splitted.size() > 1))
- {
- CCstatus.ImageZoomEnabled = alphanumericToBoolean(splitted[1]);
- }
- else if ((toUpper(splitted[0]) == "CAMZOOMOFFSETX") && (splitted.size() > 1))
- {
- if (isStringNumeric(splitted[1]))
- {
- int _ImageZoomOffsetX = std::stoi(splitted[1]);
- if (CCstatus.CamSensor_id == OV2640_PID)
- {
- CCstatus.ImageZoomOffsetX = clipInt(_ImageZoomOffsetX, 480, -480);
- }
- else if (CCstatus.CamSensor_id == OV3660_PID)
- {
- CCstatus.ImageZoomOffsetX = clipInt(_ImageZoomOffsetX, 704, -704);
- }
- else if (CCstatus.CamSensor_id == OV5640_PID)
- {
- CCstatus.ImageZoomOffsetX = clipInt(_ImageZoomOffsetX, 960, -960);
- }
- }
- }
- else if ((toUpper(splitted[0]) == "CAMZOOMOFFSETY") && (splitted.size() > 1))
- {
- if (isStringNumeric(splitted[1]))
- {
- int _ImageZoomOffsetY = std::stoi(splitted[1]);
- if (CCstatus.CamSensor_id == OV2640_PID)
- {
- CCstatus.ImageZoomOffsetY = clipInt(_ImageZoomOffsetY, 360, -360);
- }
- else if (CCstatus.CamSensor_id == OV3660_PID)
- {
- CCstatus.ImageZoomOffsetY = clipInt(_ImageZoomOffsetY, 528, -528);
- }
- else if (CCstatus.CamSensor_id == OV5640_PID)
- {
- CCstatus.ImageZoomOffsetY = clipInt(_ImageZoomOffsetY, 720, -720);
- }
- }
- }
- else if ((toUpper(splitted[0]) == "CAMZOOMSIZE") && (splitted.size() > 1))
- {
- if (isStringNumeric(splitted[1]))
- {
- int _ImageZoomSize = std::stoi(splitted[1]);
- if (CCstatus.CamSensor_id == OV2640_PID)
- {
- CCstatus.ImageZoomSize = clipInt(_ImageZoomSize, 29, 0);
- }
- else if (CCstatus.CamSensor_id == OV3660_PID)
- {
- CCstatus.ImageZoomSize = clipInt(_ImageZoomSize, 43, 0);
- }
- else if (CCstatus.CamSensor_id == OV5640_PID)
- {
- CCstatus.ImageZoomSize = clipInt(_ImageZoomSize, 59, 0);
- }
- }
- }
- else if ((toUpper(splitted[0]) == "LEDINTENSITY") && (splitted.size() > 1))
- {
- if (isStringNumeric(splitted[1]))
- {
- float ledintensity = std::stof(splitted[1]);
- Camera.SetLEDIntensity(ledintensity);
- }
- }
- else if ((toUpper(splitted[0]) == "DEMO") && (splitted.size() > 1))
- {
- CCstatus.DemoMode = alphanumericToBoolean(splitted[1]);
- if (CCstatus.DemoMode == true)
- {
- Camera.useDemoMode();
- }
- }
- }
- Camera.setSensorDatenFromCCstatus(); // CCstatus >>> Kamera
- Camera.SetQualityZoomSize(CCstatus.ImageQuality, CCstatus.ImageFrameSize, CCstatus.ImageZoomEnabled, CCstatus.ImageZoomOffsetX, CCstatus.ImageZoomOffsetY, CCstatus.ImageZoomSize, CCstatus.ImageVflip);
- rawImage = new CImageBasis("rawImage");
- rawImage->CreateEmptyImage(CCstatus.ImageWidth, CCstatus.ImageHeight, 3);
- return true;
- }
- ClassFlowTakeImage::ClassFlowTakeImage(std::vector<ClassFlow *> *lfc) : ClassFlowImage(lfc, TAG)
- {
- imagesLocation = "/log/source";
- imagesRetention = 5;
- SetInitialParameter();
- }
- string ClassFlowTakeImage::getHTMLSingleStep(string host)
- {
- string result;
- result = "Raw Image: <br>\n<img src=\"" + host + "/img_tmp/raw.jpg\">\n";
- return result;
- }
- // wird bei jeder Auswertrunde aufgerufen
- bool ClassFlowTakeImage::doFlow(string zwtime)
- {
- psram_init_shared_memory_for_take_image_step();
- string logPath = CreateLogFolder(zwtime);
- int flash_duration = (int)(CCstatus.WaitBeforePicture * 1000);
- #ifdef DEBUG_DETAIL_ON
- LogFile.WriteHeapInfo("ClassFlowTakeImage::doFlow - Before takePictureWithFlash");
- #endif
- #ifdef WIFITURNOFF
- esp_wifi_stop(); // to save power usage and
- #endif
- // wenn die Kameraeinstellungen durch Erstellen eines neuen Referenzbildes verändert wurden, müssen sie neu gesetzt werden
- if (CFstatus.changedCameraSettings)
- {
- Camera.setSensorDatenFromCCstatus(); // CCstatus >>> Kamera
- Camera.SetQualityZoomSize(CCstatus.ImageQuality, CCstatus.ImageFrameSize, CCstatus.ImageZoomEnabled, CCstatus.ImageZoomOffsetX, CCstatus.ImageZoomOffsetY, CCstatus.ImageZoomSize, CCstatus.ImageVflip);
- CFstatus.changedCameraSettings = false;
- }
- takePictureWithFlash(flash_duration);
- #ifdef WIFITURNOFF
- esp_wifi_start();
- #endif
- #ifdef DEBUG_DETAIL_ON
- LogFile.WriteHeapInfo("ClassFlowTakeImage::doFlow - After takePictureWithFlash");
- #endif
- LogImage(logPath, "raw", NULL, NULL, zwtime, rawImage);
- RemoveOldLogs();
- #ifdef DEBUG_DETAIL_ON
- LogFile.WriteHeapInfo("ClassFlowTakeImage::doFlow - After RemoveOldLogs");
- #endif
- psram_deinit_shared_memory_for_take_image_step();
- return true;
- }
- esp_err_t ClassFlowTakeImage::SendRawJPG(httpd_req_t *req)
- {
- int flash_duration = (int)(CCstatus.WaitBeforePicture * 1000);
- time(&TimeImageTaken);
- localtime(&TimeImageTaken);
- return Camera.CaptureToHTTP(req, flash_duration);
- }
- ImageData *ClassFlowTakeImage::SendRawImage(void)
- {
- CImageBasis *zw = new CImageBasis("SendRawImage", rawImage);
- ImageData *id;
- int flash_duration = (int)(CCstatus.WaitBeforePicture * 1000);
- Camera.CaptureToBasisImage(zw, flash_duration);
- time(&TimeImageTaken);
- localtime(&TimeImageTaken);
- id = zw->writeToMemoryAsJPG();
- delete zw;
- return id;
- }
- time_t ClassFlowTakeImage::getTimeImageTaken(void)
- {
- return TimeImageTaken;
- }
- ClassFlowTakeImage::~ClassFlowTakeImage(void)
- {
- delete rawImage;
- }
|