]> git.defcon.no Git - qopencamwidget/blob - view.cpp
Qt Widget that handles webcams :D
[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 widget's
30 // "Take snapshot" button.
31
32 View::View( QWidget *parent)
33 : QMainWindow(parent)
34 {
35
36 // Create the widget, setting the Main window (this) as its parent.
37 QOpenCamWidget *cw = new QOpenCamWidget(this);
38
39 // bool QOpenCamWidget::grabCapture(int source) tries to get a
40 // video capture handle from OpenCV, using the source number
41 // as the device to open. Here I use -1 as the number, meaning
42 // "give me any supported and available video devica you can find".
43 if ( cw->grabCapture(-1) )
44 {
45 // grabCapture() returns true on success. This means that
46 // we have a capture device available, and can start streaming
47 // video from the device to display on the widget.
48
49 // TODO: Document this feature..
50 cw->setSnapshotVisible(true);
51
52 // Video output does not start until QOpenCamWidget::startCapture()
53 // is explicitly called.
54 cw->startCapture();
55 }
56
57 // The QOpenCamWidget does not provide a video stream for further
58 // processing, but it does provide a snapshot of the stream if
59 // the SLOT void QOpenCamWidget::startSnap(void) is triggered.
60 // In this demo app, the slot is connected to the widgets
61 // internal "Take snapshot" trigger button, but the SLOT is
62 // public, and may easily be triggered from "outside".
63 //
64 // When startSnap() is triggered, it will use a SIGNAL to return
65 // its image, if the aquisition was successful. The image
66 // will be available through QOpenCamWidget::imageReady(Qimage);
67 // In this sample, I hook that signal up to a slot on this QMainWindow,
68 // that saves the image, and exits the application.
69 connect( cw, SIGNAL(imageReady(QImage)), this, SLOT(saveImage(QImage)));
70
71 this->setCentralWidget(cw);
72 }
73
74 View::~View(void)
75 {
76 }
77
78 void View::saveImage(QImage image)
79 {
80 // This is the SLOT that recieves QOpenCamWidget::imageReady(QImage) SIGNAL.
81 // To keep the example as simple as possible, I do as little as possible..
82 qDebug() << "Recieved an image of size "
83 << image.width() << "x" << image.height();
84 image.save("snapshot.png");
85
86 QApplication::exit();
87 }