-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcamerawidget.cpp
More file actions
300 lines (253 loc) · 8.79 KB
/
camerawidget.cpp
File metadata and controls
300 lines (253 loc) · 8.79 KB
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#include "camerawidget.h"
#include <QtGui/QImage>
#include <QtGui/QPainter>
#include <QtGui/QHBoxLayout>
#include <QtGui/QPaintEvent>
#include <QtGui/QMessageBox>
#include <QtGui/QApplication>
#include <QtGui/QStackedWidget>
#include <QtGui/QDesktopServices>
#include <QtCore/QString>
#include <QtCore/QTimer>
#include <QtCore/QDebug>
#include <QtCore/QDir>
#include "button.h"
#include "cameravideosurface.h"
CameraWidget::CameraWidget(QWidget *parent)
: QMainWindow(parent)
{
setWindowTitle("QCameraDecoder");
setAutoFillBackground(false);
QPalette palette = this->palette();
palette.setColor(QPalette::Background, Qt::black);
setPalette(palette);
QWidget* mainWidget = new QWidget(this);
mainWidget->setPalette(palette);
QHBoxLayout* hboxl = new QHBoxLayout;
hboxl->setSpacing(0);
hboxl->setMargin(0);
m_stackedWidget = new QStackedWidget();
m_stackedWidget->setPalette(palette);
m_videoWidget = new QWidget();
m_videoWidget->setPalette(palette);
m_stackedWidget->addWidget(m_videoWidget);
QWidget* secondWidget = new QWidget(this);
secondWidget->setPalette(palette);
m_stackedWidget->addWidget(secondWidget);
m_stackedWidget->setCurrentIndex(0);
hboxl->addWidget(m_stackedWidget);
QSize iconSize(80, 80);
QVBoxLayout* vboxl = new QVBoxLayout;
vboxl->setSpacing(0);
vboxl->setMargin(0);
m_exitBtn = new Button(this);
QObject::connect(m_exitBtn, SIGNAL(pressed()),qApp , SLOT(quit()));
QPixmap p = QPixmap(":/icons/exit.png");
m_exitBtn->setPixmap(p.scaled(iconSize, Qt::KeepAspectRatio, Qt::SmoothTransformation));
vboxl->addWidget(m_exitBtn);
vboxl->setAlignment(m_exitBtn, Qt::AlignHCenter | Qt::AlignTop);
m_cameraBtn = new Button(this);
QObject::connect(m_cameraBtn, SIGNAL(pressed()), this, SLOT(searchAndLock()));
p = QPixmap(":/icons/camera.png");
m_cameraBtn->setPixmap(p.scaled(iconSize, Qt::KeepAspectRatio, Qt::SmoothTransformation));
vboxl->addWidget(m_cameraBtn);
vboxl->setAlignment(m_cameraBtn, Qt::AlignCenter);
hboxl->addLayout(vboxl);
mainWidget->setLayout(hboxl);
setCentralWidget(mainWidget);
m_videoSurface = 0;
m_focusing = false;
m_pictureCaptured = false;
m_showViewFinder = false;
QTimer::singleShot(1000,this,SLOT(enableCamera()));
}
CameraWidget::~CameraWidget()
{
delete m_videoWidget;
delete m_stackedWidget;
delete m_exitBtn;
delete m_cameraBtn;
}
void CameraWidget::searchAndLock()
{
m_focusing = false;
if (m_pictureCaptured) {
// Starting view finder again
m_pictureCaptured = false;
m_stackedWidget->setCurrentIndex(0);
if (m_videoSurface) {
m_showViewFinder = true;
}
} else {
// Search and lock picture (=focus)
if (m_camera->supportedLocks() & QCamera::LockFocus) {
m_focusing = true;
m_camera->searchAndLock(QCamera::LockFocus);
} else {
// No focus functionality, take picture right away
captureImage();
}
}
}
void CameraWidget::enableCamera()
{
m_camera = new QCamera();
m_camera->setCaptureMode(QCamera::CaptureStillImage);
connect(m_camera, SIGNAL(error(QCamera::Error)),
this, SLOT(error(QCamera::Error)));
connect(m_camera, SIGNAL(lockStatusChanged(QCamera::LockStatus,QCamera::LockChangeReason)),
this, SLOT(lockStatusChanged(QCamera::LockStatus,QCamera::LockChangeReason)));
connect(m_camera, SIGNAL(lockFailed()),
this, SLOT(lockFailed()));
connect(m_camera, SIGNAL(locked()), this, SLOT(locked()));
QMediaService *ms = m_camera->service();
QVideoRendererControl *vrc = ms->requestControl<QVideoRendererControl*>();
m_videoSurface = new CameraVideoSurface(this,this,this);
vrc->setSurface(m_videoSurface);
m_cameraFocus = m_camera->focus();
setCameraFocus();
m_cameraFocus->setFocusMode(m_focusModel);
m_stillImageCapture = new QCameraImageCapture(m_camera);
connect(m_stillImageCapture, SIGNAL(imageCaptured(int, QImage)), this, SLOT(imageCaptured(int, QImage)));
if (m_camera->state() == QCamera::ActiveState) {
m_camera->stop();
}
m_videoWidget->show();
m_camera->start();
m_showViewFinder = true;
}
void CameraWidget::setCameraFocus ()
{
if (!m_cameraFocus)
return;
if (m_cameraFocus->isFocusModeSupported(QCameraFocus::MacroFocus)) {
m_focusModel = QCameraFocus::MacroFocus;
return;
}
if (m_cameraFocus->isFocusModeSupported(QCameraFocus::HyperfocalFocus)) {
m_focusModel = QCameraFocus::HyperfocalFocus;
return;
}
if (m_cameraFocus->isFocusModeSupported(QCameraFocus::InfinityFocus)) {
m_focusModel = QCameraFocus::InfinityFocus;
return;
}
if (m_cameraFocus->isFocusModeSupported(QCameraFocus::ContinuousFocus)) {
m_focusModel = QCameraFocus::ContinuousFocus;
return;
}
if (m_cameraFocus->isFocusModeSupported(QCameraFocus::AutoFocus)) {
m_focusModel = QCameraFocus::AutoFocus;
return;
} else {
m_focusModel = QCameraFocus::ManualFocus;
return;
}
}
void CameraWidget::error(QCamera::Error e)
{
switch (e) {
case QCamera::NoError:
break;
case QCamera::CameraError:
QMessageBox::warning(this, "QCameraExample", "An error has occurred");
break;
case QCamera::InvalidRequestError:
QMessageBox::warning(this, "QCameraExample", "System resource doesn't support requested functionality.");
break;
case QCamera::ServiceMissingError:
QMessageBox::warning(this, "QCameraExample", "No camera service available.");
break;
case QCamera::NotSupportedFeatureError :
QMessageBox::warning(this, "QCameraExample", "The feature is not supported.");
break;
};
}
void CameraWidget::lockFailed()
{
qDebug() << "[error]: lockFailed!";
}
void CameraWidget::lockStatusChanged(QCamera::LockStatus status,
QCamera::LockChangeReason reason)
{
if (status == QCamera::Locked) {
if (reason == QCamera::LockAcquired) {
m_focusing = false;
captureImage();
m_camera->unlock();
}
} else if (status == QCamera::Unlocked && m_focusing) {
}
}
void CameraWidget::locked()
{
qDebug() << "[information] : locked";
}
void CameraWidget::updateVideo()
{
if (m_showViewFinder)
repaint();
}
void CameraWidget::captureImage()
{
if (m_pictureCaptured) {
m_pictureCaptured = false;
m_stackedWidget->setCurrentIndex(0);
m_showViewFinder = true;
} else {
QString path(QDesktopServices::storageLocation(QDesktopServices::PicturesLocation));
QDir dir(path);
// Get next filename
QStringList files = dir.entryList(QStringList() << "camera_*.jpg");
int lastImage = 0;
Q_FOREACH (const QString &fileName, files) {
int imgNumber = fileName.mid(7, fileName.size() - 11).toInt();
lastImage = qMax(lastImage, imgNumber);
}
// Capture image
if (m_stillImageCapture->isReadyForCapture()) {
m_capturedImageName = QString("camera_%1.jpg").arg(lastImage + 1);
m_stillImageCapture->capture(m_capturedImageName);
}
}
}
void CameraWidget::imageCaptured(int id, const QImage &preview)
{
m_showViewFinder = false;
m_focusing = false;
// Image captured, show it to the user
m_stackedWidget->setCurrentIndex(1);
// Get picture location
QString path(QDesktopServices::storageLocation(QDesktopServices::PicturesLocation));
m_capturedImageName.prepend(path + "/");
m_capturedImage = preview;
// Set suitable size to the image
QSize s = m_videoWidget->size();
s = s - QSize(20, 20);
m_capturedImage = m_capturedImage.scaled(s, Qt::KeepAspectRatio, Qt::SmoothTransformation);
m_pictureCaptured = true;
update();
}
void CameraWidget::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
QRect r = this->rect();
QFont font = painter.font();
font.setPixelSize(20);
painter.setFont(font);
painter.setPen(Qt::white);
if (m_showViewFinder && m_videoSurface && m_videoSurface->isActive()) {
m_videoSurface->paint(&painter);
} else {
// Draw black
painter.fillRect(event->rect(), palette().background());
if (m_pictureCaptured) {
// Paint captured image
QPoint centerPic((qAbs(r.size().width() - m_capturedImage.size().width())) / 2,
(qAbs(r.size().height() - m_capturedImage.size().height())) / 2);
painter.drawImage(centerPic, m_capturedImage);
// Paint filename
painter.drawText(r, Qt::AlignBottom | Qt::AlignCenter, m_capturedImageName);
}
}
}