-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcaptureCameraVideo.cpp
73 lines (58 loc) · 1.66 KB
/
captureCameraVideo.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include "captureCameraVideo.h"
capture::capture(QString source1)
{
source = source1;
}
capture::~capture()
{
std::cout << "Capture destroyed" << std::endl;
delete streamThread;
}
void capture::doneThread()
{
// Request an interrupt to escape from while loop
streamThread->requestInterruption();
streamThread->quit(); // Send quit request to thread
streamThread->wait(); // Wait for thread to quit
}
void capture::start()
{
// Start thread
QObject::connect(streamThread, &QThread::started, this, &capture::readFrames);
this->moveToThread(streamThread);
streamThread->start();
}
void capture::setSource(QString source1)
{
source = source1;
}
void capture::readFrames()
{
if(source.isEmpty())
return;
cv::VideoCapture cap;
std::string x = source.toStdString();
if(source.length() == 1)
cap.open(int(x[0])-48);
else
cap.open(x);
if(!cap.isOpened())
std::cerr << "Unable to open source: " << x<<std::endl;
while(true)
{
cap.read(frame);
if(frame.empty())
{ std::cerr << "Unable to read frame "<<std::endl; break;}
// Invoke the signal so that connected classes know frame changed
QImage frameUp = QImage(frame.data,frame.cols,frame.rows,QImage::Format_RGB888).rgbSwapped();
emit frameChange(frameUp);
// If interrupt request come return from the function
// If one does not break the loop the thread will not terminate
if(QThread::currentThread()->isInterruptionRequested())
{
cap.release();
std::cout << "quit1"<<std::endl;
return;
}
}
}