Created
December 20, 2017 17:34
-
-
Save fernandoc1/bcda915a1781a25632e9558ff07dab15 to your computer and use it in GitHub Desktop.
C++ lambda function with Qt Timer and OpenCV example.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <QCoreApplication> | |
| #include <QTimer> | |
| #include <QDebug> | |
| #include <memory> | |
| #include <opencv2/highgui/highgui.hpp> | |
| int main(int argc, char** argv) | |
| { | |
| QCoreApplication a(argc, argv); | |
| ////////////////////////////////////////////////////////////////////// | |
| // This timer is required, because it ensures that all the code // | |
| // runs in the main thread. This issue is due to, apparently, // | |
| // OpenCV not showing it's windows in a thread other than the main. // | |
| ////////////////////////////////////////////////////////////////////// | |
| std::shared_ptr<WebSocketCamera> websocketCamera(new WebSocketCamera("ws://localhost:1234/camera")); | |
| websocketCamera->start(); | |
| std::shared_ptr<cv::Mat> image(new cv::Mat()); | |
| std::shared_ptr<bool> canExit(new bool); | |
| *canExit = false; | |
| QTimer captureLoopTimer(&a); | |
| QObject::connect(&captureLoopTimer, &QTimer::timeout, [=]() | |
| { | |
| websocketCamera->getImage(*image); | |
| cv::imshow("DISPLAY", *image); | |
| int key = cv::waitKey(1); | |
| if(key != -1) | |
| { | |
| *canExit = true; | |
| } | |
| }); | |
| captureLoopTimer.start(); | |
| QTimer monitorKeyTimer(&a); | |
| QObject::connect(&monitorKeyTimer, &QTimer::timeout, [=]() | |
| { | |
| if(*canExit) | |
| { | |
| QCoreApplication::quit(); | |
| } | |
| }); | |
| monitorKeyTimer.start(); | |
| return a.exec(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment