diff options
Diffstat (limited to 'app/camera.cpp')
-rw-r--r-- | app/camera.cpp | 49 |
1 files changed, 20 insertions, 29 deletions
diff --git a/app/camera.cpp b/app/camera.cpp index bd3dd99..f935f22 100644 --- a/app/camera.cpp +++ b/app/camera.cpp @@ -16,7 +16,7 @@ #include "camera.h" #include <QPainter> -#include <opencv2/imgproc/imgproc.hpp> +#include <opencv2/imgproc.hpp> #include <linux/videodev2.h> #include <fcntl.h> @@ -67,54 +67,45 @@ void Camera::enumerateCameras() { } camcnt = cam_arr.length(); - cam_arr_bak = cam_arr; } void Camera::start(int no, int fps, QString res) { - try{ - for (QVariantList::iterator iter = cam_arr_bak.begin(); iter != cam_arr_bak.end(); ++iter){ - if (*iter == no){ - cam_arr_bak.erase(iter); - break; - } + int retryCount = 0; + while(!capture->open(no)){ + if(retryCount++==5){ + qDebug()<< "Try to open camera for 5 times failed, give up."; + return; + }else{ + qDebug()<< "open camera failed, retry " << retryCount; + usleep(200000); } + } - if (capture && capture->open(no)){ - capture->set(CV_CAP_PROP_FRAME_WIDTH, res.section("*", 0, 0).toInt()); - capture->set(CV_CAP_PROP_FRAME_HEIGHT, res.section("*", 1, 1).toInt()); - - if (fps > 0){ - timer->start(1000/fps); - running = true; - emit isrunningChanged(running); + capture->set(CAP_PROP_FRAME_WIDTH, res.section("*", 0, 0).toInt()); + capture->set(CAP_PROP_FRAME_HEIGHT, res.section("*", 1, 1).toInt()); - emit camraCntChanged(cam_arr_bak); - } - camnumbackup = no; - } - } - catch(cv::Exception & e) { - qDebug() << "als-meter-demo open device error: " << e.msg.c_str(); + if (fps > 0){ + timer->start(1000/fps); + running = true; + emit isrunningChanged(running); } } void Camera::stop() { - cam_arr_bak.push_back(camnumbackup); - if (timer->isActive()) timer->stop(); - if (capture && capture->isOpened()) + if (capture->isOpened()){ + qDebug()<< "release camera."; capture->release(); + } image = QImage(); update(); running = false; emit isrunningChanged(running); - - emit camraCntChanged(cam_arr_bak); } QVariantList Camera::camranum() const{ - return cam_arr_bak; + return cam_arr; } int Camera::cameraCnt() { |