]>
git.defcon.no Git - qopencamwidget/blob - qopencamwidget.cpp
2 This file is one part of two, that together make QOpenCamWidget,
3 a Qt 4 widget that displays video input from a webcam.
5 Copyright (C) 2009 Jon Langseth
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
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.
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.
22 #include "qopencamwidget.h"
24 // ------------------------------------------------------------- //
25 // Constructor and Destructor
26 // ------------------------------------------------------------- //
29 * \brief Consctructs a QWidget based widget for displaying video
30 * coming from an OpenCV capture source
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.
36 * This class solves the complexity of adding a webcam view,
37 * by using the cross-platform available OpenCV library.
39 * Limitations, i.e. reasons to read this code and reimplement,
40 * are: saving or streaming video is not really available (unless
41 * you do repeated timer-triggered connections to the startSnap slot),
42 * the widget size is identical to the video source dimensions (it
43 * resizes the wodget using setMinimuSize to the video dimensions,
44 * and does not handle resizing to sizes above this dimension.
46 * A brief summary of how to use this class:
48 * QOpenCamWidget *cw = new QOpenCamWidget(this);
49 * if ( cw->grabCapture(-1) ) {
52 * connect( cw, SIGNAL(imageReady(QImage)), this, SLOT(saveImage(QImage)));
54 * QPushButton *trigger = new QPushButton(this);
55 * trigger->setText("Take picture");
56 * connect( trigger, SIGNAL(clicked()), cw, SLOT(startSnap()));
60 * \param *parent The parent widget containing this widget, defaults to NULL.
63 QOpenCamWidget::QOpenCamWidget(QWidget
*parent
)
66 // Setting sane default values (i.e. NULL) for
67 // private CvCapture *nextFrame and
68 // private QTimer *frametimer from class definition
73 QOpenCamWidget::~QOpenCamWidget(void)
75 cvReleaseCapture( &capture
);
77 // ------------------------------------------------------------- //
78 // Public methods (not signals/slots)
79 // ------------------------------------------------------------- //
82 * \brief A paint event is a request to repaint all or part of a widget.
84 * It can happen for one of the following reasons:
86 * \li repaint() or update() was invoked,
87 * \li the widget was obscured and has now been uncovered, or
88 * \li many other reasons.
90 * QOpenCamWidget uses the paintEvent to draw each frame onto the screen.
91 * The paintEvent itself is regularily triggered by explicit update()
92 * calls in QOpenCamWidget::grabFrame().
96 QOpenCamWidget::paintEvent ( QPaintEvent
* event
)
98 QPainter
* paint
= new QPainter
;
103 // To make the widget as blazingly fast as possible
104 // we output the last captured frame direclty onto
106 paint
->drawImage(event
->rect(), *nextFrame
);
110 paint
->drawText(event
->rect(),"No data, check/test camera");
119 * \brief Grabs an OpenCV video capture source
121 * By grabbing a source, it is meant to open the capture source,
122 * and have it ready to start streaming/capturing frames.
123 * Returns true on success, false on error. The grabCapture
124 * is separated from the constructor and/or frame-grabbing, so that you
125 * may do the error-checking you really should do before proceeding.
127 * \param source The OpenCV capture source enumeration index to open
131 QOpenCamWidget::grabCapture(int source
)
133 capture
= cvCaptureFromCAM(0);
136 qDebug() << "QOpenCamWidget::grabCapture(" << source
<< ") failed";
139 cvGrabFrame(capture
); // Grab a single frame, do resizing based on it.
140 IplImage
*image
= cvRetrieveFrame(capture
);
141 QSize t_size
= QSize(image
->width
,image
->height
);
143 this->setMinimumSize(t_size
);
144 this->setMaximumSize(t_size
);
150 * \brief Starts up grabbing of video frames
152 * The actual grabbing and displaying of video frames is performed by
153 * a QTimer triggering the SLOT QOpenCamWidget::grabFrame().
154 * startCapture() sets up the timer running this captureFrame loop.
156 * The SLOT QOpenCamWidget::startSnap() is used to get image frames
157 * out from the widget for other uses, like saving or processing.
158 * This function relies on the timer created and configured by
159 * startCapture(), and as such, this function is the only permitted
160 * way to start the actual capture/streaming of video from the source.
164 QOpenCamWidget::startCapture(void)
166 frametimer
= new QTimer(this);
167 frametimer
->start(50);
168 connect(frametimer
,SIGNAL(timeout()), this,SLOT(grabFrame()));
172 * \brief Converts from the OpenCV IplImage data structure to a QImage
174 * OpenCV uses a data strcuture calles IplImage, optimized for
175 * computer vision image processing tasks. This code was adapted
176 * from kcamwidget.cpp, part of the KDE SVN at
177 * playground/multimedia/kcam/kcamwidget.cpp
179 * In regard that the IplImage can be forced into a format
180 * that aligns well with a RBG888-format, the conversion
181 * becomes one of the shortes, simples IplImage->QImage I've seen.
183 * \param *img The IplImage to be converted to a QImage.
187 QOpenCamWidget::Ipl2QImage(IplImage
*img
)
189 cvConvertImage(img
,img
, CV_CVTIMG_SWAP_RB
);
190 QImage
* qimage
= new QImage(
191 reinterpret_cast<uchar
*>(img
->imageData
),
192 img
->width
, img
->height
,
193 3* img
->width
, QImage::Format_RGB888
);
197 // ------------------------------------------------------------- //
199 // ------------------------------------------------------------- //
202 * \brief Grabs a frame and causes an update() when triggered.
204 * This is the SLOT that actually reads the video source and
205 * causes the widget to display live video. Preferably this
206 * slot will never be called my any other signal that a timeout()
207 * on the frametimer, which is controlled by QOpenCamWidget::startCapture()
210 void QOpenCamWidget::grabFrame(void)
212 if ( !capture
) { qDebug() << "Capture device not ready!"; return; }
214 cvGrabFrame(capture
);
216 IplImage
*iplimage
= cvRetrieveFrame(capture
);
219 nextFrame
= Ipl2QImage(iplimage
);
230 * \brief Trigger this slot to save a frame from the widget.
232 * When this slot is triggered, the widgets capture loop is temporarily
233 * stopped, and the last displayed frame is "captured", and made
234 * available through the emitting of the class imageReady SIGNAL.
236 * It is possible, though I would not recommend, to use repeated
237 * triggering of this slot to do repeated frame-capture, and thus
238 * make a form of "Animation" or "Video" capture.
241 void QOpenCamWidget::startSnap(void)
244 if (frametimer
->isActive())
247 emit
imageReady(QImage(*nextFrame
));