aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorzheng_wenlong <wenlong_zheng@nexty-ele.com>2018-12-25 16:54:55 +0900
committerzheng_wenlong <wenlong_zheng@nexty-ele.com>2018-12-25 16:54:55 +0900
commitbdecd54715fe22d7f2ceef70a749588ea7c725f2 (patch)
tree55ab1f4bac2926f96339985d96eb59959e5c94a6
parentdec39c27fec4a697e55e3e9a7abf95fa8708db43 (diff)
-rw-r--r--app/camera.cpp49
-rw-r--r--app/camera.h9
-rw-r--r--app/main.qml5
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 <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() {
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 <QTimer>
#include <QImage>
#include <QQuickPaintedItem>
+#include <opencv2/videoio.hpp>
#include <iostream>
-#include <opencv2/core/core.hpp>
-#include <opencv2/highgui/highgui.hpp>
+#include <opencv2/core.hpp>
+#include <opencv2/imgcodecs.hpp>
+#include <opencv2/highgui.hpp>
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()
}
}