This repository has been archived by the owner on Jul 21, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webcamview.cpp
388 lines (315 loc) · 9.67 KB
/
webcamview.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
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
#include "webcamview.h"
WebcamView::WebcamView(QWidget * parent)
: QGraphicsView(parent)
{
QSettings settings(QSettings::NativeFormat, QSettings::UserScope, "JDWhite", "MagniRead");
int device = (settings.contains("webcam/deviceIndex"))
? settings.value("webcam/deviceIndex").toInt()
: DEFAULT_DEVICE;
if (settings.contains("controls/clickToDrag")) {
setClickToDragEnabled( settings.value("controls/clickToDrag").toBool() );
}
if (settings.contains("controls/isLineDrawn")) {
setGuidingLineEnabled( settings.value("controls/isLineDrawn").toBool() );
}
if (settings.contains("controls/linePos")) {
// Change percentage to fraction of position down the screen
setGuidingLinePos( settings.value("controls/linePos").toDouble() / 100 );
}
if (settings.contains("controls/lineThickness")) {
setGuidingLineThickness( settings.value("controls/lineThickness").toInt() );
}
if (settings.contains("controls/lineColor")) {
QColor color = QColor( settings.value("controls/lineColor").toString() );
if (color.isValid()) {
setGuidingLineColor(color);
}
}
init(DEFAULT_MODE, device, parent);
}
WebcamView::WebcamView(int device, QWidget * parent)
: QGraphicsView(parent)
{
init(DEFAULT_MODE, device, parent);
}
/*
* Initialization of WebcamView
*/
void WebcamView::init(WebcamView::Mode mode, int device, QWidget * parent) {
this->mode = mode;
// Change viewport functionality & appearance
setCursor(Qt::OpenHandCursor);
setHorizontalScrollBarPolicy( Qt::ScrollBarAlwaysOff );
setVerticalScrollBarPolicy( Qt::ScrollBarAlwaysOff );
setAlignment(Qt::AlignTop | Qt::AlignLeft);
// To zoom from center of the image
setTransformationAnchor(QGraphicsView::AnchorViewCenter);
// To fully update everything that is drawn in paint event
setViewportUpdateMode(QGraphicsView::FullViewportUpdate);
// To always track mouse, not just when clicking
this->setMouseTracking(true);
// Attach scene
scene = new QGraphicsScene(parent);
setScene(scene);
// Setup video capture and load video
videoPlayer = new WebcamPlayer(this);
openWebcam(device);
connect(videoPlayer, SIGNAL (imageRead(QImage)),
this, SLOT (setSnapshotImage(QImage)));
connect(videoPlayer, SIGNAL (imageProcessed(QImage)),
this, SLOT (updateImage(QImage)));
connect(videoPlayer, SIGNAL (readError()),
this, SLOT (handleError()));
// Initial display
if (mode == SNAPSHOT) {
// TODO: Instead of sample image, "choose webcam" image shown on screen
updateImage(QImage(":/media/sampleImage.jpg"));
}
else if (mode == PREVIEW) {
videoPlayer->play();
}
else if (mode == ERROR ) {
// TODO: "Webcam not found" image shown on screen
handleError();
}
// Prepare view for display
this->show();
}
/*
* Rescale image so that it keeps the aspect ratio, fills the entire viewport, and scrolls properly
*/
void WebcamView::updateImage(QImage img) {
// Replace old image
image = img;
// Rescale new image so that it at least fills the viewport
QPixmap pixmap = QPixmap::fromImage(img).scaled(
static_cast<int>(this->size().width()),
static_cast<int>(this->size().height()),
Qt::KeepAspectRatioByExpanding, Qt::FastTransformation );
// Create painter for pixmap (unused, but guiding line jitters less when paintEvent called)
QPainter painter(&pixmap);
imageItem.setPixmap(pixmap);
if (scene->items().count() == 0) {
scene->addItem(&imageItem);
}
// Prepare rescaled view for display
this->show();
}
void WebcamView::setSnapshotImage(const QImage & img) {
snapshotImage = img;
}
/*
* Manually process snapshot image and update viewport to show processed image
*/
void WebcamView::processSnapshotImage() {
QImage processedImage;
if (!snapshotImage.isNull()) {
cv::Mat cvImage = videoPlayer->convertQImageToMat(snapshotImage);
cvImage = videoPlayer->processImage(cvImage);
processedImage = videoPlayer->convertMatToQImage(cvImage);
updateImage(processedImage);
}
}
/*
* Resize current image to fit the screen
*/
void WebcamView::resize() {
if (!image.isNull()) {
updateImage(image);
}
}
/*
* Change what is being viewed
*/
void WebcamView::setMode(WebcamView::Mode mode) {
this->mode = mode;
if (mode == PREVIEW) {
videoPlayer->play();
}
else if (mode == SNAPSHOT) {
videoPlayer->stop();
}
emit modeChanged();
}
WebcamView::Mode WebcamView::getMode() {
return mode;
}
void WebcamView::setBrightness(double brightness) {
videoPlayer->setBrightness(brightness);
}
double WebcamView::getBrightness() {
return videoPlayer->getBrightness();
}
void WebcamView::setContrast(double contrast) {
videoPlayer->setContrast(contrast);
}
double WebcamView::getContrast() {
return videoPlayer->getContrast();
}
void WebcamView::setFilter(std::string filter) {
videoPlayer->setFilter(filter);
}
std::string WebcamView::getFilter() {
return videoPlayer->getFilter();
}
int WebcamView::getWebcam() {
return videoPlayer->getWebcam();
}
void WebcamView::setRotation(int angle) {
videoPlayer->setRotation(angle);
}
int WebcamView::getRotation() {
return videoPlayer->getRotation();
}
/*
* Change whether they want click to start and stop dragging, or hold to drag and drop
*/
void WebcamView::setClickToDragEnabled(bool isClickToDrag) {
this->isClickToDrag = isClickToDrag;
if (isClickToDrag) {
// Use custom drag functions (click to start dragging, move mouse to move image, click to stop dragging)
setDragMode(QGraphicsView::NoDrag);
}
else {
// Hold mouse button down to drag and release to drop instead
setDragMode(QGraphicsView::ScrollHandDrag);
}
}
bool WebcamView::isClickToDragEnabled() {
return isClickToDrag;
}
/*
* If currently dragging using clickToDrag
*/
void WebcamView::setDragging(bool isDragging) {
this->dragging = isDragging;
}
bool WebcamView::isDragging() {
return dragging;
}
void WebcamView::setGuidingLineEnabled(bool guidingLineEnabled) {
this->guidingLineEnabled = guidingLineEnabled;
}
void WebcamView::setGuidingLineColor(QColor color) {
this->guidingLineColor = color;
}
/*
* Set position of guiding line as percentage of height (0% is bottom, 100% is top)
*/
void WebcamView::setGuidingLinePos(double percent) {
if (percent < 0) {
percent = 0;
}
else if (percent > 1) {
percent = 1;
}
this->guidingLinePos = 1-percent;
}
void WebcamView::setGuidingLineThickness(int px) {
if (px < 1) {
px = 1;
}
this->guidingLineThickness = px;
}
bool WebcamView::isGuidingLineEnabled() {
return guidingLineEnabled;
}
/*
* Changes to error mode and stops video player if playing
*/
void WebcamView::handleError() {
setMode(ERROR);
videoPlayer->stop();
}
/*
* Left click to start dragging just by moving the mouse. Left click again to stop
*/
void WebcamView::mousePressEvent(QMouseEvent * event) {
QGraphicsView::mousePressEvent(event);
if (!isClickToDrag) {
return;
}
if (event->button() == Qt::LeftButton) {
mouseOriginX = event->x();
mouseOriginY = event->y();
// Turn off dragging to prevent resetting cursor
dragging = !dragging;
if (isDragging()) {
setCursor(Qt::ClosedHandCursor);
}
else {
setCursor(Qt::OpenHandCursor);
}
}
}
/*
* Move image with the mouse if dragging
*/
void WebcamView::mouseMoveEvent(QMouseEvent * event) {
QGraphicsView::mouseMoveEvent(event);
if (!isClickToDrag) {
return;
}
// Move image to center on where the mouse is on the image
if (isDragging()) {
QPointF oldPt = mapToScene(mouseOriginX, mouseOriginY);
QPointF newPt = mapToScene(event->pos());
QPointF delta = newPt - oldPt;
// Translate relative to mouse
ViewportAnchor anchor = transformationAnchor();
setTransformationAnchor(QGraphicsView::NoAnchor);
translate(delta.x(), delta.y());
// Reset transformation anchor
setTransformationAnchor(anchor);
mouseOriginX = event->x();
mouseOriginY = event->y();
}
}
/*
* Stop dragging image if mouse leaves view
*/
void WebcamView::leaveEvent(QEvent * event) {
QGraphicsView::leaveEvent(event);
if (isClickToDrag) {
setDragging(false);
setCursor(Qt::OpenHandCursor);
}
}
/*
* Draw dotted line across viewport to guide reading
*/
void WebcamView::paintEvent(QPaintEvent * event) {
QGraphicsView::paintEvent(event);
if (guidingLineEnabled) {
QPainter painter(viewport());
QPen pen(guidingLineColor, guidingLineThickness, Qt::SolidLine);
painter.setPen(pen);
painter.setRenderHint(QPainter::Antialiasing);
painter.drawLine(QPointF(0, viewport()->height() * guidingLinePos ), QPointF(viewport()->width(), viewport()->height() * guidingLinePos) );
}
}
/*
* Change the webcam to the index of the device specified
*/
bool WebcamView::openWebcam(int device) {
bool isOpened = videoPlayer->open(device);
if (!isOpened) {
handleError();
}
else {
switch (mode) {
case PREVIEW:
videoPlayer->play();
break;
case SNAPSHOT:
break;
case ERROR:
default:
handleError();
break;
}
}
return isOpened;
}
WebcamView::~WebcamView() {
}