Commit c31681bd authored by Jeff Niu's avatar Jeff Niu

Opening and closing the camera threads

parent 9abe0505
#include "camera.h"
#include <QDebug>
#include <QtGui/QPainter>
#include <QtWidgets/QVBoxLayout>
Capture::Capture(QObject *parent)
: QObject(parent) {}
void Capture::start(int cam) {
if (!m_video_capture) {
if (!m_video_capture || !m_video_capture->isOpened()) {
m_video_capture.reset(new cv::VideoCapture(cam));
}
if (m_video_capture->isOpened()) {
......@@ -18,6 +17,7 @@ void Capture::start(int cam) {
void Capture::stop() {
m_timer.stop();
m_video_capture->release();
}
void Capture::timerEvent(QTimerEvent *ev) {
......@@ -115,6 +115,14 @@ CameraDisplay::CameraDisplay(QWidget *parent, int camera)
m_image_viewer = new ImageViewer(this);
setLayout(m_layout);
m_layout->addWidget(m_image_viewer);
m_converter.setProcessAll(false);
m_capture_thread.start();
m_converter_thread.start();
m_capture.moveToThread(&m_capture_thread);
m_converter.moveToThread(&m_converter_thread);
QObject::connect(&m_capture, &Capture::matReady, &m_converter, &Converter::processFrame);
QObject::connect(&m_converter, &Converter::imageReady, m_image_viewer, &ImageViewer::setImage);
}
CameraDisplay::~CameraDisplay() {
......@@ -132,19 +140,19 @@ int CameraDisplay::getCamera() {
void CameraDisplay::setVisible(bool visible) {
if (visible) {
m_converter.setProcessAll(false);
m_capture_thread.start();
m_converter_thread.start();
m_capture.moveToThread(&m_capture_thread);
m_converter.moveToThread(&m_converter_thread);
QObject::connect(&m_capture, &Capture::matReady, &m_converter, &Converter::processFrame);
QObject::connect(&m_converter, &Converter::imageReady, m_image_viewer, &ImageViewer::setImage);
QMetaObject::invokeMethod(&m_capture, "start");
} else {
pauseVideo();
}
setFixedSize(800, 600);
QDialog::setVisible(visible);
}
void CameraDisplay::reject() {
pauseVideo();
QDialog::reject();
}
void CameraDisplay::pauseVideo() {
QMetaObject::invokeMethod(&m_capture, "stop");
}
......@@ -101,6 +101,8 @@ protected:
void reject() override;
private:
void pauseVideo();
QVBoxLayout *m_layout;
ImageViewer *m_image_viewer;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment