]> git.defcon.no Git - qopencamwidget/blob - view.cpp
Changes to view reflect changes in the QOpenCamWidget.
[qopencamwidget] / view.cpp
1 /*
2 QOpenCamWidget demo application main window (view.c)
3 This file is part of a demonstration application, illustrating
4 how to use the QOpenCamWidget, a Qt 4 widget that displays
5 video input from a webcam, along with a snapshot button.
6
7 Copyright (C) 2009 Jon Langseth
8
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License
11 as published by the Free Software Foundation in its version 2
12 of the License.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 */
23
24 #include "view.h"
25 #include "qopencamwidget.h"
26
27 // The View class is a QMainWindow, used to display our QOpenCamWidget
28 // The constructor of the View takes care of setting up the Widget,
29 // and handles the result of a user clicking (or activating) the
30 // "Take snapshot" button.
31
32 View::View( QWidget *parent)
33 : QMainWindow(parent)
34 {
35
36 QWidget *cw = new QWidget();
37 QVBoxLayout *layout = new QVBoxLayout();
38
39 // This button will be used to trigger a snapshot later on..
40 QPushButton *trigger = new QPushButton(this);
41 trigger->setText("Take picture");
42
43 // Create the widget, setting the Main window (this) as its parent.
44 QOpenCamWidget *cwcam = new QOpenCamWidget(this);
45 // bool QOpenCamWidget::grabCapture(int source) tries to get a
46 // video capture handle from OpenCV, using the source number
47 // as the device to open. Here I use -1 as the number, meaning
48 // "give me any supported and available video devica you can find".
49 if ( cwcam->grabCapture(-1) )
50 {
51 // grabCapture() returns true on success. This means that
52 // we have a capture device available, and can start streaming
53 // video from the device to display on the widget.
54
55 // Video output does not start until QOpenCamWidget::startCapture()
56 // is explicitly called.
57 cwcam->startCapture();
58 }
59
60 // The QOpenCamWidget does not provide a video stream for further
61 // processing, but it does provide a snapshot of the stream if
62 // the SLOT void QOpenCamWidget::startSnap(void) is triggered.
63 // In this demo app, the slot is connected to the widgets
64 // internal "Take snapshot" trigger button, but the SLOT is
65 // public, and may easily be triggered from "outside".
66 //
67 // When startSnap() is triggered, it will use a SIGNAL to return
68 // its image, if the aquisition was successful. The image
69 // will be available through QOpenCamWidget::imageReady(Qimage);
70 // In this sample, I hook that signal up to a slot on this QMainWindow,
71 // that saves the image, and exits the application.
72 connect( cwcam, SIGNAL(imageReady(QImage)), this, SLOT(saveImage(QImage)));
73
74 // To trigger the startSnap(), we hook our previously created
75 // pushbutton to that SLOT.
76 connect( trigger, SIGNAL(clicked()), cwcam, SLOT(startSnap()));
77
78 // Add it all up, and we are ready to run.
79 layout->addWidget(cwcam);
80 layout->addWidget(trigger);
81 cw->setLayout(layout);
82 this->setCentralWidget(cw);
83 }
84
85 View::~View(void)
86 {
87 }
88
89 void View::saveImage(QImage image)
90 {
91 // This is the SLOT that recieves QOpenCamWidget::imageReady(QImage) SIGNAL.
92 // To keep the example as simple as possible, I do as little as possible..
93 qDebug() << "Recieved an image of size "
94 << image.width() << "x" << image.height();
95 image.save("snapshot.png");
96
97 QApplication::exit();
98 }