-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcapturer.cpp
75 lines (66 loc) · 1.67 KB
/
capturer.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
74
75
#include "capturer.h"
#include <opencv2/highgui/highgui.hpp>
#include <QThread>
#include <stdexcept>
Capturer::Capturer(int camId, int frameWidth, int frameHeight) :
camId(camId),
frameWidth(frameWidth),
frameHeight(frameHeight),
shotRequested(false)
{
}
Capturer::~Capturer()
{
}
void Capturer::process()
{
try
{
cv::VideoCapture camera(camId);
if (!camera.isOpened())
throw QString("Can't open camera");
camera.set(CV_CAP_PROP_FRAME_WIDTH, frameWidth);
camera.set(CV_CAP_PROP_FRAME_HEIGHT, frameHeight);
const std::string windowName = "Camera " + QString::number(camId).toStdString();
cv::namedWindow(windowName);
while (!QThread::currentThread()->isInterruptionRequested())
{
cv::Mat frame;
bool read = camera.read(frame);
if (!read)
continue;
cv::imshow(windowName, frame);
cv::waitKey(1);
if (shotRequested)
{
saveFrame(frame);
shotRequested = false;
}
}
cv::destroyWindow(windowName);
camera.release();
}
catch (const QString& errorStr)
{
emit error(errorStr);
}
emit cameraStopped();
emit finished();
}
void Capturer::requestShot(QString pictureName)
{
this->pictureName = pictureName;
shotRequested = true;
}
void Capturer::saveFrame(const cv::Mat& frame)
{
try
{
cv::imwrite(pictureName.toStdString(), frame);
emit shotTaken();
}
catch (std::runtime_error& ex)
{
emit error((QString)"Error while saving picture\n" + ex.what());
}
}