-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWebcamWindow.cpp
More file actions
187 lines (159 loc) · 5.97 KB
/
WebcamWindow.cpp
File metadata and controls
187 lines (159 loc) · 5.97 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
* Copyright (c) 2016, Roman Meyta <theshrodingerscat@gmail.com>
* All rights reserved
*/
#include <QLabel>
#include <QImage>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QPushButton>
#include <QComboBox>
#include <QGroupBox>
#include <QSplitter>
#include <QString>
#include <QSizePolicy>
#include "WebcamWindow.h"
#include "VideoDevice.h"
#include "VideoCapture.h"
#include "ImageFormats.h"
WebcamWindow::WebcamWindow(QWidget *parent):
QMainWindow(parent),
m_viewport(new QLabel),
m_frameMutex(),
m_frame(),
m_controlLayout(new QVBoxLayout),
m_controlGroup(new QGroupBox),
m_windowLayout(new QHBoxLayout),
m_windowGroup(new QGroupBox),
m_startButton(new QPushButton("Turn On")),
m_stopButton(new QPushButton("Turn Off")),
m_devicesLabel(new QLabel("Devices")),
m_devices(new QComboBox),
m_resolutionsLabel(new QLabel("Resolutions")),
m_resolutions(new QComboBox),
m_devicesGroup(new QGroupBox),
m_devicesLayout(new QVBoxLayout),
m_vsplitter(new QSplitter),
m_flipButton(new QPushButton("Flip frame")),
m_videoCapture(nullptr),
m_isCapturing(false),
m_isFlipped(false) {
setWindowTitle("QtWebcam");
setWindowFlags(this->windowFlags() | Qt::MSWindowsFixedSizeDialogHint);
m_devicesLayout->addWidget(m_devicesLabel);
m_devicesLayout->addWidget(m_devices);
m_devicesLayout->addWidget(m_resolutionsLabel);
m_devicesLayout->addWidget(m_resolutions);
m_devicesGroup->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
m_devicesGroup->setLayout(m_devicesLayout);
m_controlLayout->addWidget(m_devicesGroup);
m_controlLayout->addWidget(m_vsplitter);
m_controlLayout->addWidget(m_startButton);
m_controlLayout->addWidget(m_stopButton);
m_controlLayout->addWidget(m_flipButton);
m_controlGroup->setLayout(m_controlLayout);
m_controlGroup->setMinimumWidth(200);
m_controlGroup->setMaximumWidth(200);
m_stopButton->setEnabled(false);
m_flipButton->setEnabled(false);
m_viewport->setMinimumSize(320, 240);
m_windowLayout->addWidget(m_viewport);
m_windowLayout->addWidget(m_controlGroup);
m_windowLayout->setSizeConstraint(QLayout::SetFixedSize);
m_windowGroup->setLayout(m_windowLayout);
setCentralWidget(m_windowGroup);
m_videoCapture = new VideoCapture([this](unsigned char* data, int len, VideoDevice* device){
processFrame(data, len, device);
});
auto devicesNames = m_videoCapture->getDevicesNames();
for (auto& deviceName: devicesNames) {
QString name = QString::fromWCharArray(deviceName.c_str());
m_devices->addItem(name);
}
auto deviceResolutions = m_videoCapture->getActiveDeviceResolutions();
for (auto& deviceResolution: deviceResolutions) {
QString resolution = QString::fromStdString(deviceResolution);
m_resolutions->addItem(resolution);
}
connect(m_resolutions, static_cast<void(QComboBox::*)(int)>(&QComboBox::currentIndexChanged), this, static_cast<void(WebcamWindow::*)(int)>(&WebcamWindow::changeResolution));
connect(m_devices, static_cast<void(QComboBox::*)(int)>(&QComboBox::currentIndexChanged), this, static_cast<void(WebcamWindow::*)(int)>(&WebcamWindow::changeDevice));
connect(m_startButton, &QPushButton::released, this, &WebcamWindow::startCapture);
connect(m_stopButton, &QPushButton::released, this, &WebcamWindow::stopCapture);
connect(m_flipButton, &QPushButton::released, this, &WebcamWindow::flipFrame);
}
WebcamWindow::~WebcamWindow() {
m_devices->blockSignals(true);
m_resolutions->blockSignals(true);
m_videoCapture->stopCapture();
delete m_videoCapture;
}
void WebcamWindow::processFrame(const unsigned char* data, int len, VideoDevice* device) {
if (!device || data == nullptr || len <= 0) {
return;
}
QImageMaker makeQImage;
for (auto& formatRow: ImageFormatTable) {
if (formatRow.directshowFormat == device->getCurrentProperties().pixelFormat) {
makeQImage = formatRow.makeQImage;
break;
}
}
QImage newFrame(makeQImage(data, len, device->getCurrentProperties().width,
device->getCurrentProperties().height));
m_frameMutex.lock();
m_frame = newFrame.mirrored(device->getCurrentProperties().isFlippedHorizontal,
device->getCurrentProperties().isFlippedVertical || m_isFlipped);
m_frameMutex.unlock();
QMetaObject::invokeMethod(this, "presentFrame", Qt::QueuedConnection);
}
void WebcamWindow::presentFrame() {
m_frameMutex.lock();
m_viewport->setPixmap(QPixmap::fromImage(m_frame));
m_frameMutex.unlock();
adjustSize();
m_viewport->repaint();
}
void WebcamWindow::changeResolution(int resolutionNum) {
bool wasCapturing = m_isCapturing;
stopCapture();
m_videoCapture->changeActiveDeviceResolution(resolutionNum);
adjustSize();
if (wasCapturing) {
startCapture();
}
}
void WebcamWindow::changeDevice(int deviceNum) {
bool wasCapturing = m_isCapturing;
stopCapture();
m_videoCapture->changeActiveDevice(deviceNum);
m_resolutions->clear();
auto deviceResolutions = m_videoCapture->getActiveDeviceResolutions();
for (auto& deviceResolution : deviceResolutions) {
QString resolution = QString::fromStdString(deviceResolution);
m_resolutions->addItem(resolution);
}
if (wasCapturing) {
startCapture();
}
}
void WebcamWindow::flipFrame() {
m_isFlipped = !m_isFlipped;
}
void WebcamWindow::startCapture() {
m_startButton->setEnabled(false);
m_stopButton->setEnabled(true);
m_flipButton->setEnabled(true);
if (m_videoCapture->startCapture()) {
m_isCapturing = true;
}
}
void WebcamWindow::stopCapture() {
m_startButton->setEnabled(true);
m_stopButton->setEnabled(false);
m_flipButton->setEnabled(false);
if (m_videoCapture->stopCapture()) {
m_isCapturing = false;
}
m_frame.fill(Qt::GlobalColor::white);
presentFrame();
}