From bdecd54715fe22d7f2ceef70a749588ea7c725f2 Mon Sep 17 00:00:00 2001 From: zheng_wenlong Date: Tue, 25 Dec 2018 16:54:55 +0900 Subject: use camera index from end --- app/camera.cpp | 49 ++++++++++++++++++++----------------------------- app/camera.h | 9 ++++----- app/main.qml | 5 ++--- 3 files changed, 26 insertions(+), 37 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 -#include +#include #include #include @@ -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() { diff --git a/app/camera.h b/app/camera.h index 6d5d17c..11b5e82 100644 --- a/app/camera.h +++ b/app/camera.h @@ -21,9 +21,11 @@ #include #include #include +#include #include -#include -#include +#include +#include +#include using namespace std; @@ -48,7 +50,6 @@ public: signals: void isrunningChanged(const bool& isrunning); - void camraCntChanged(const QVariantList& camcnt); public slots: void grab(); @@ -60,8 +61,6 @@ private: cv::VideoCapture* capture; QVariantList cam_arr; - - int camnumbackup; int camcnt; }; diff --git a/app/main.qml b/app/main.qml index 1c52928..be756b8 100644 --- a/app/main.qml +++ b/app/main.qml @@ -57,16 +57,13 @@ ApplicationWindow { onTextMessageReceived: { var message_json = JSON.parse(message); - console.log("Raw response: " + message) if (message_json[0] === msgid_enu.event){ var propertyName = message_json[2].event.split("/")[1] if (propertyName === "messages.vehicle.average.speed"){ vehiclespeed = message_json[2].data.value - console.log("vehiclespeed: " + vehiclespeed) }else if (propertyName === "messages.engine.speed"){ enginespeed = message_json[2].data.value - console.log("enginespeed: " + enginespeed) }else if ( message.indexOf("messages.fuel.level") > 0 ){ fuellevel = message_json[2].data.value }else if (propertyName === "messages.transmission.gearinfo"){ @@ -234,9 +231,11 @@ ApplicationWindow { do_subscribe() } var number = camdev.camranum(); + console.log("tachometer visible is " + visible) camdev.start(number[0], "30", "640*480") }else{ do_subscribe() + console.log("tachometer visible is " + visible) camdev.stop() } } -- cgit 1.2.3-korg