-
Notifications
You must be signed in to change notification settings - Fork 16
/
cameragrabber.cpp
174 lines (140 loc) · 4.67 KB
/
cameragrabber.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
/****************************************************************************
**
** This library is free software; you can redistribute it and/or
** modify it under the terms of the GNU Lesser General Public
** License as published by the Free Software Foundation; either
** version 2.1 of the License, or (at your option) any later version.
**
** This library is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
** Lesser General Public License for more details.
**
** You should have received a copy of the GNU Lesser General Public
** License along with this library; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
**
****************************************************************************/
#include "cameragrabber.h"
#include <QEventLoop>
#include <QTimer>
#include <QElapsedTimer>
#include <QDateTime>
#include <QPainter>
#include <QCamera>
#include <opencv/cv.h>
#include <opencv/highgui.h>
CameraGrabber::CameraGrabber(QObject *parent)
: AbstractImageGrabber(parent)
, m_deviceIndex(-1)
{
setInitializationTime(1000);
connect(this, SIGNAL(stateChanged(AbstractGrabber::State)), this, SLOT(onStateChanged(AbstractGrabber::State)));
}
CameraGrabber::~CameraGrabber()
{
if (this->state() == AbstractGrabber::ActiveState)
this->releaseCamera();
}
void CameraGrabber::setDeviceIndex(int index)
{
if (m_deviceIndex != index) {
m_deviceIndex = index;
}
}
int CameraGrabber::deviceIndex() const
{
return m_deviceIndex;
}
void CameraGrabber::setSize(const QSize &size)
{
if (m_size != size) {
m_size = size;
}
}
QSize CameraGrabber::size() const
{
return m_size;
}
QStringList CameraGrabber::availableDeviceNames()
{
QByteArray device;
QList<QByteArray> devices = QCamera::availableDevices();
QStringList names;
Q_FOREACH (device, devices) {
names.append(QCamera::deviceDescription(device));
}
return names;
}
QSize CameraGrabber::maximumFrameSize(int deviceIndex)
{
if (deviceIndex < 0 || deviceIndex > CameraGrabber::availableDeviceNames().count())
return QSize();
CvCapture *capture = cvCreateCameraCapture(deviceIndex);
QSize size;
size.setWidth(cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH));
size.setHeight(cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT));
cvReleaseCapture(&capture);
return size;
}
bool CameraGrabber::start()
{
if (!createCamera())
return false;
return AbstractImageGrabber::start();
}
void CameraGrabber::onStateChanged(AbstractGrabber::State state)
{
if (state == AbstractGrabber::StoppedState) {
releaseCamera();
}
}
bool CameraGrabber::createCamera()
{
if (m_deviceIndex < 0 || m_deviceIndex > CameraGrabber::availableDeviceNames().count()) {
setError(AbstractGrabber::DeviceNotFoundError, tr("Device to be grabbed was not found."));
return false;
}
m_camera = cvCreateCameraCapture(m_deviceIndex);
//init size
if (!size().isValid()) {
QSize size;
size.setWidth(cvGetCaptureProperty(m_camera, CV_CAP_PROP_FRAME_WIDTH));
size.setHeight(cvGetCaptureProperty(m_camera, CV_CAP_PROP_FRAME_HEIGHT));
setSize(size);
}
return true;
}
void CameraGrabber::releaseCamera()
{
cvReleaseCapture(&m_camera);
m_camera = 0;
}
QImage CameraGrabber::captureFrame()
{
IplImage *iplImage = cvQueryFrame(m_camera);
if (!iplImage)
return QImage();
int height = iplImage->height;
int width = iplImage->width;
QImage frame;
if (iplImage->depth == IPL_DEPTH_8U && iplImage->nChannels == 3) {
//cvCvtColor( iplImage, iplImage, CV_BGR2RGB);
uchar *data = (uchar*)iplImage->imageData;
frame = QImage(data, width, height, QImage::Format_RGB888).rgbSwapped(); // memory leak
//resize the frame to a given size
if (frame.size() != size()) {
QImage scaledFrame = frame.scaled(size(), Qt::KeepAspectRatio);
if (scaledFrame.size() != size()) {
QImage newFrame(size(), QImage::Format_RGB888);
newFrame.fill(Qt::black);
QPainter painter(&newFrame);
painter.drawImage(scaledFrame.width() < size().width() ? (newFrame.width() - scaledFrame.width()) / 2 : 0,
scaledFrame.height() < size().height() ? (newFrame.height() - scaledFrame.height()) / 2 : 0, scaledFrame);
scaledFrame = newFrame;
}
frame = scaledFrame;
}
}
return frame;
}