]> git.defcon.no Git - qopencamwidget/blob - qopencamwidget.cpp
Corrections to documentation
[qopencamwidget] / qopencamwidget.cpp
1 /*
2 This file is one part of two, that together make QOpenCamWidget,
3 a Qt 4 widget that displays video input from a webcam.
4
5 Copyright (C) 2009 Jon Langseth
6
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License
9 as published by the Free Software Foundation in its version 2
10 of the License.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 */
21
22 #include "qopencamwidget.h"
23
24 // ------------------------------------------------------------- //
25 // Constructor and Destructor
26 // ------------------------------------------------------------- //
27
28 /*!
29 * \brief Consctructs a QWidget based widget for displaying video
30 * coming from an OpenCV capture source
31 *
32 * Including webcam data in a Qt application can be problematic,
33 * at least as long as Phonon does not support webcams, and the
34 * Phonon GStreamer backend only supports simple pipelines.
35 *
36 * This class solves the complexity of adding a webcam view,
37 * by using the cross-platform available OpenCV library.
38 *
39 * Limitations, i.e. reasons to read this code and reimplement, are:
40 *
41 * \li saving or streaming video is not really available (unless you do
42 * repeated timer-triggered connections to the startSnap slot).
43 * \li this is a crude and simple implementation, created to solve
44 * the problem of creating a "view-and-shoot" camera app.
45 * If your needs are more complex than that, read the code,
46 * and reimplement.
47 *
48 * Note that even tough the main file here totals some ~250 lines,
49 * only about 90 of those are actual code lines, and and even those
50 * contain a lot of "air"...
51 *
52 * A brief summary of how to use this class:
53 * \code
54 * QOpenCamWidget *cw = new QOpenCamWidget(this);
55 * if ( cw->grabCapture(-1) ) {
56 * cw->startCapture();
57 * }
58 * connect( cw, SIGNAL(imageReady(QImage)), this, SLOT(saveImage(QImage)));
59 *
60 * QPushButton *trigger = new QPushButton(this);
61 * trigger->setText("Take picture");
62 * connect( trigger, SIGNAL(clicked()), cw, SLOT(startSnap()));
63 *
64 * \endcode
65 *
66 * \param *parent The parent widget containing this widget, defaults to NULL.
67 *
68 **/
69 QOpenCamWidget::QOpenCamWidget(QWidget *parent)
70 : QWidget(parent)
71 {
72 // Setting sane default values (i.e. NULL) for
73 // private CvCapture *nextFrame and
74 // private QTimer *frametimer from class definition
75 nextFrame = NULL;
76 frametimer = NULL;
77 }
78
79 QOpenCamWidget::~QOpenCamWidget(void)
80 {
81 cvReleaseCapture( &capture );
82 }
83 // ------------------------------------------------------------- //
84 // Public methods (not signals/slots)
85 // ------------------------------------------------------------- //
86
87 /*!
88 * \brief A paint event is a request to repaint all or part of a widget.
89 *
90 * It can happen for one of the following reasons:
91 *
92 * \li repaint() or update() was invoked,
93 * \li the widget was obscured and has now been uncovered, or
94 * \li many other reasons.
95 *
96 * QOpenCamWidget uses the paintEvent to draw each frame onto the screen.
97 * The paintEvent itself is regularily triggered by explicit update()
98 * calls in QOpenCamWidget::grabFrame().
99 *
100 **/
101 void
102 QOpenCamWidget::paintEvent ( QPaintEvent * event )
103 {
104 QPainter * paint = new QPainter;
105 paint->begin(this);
106
107 if ( nextFrame )
108 {
109 // To make the widget as blazingly fast as possible
110 // we output the last captured frame direclty onto
111 // the widget area.
112 paint->drawImage(event->rect(), *nextFrame);
113 }
114 else
115 {
116 paint->drawText(event->rect(),"No data, check/test camera");
117 }
118 paint->end();
119 // Clean up..
120 delete(paint);
121 }
122
123
124 /*!
125 * \brief Grabs an OpenCV video capture source
126 *
127 * By grabbing a source, it is meant to open the capture source,
128 * and have it ready to start streaming/capturing frames.
129 * Returns true on success, false on error. The grabCapture
130 * is separated from the constructor and/or frame-grabbing, so that you
131 * may do the error-checking you really should do before proceeding.
132 *
133 * \param source The OpenCV capture source enumeration index to open
134 *
135 **/
136 bool
137 QOpenCamWidget::grabCapture(int source)
138 {
139 capture = cvCaptureFromCAM(0);
140 if (!capture)
141 {
142 qDebug() << "QOpenCamWidget::grabCapture(" << source << ") failed";
143 return false;
144 }
145 cvGrabFrame(capture); // Grab a single frame, do resizing based on it.
146 IplImage *image = cvRetrieveFrame(capture);
147 QSize t_size = QSize(image->width,image->height);
148
149 this->setMinimumSize(t_size);
150 this->setMaximumSize(t_size);
151
152 return true;
153 }
154
155 /*!
156 * \brief Starts up grabbing of video frames
157 *
158 * The actual grabbing and displaying of video frames is performed by
159 * a QTimer triggering the SLOT QOpenCamWidget::grabFrame().
160 * startCapture() sets up the timer running this captureFrame loop.
161 *
162 * The SLOT QOpenCamWidget::startSnap() is used to get image frames
163 * out from the widget for other uses, like saving or processing.
164 * This function relies on the timer created and configured by
165 * startCapture(), and as such, this function is the only permitted
166 * way to start the actual capture/streaming of video from the source.
167 *
168 **/
169 void
170 QOpenCamWidget::startCapture(void)
171 {
172 frametimer = new QTimer(this);
173 frametimer->start(50);
174 connect(frametimer,SIGNAL(timeout()), this,SLOT(grabFrame()));
175 }
176
177 /*!
178 * \brief Converts from the OpenCV IplImage data structure to a QImage
179 *
180 * OpenCV uses a data strcuture calles IplImage, optimized for
181 * computer vision image processing tasks. This code was adapted
182 * from kcamwidget.cpp, part of the KDE SVN at
183 * playground/multimedia/kcam/kcamwidget.cpp
184 *
185 * In regard that the IplImage can be forced into a format
186 * that aligns well with a RBG888-format, the conversion
187 * becomes one of the shortes, simples IplImage->QImage I've seen.
188 *
189 * \param *img The IplImage to be converted to a QImage.
190 *
191 **/
192 QImage*
193 QOpenCamWidget::Ipl2QImage(IplImage *img)
194 {
195 cvConvertImage(img,img, CV_CVTIMG_SWAP_RB);
196 QImage * qimage = new QImage(
197 reinterpret_cast<uchar*>(img->imageData),
198 img->width, img->height,
199 3* img->width, QImage::Format_RGB888);
200 return qimage;
201
202 }
203 // ------------------------------------------------------------- //
204 // Public SLOTS
205 // ------------------------------------------------------------- //
206
207 /*!
208 * \brief Grabs a frame and causes an update() when triggered.
209 *
210 * This is the SLOT that actually reads the video source and
211 * causes the widget to display live video. Preferably this
212 * slot will never be called my any other signal that a timeout()
213 * on the frametimer, which is controlled by QOpenCamWidget::startCapture()
214 *
215 **/
216 void QOpenCamWidget::grabFrame(void)
217 {
218 if ( !capture ) { qDebug() << "Capture device not ready!"; return; }
219
220 cvGrabFrame(capture);
221
222 IplImage *iplimage = cvRetrieveFrame(capture);
223 if (iplimage)
224 {
225 nextFrame = Ipl2QImage(iplimage);
226 }
227 else
228 {
229 nextFrame = NULL;
230 }
231
232 update();
233 }
234
235 /*!
236 * \brief Trigger this slot to save a frame from the widget.
237 *
238 * When this slot is triggered, the widgets capture loop is temporarily
239 * stopped, and the last displayed frame is "captured", and made
240 * available through the emitting of the class imageReady SIGNAL.
241 *
242 * It is possible, though I would not recommend, to use repeated
243 * triggering of this slot to do repeated frame-capture, and thus
244 * make a form of "Animation" or "Video" capture.
245 *
246 **/
247 void QOpenCamWidget::startSnap(void)
248 {
249 if ( frametimer ) {
250 if (frametimer->isActive())
251 {
252 frametimer->stop();
253 emit imageReady(QImage(*nextFrame));
254 frametimer->start();
255 }
256 }
257 }
258