summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorzheng_wenlong <wenlong_zheng@nexty-ele.com>2019-08-29 16:28:15 +0900
committerzheng_wenlong <wenlong_zheng@nexty-ele.com>2019-08-29 16:28:15 +0900
commit31d21fe68392975cedba9e51072e9fbe73d17c0e (patch)
treeb170ea09498672e085e267b2af0d43a313507775
parent5b530ac0e2ec4abe54a2c64274b36b480ed13e25 (diff)
save image by 1 seconds
-rw-r--r--app/camera.cpp57
-rw-r--r--app/camera.h5
-rw-r--r--app/main.qml4
3 files changed, 32 insertions, 34 deletions
diff --git a/app/camera.cpp b/app/camera.cpp
index 8ee99c9..be49b97 100644
--- a/app/camera.cpp
+++ b/app/camera.cpp
@@ -23,6 +23,7 @@
#include <unistd.h>
#include <sys/ioctl.h>
+#define IMAGE_PATH "/home/0/CAMERA/"
using namespace cv;
Camera::Camera() {
@@ -48,7 +49,7 @@ void Camera::paint(QPainter *painter) {
void Camera::enumerateCameras() {
int maxID = 20;
- for (int idx = 2; idx <maxID; idx++){
+ for (int idx = maxID; idx > 2; idx--){
std::stringstream no;
no << "/dev/video" << idx;
qDebug() << idx;
@@ -67,54 +68,47 @@ 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(CAP_PROP_FRAME_WIDTH, res.section("*", 0, 0).toInt());
- capture->set(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){
+ framePsd=fps;
+ frameCnt=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() {
@@ -134,6 +128,11 @@ void Camera::grab() {
image = QImage((const uchar*)(frame.data), frame.cols, frame.rows, frame.step, QImage::Format_RGB888).rgbSwapped();
image = image.scaled(this->width(), this->height(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
update();
+ if(frameCnt==0){
+ QString fileName = QString("%1/%2.jpg").arg(IMAGE_PATH, cvGetTickCount());
+ image.save(fileName);
+ }
+ frameCnt=(frameCnt+1)%framePsd;
}
}
} \ No newline at end of file
diff --git a/app/camera.h b/app/camera.h
index 1b85d27..e698f81 100644
--- a/app/camera.h
+++ b/app/camera.h
@@ -50,7 +50,6 @@ public:
signals:
void isrunningChanged(const bool& isrunning);
- void camraCntChanged(const QVariantList& camcnt);
public slots:
void grab();
@@ -62,9 +61,9 @@ private:
cv::VideoCapture* capture;
QVariantList cam_arr;
-
- int camnumbackup;
int camcnt;
+ int frameCnt;
+ int framePsd;
};
#endif // CAMERA_H
diff --git a/app/main.qml b/app/main.qml
index 0641efe..f0b089f 100644
--- a/app/main.qml
+++ b/app/main.qml
@@ -23,8 +23,8 @@ import QtWebSockets 1.0
ApplicationWindow {
id: root
- width: 1920
- height: 720
+ width: 1080
+ height: 1488
Camera {
id: camdev_device