1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the Qt Mobility Components. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 |
|
42 #include "cameracapture.h" |
|
43 #include "ui_cameracapture.h" |
|
44 #include "settings.h" |
|
45 |
|
46 #include <qmediaservice.h> |
|
47 #include <qmediarecorder.h> |
|
48 #include <experimental/qcamera.h> |
|
49 #include <qvideowidget.h> |
|
50 |
|
51 #include <qmessagebox.h> |
|
52 |
|
53 #include <QtGui> |
|
54 |
|
55 CameraCapture::CameraCapture(QWidget *parent) : |
|
56 QMainWindow(parent), |
|
57 ui(new Ui::CameraCapture), |
|
58 camera(0), |
|
59 imageCapture(0), |
|
60 mediaRecorder(0), |
|
61 audioSource(0), |
|
62 videoWidget(0) |
|
63 { |
|
64 ui->setupUi(this); |
|
65 #if defined(Q_OS_SYMBIAN) |
|
66 outputDir = QDir::rootPath(); // this defaults to C:\Data in symbian |
|
67 #else |
|
68 outputDir = QDir::currentPath(); |
|
69 #endif |
|
70 |
|
71 //camera devices |
|
72 QByteArray cameraDevice; |
|
73 |
|
74 ui->actionCamera->setMenu(new QMenu(this)); |
|
75 QActionGroup *videoDevicesGroup = new QActionGroup(this); |
|
76 videoDevicesGroup->setExclusive(true); |
|
77 foreach(const QByteArray &deviceName, QCamera::availableDevices()) { |
|
78 QString description = deviceName+" "+camera->deviceDescription(deviceName); |
|
79 QAction *videoDeviceAction = new QAction(description, videoDevicesGroup); |
|
80 videoDeviceAction->setCheckable(true); |
|
81 videoDeviceAction->setData(QVariant(deviceName)); |
|
82 if (cameraDevice.isEmpty()) { |
|
83 cameraDevice = deviceName; |
|
84 videoDeviceAction->setChecked(true); |
|
85 } |
|
86 ui->actionCamera->menu()->addAction(videoDeviceAction); |
|
87 } |
|
88 |
|
89 m_autoFocus = true; |
|
90 m_takeImage = false; |
|
91 |
|
92 connect(videoDevicesGroup, SIGNAL(triggered(QAction*)), this, SLOT(updateCameraDevice(QAction*))); |
|
93 connect(ui->actionFlash_On_2, SIGNAL(triggered()), this, SLOT(setFlashOn())); |
|
94 connect(ui->actionFlash_Off, SIGNAL(triggered()), this, SLOT(setFlashOff())); |
|
95 connect(ui->actionFlash_auto, SIGNAL(triggered()), this, SLOT(setFlashAuto())); |
|
96 connect(ui->actionRed_eye, SIGNAL(triggered()), this, SLOT(setFlashRed())); |
|
97 connect(ui->actionFlash_FillIn, SIGNAL(triggered()), this, SLOT(setFlashFillIn())); |
|
98 |
|
99 connect(ui->action_Focus_On, SIGNAL(triggered()), this, SLOT(setFocusOn())); |
|
100 connect(ui->action_Focus_Off, SIGNAL(triggered()), this, SLOT(setFocusOff())); |
|
101 |
|
102 connect(ui->actionNight, SIGNAL(triggered()), this, SLOT(setExposureNight())); |
|
103 connect(ui->actionBacklight, SIGNAL(triggered()), this, SLOT(setExposureBacklight())); |
|
104 connect(ui->actionSport, SIGNAL(triggered()), this, SLOT(setExposureSport())); |
|
105 connect(ui->actionBeach, SIGNAL(triggered()), this, SLOT(setExposureBeach())); |
|
106 |
|
107 connect(ui->actionAuto, SIGNAL(triggered()), this, SLOT(setWBAuto())); |
|
108 connect(ui->actionSunlight, SIGNAL(triggered()), this, SLOT(setWBSunlight())); |
|
109 connect(ui->actionCloudy, SIGNAL(triggered()), this, SLOT(setWBCloudy())); |
|
110 connect(ui->actionTungsten, SIGNAL(triggered()), this, SLOT(setWBTungsten())); |
|
111 |
|
112 ui->actionAudio->setMenu(new QMenu(this)); |
|
113 |
|
114 setCamera(cameraDevice); |
|
115 |
|
116 mediaKeysObserver = new MediaKeysObserver(this); |
|
117 connect(mediaKeysObserver, SIGNAL(mediaKeyPressed(MediaKeysObserver::MediaKeys)), this, SLOT(handleMediaKeyEvent(MediaKeysObserver::MediaKeys))); |
|
118 } |
|
119 |
|
120 CameraCapture::~CameraCapture() |
|
121 { |
|
122 delete mediaRecorder; |
|
123 delete videoWidget; |
|
124 delete camera; |
|
125 } |
|
126 |
|
127 void CameraCapture::setFlashOn() |
|
128 { |
|
129 camera->setFlashMode(QCamera::FlashOn); |
|
130 } |
|
131 |
|
132 void CameraCapture::setFlashOff() |
|
133 { |
|
134 camera->setFlashMode(QCamera::FlashOff); |
|
135 } |
|
136 |
|
137 void CameraCapture::setFlashAuto() |
|
138 { |
|
139 camera->setFlashMode(QCamera::FlashAuto); |
|
140 } |
|
141 |
|
142 void CameraCapture::setFlashRed() |
|
143 { |
|
144 camera->setFlashMode(QCamera::FlashRedEyeReduction); |
|
145 } |
|
146 |
|
147 void CameraCapture::setFlashFillIn() |
|
148 { |
|
149 camera->setFlashMode(QCamera::FlashFill); |
|
150 } |
|
151 |
|
152 void CameraCapture::setFocusOn() |
|
153 { |
|
154 m_autoFocus = true; |
|
155 m_takeImage = false; |
|
156 } |
|
157 |
|
158 void CameraCapture::setFocusOff() |
|
159 { |
|
160 m_autoFocus = false; |
|
161 m_takeImage = false; |
|
162 } |
|
163 |
|
164 void CameraCapture::setExposureNight() |
|
165 { |
|
166 camera->setExposureMode(QCamera::ExposureNight); |
|
167 } |
|
168 |
|
169 void CameraCapture::setExposureBacklight() |
|
170 { |
|
171 camera->setExposureMode(QCamera::ExposureBacklight); |
|
172 } |
|
173 |
|
174 void CameraCapture::setExposureSport() |
|
175 { |
|
176 camera->setExposureMode(QCamera::ExposureSports); |
|
177 } |
|
178 |
|
179 void CameraCapture::setExposureBeach() |
|
180 { |
|
181 camera->setExposureMode(QCamera::ExposurePortrait); |
|
182 } |
|
183 |
|
184 void CameraCapture::setWBAuto() |
|
185 { |
|
186 camera->setWhiteBalanceMode(QCamera::WhiteBalanceAuto); |
|
187 } |
|
188 |
|
189 void CameraCapture::setWBSunlight() |
|
190 { |
|
191 camera->setWhiteBalanceMode(QCamera::WhiteBalanceSunlight); |
|
192 } |
|
193 |
|
194 void CameraCapture::setWBCloudy() |
|
195 { |
|
196 camera->setWhiteBalanceMode(QCamera::WhiteBalanceCloudy); |
|
197 } |
|
198 |
|
199 void CameraCapture::setWBTungsten() |
|
200 { |
|
201 camera->setWhiteBalanceMode(QCamera::WhiteBalanceTungsten); |
|
202 } |
|
203 |
|
204 void CameraCapture::setCamera(const QByteArray &cameraDevice) |
|
205 { |
|
206 delete imageCapture; |
|
207 delete mediaRecorder; |
|
208 delete videoWidget; |
|
209 delete camera; |
|
210 |
|
211 qDebug() << "CameraCapture::setCamera cameraDevice.isEmpty()=" << cameraDevice.isEmpty(); |
|
212 if (cameraDevice.isEmpty()) |
|
213 camera = new QCamera; |
|
214 else |
|
215 camera = new QCamera(cameraDevice); |
|
216 |
|
217 connect(camera, SIGNAL(stateChanged(QCamera::State)), this, SLOT(updateCameraState(QCamera::State))); |
|
218 connect(camera, SIGNAL(focusStatusChanged(QCamera::FocusStatus)), this, SLOT(focusStatusChanged(QCamera::FocusStatus))); |
|
219 connect(camera, SIGNAL(zoomValueChanged(qreal)), this, SLOT(zoomValueChanged(qreal))); |
|
220 connect(camera, SIGNAL(error(QCamera::Error)), this, SLOT(error(QCamera::Error))); |
|
221 |
|
222 mediaRecorder = new QMediaRecorder(camera); |
|
223 connect(mediaRecorder, SIGNAL(stateChanged(QMediaRecorder::State)), this, SLOT(updateRecorderState(QMediaRecorder::State))); |
|
224 |
|
225 imageCapture = new QStillImageCapture(camera); |
|
226 |
|
227 audioSource = new QAudioCaptureSource(camera); |
|
228 connect(audioSource, SIGNAL(availableAudioInputsChanged()), SLOT(updateAudioDevices())); |
|
229 |
|
230 mediaRecorder->setOutputLocation(QUrl("test.mkv")); |
|
231 |
|
232 connect(mediaRecorder, SIGNAL(durationChanged(qint64)), this, SLOT(updateRecordTime())); |
|
233 connect(mediaRecorder, SIGNAL(error(QMediaRecorder::Error)), this, SLOT(displayErrorMessage())); |
|
234 |
|
235 camera->setMetaData(QtMedia::Title, QVariant(QLatin1String("Test Title"))); |
|
236 |
|
237 videoWidget = new QVideoWidget; |
|
238 videoWidget->setMediaObject(camera); |
|
239 ui->stackedWidget->addWidget(videoWidget); |
|
240 |
|
241 updateCameraState(camera->state()); |
|
242 updateRecorderState(mediaRecorder->state()); |
|
243 updateAudioDevices(); |
|
244 |
|
245 connect(imageCapture, SIGNAL(readyForCaptureChanged(bool)), ui->takeImageButton, SLOT(setEnabled(bool))); |
|
246 connect(imageCapture, SIGNAL(imageCaptured(QString,QImage)), this, SLOT(processCapturedImage(QString,QImage))); |
|
247 |
|
248 } |
|
249 |
|
250 void CameraCapture::updateAudioDevices() |
|
251 { |
|
252 ui->actionAudio->menu()->clear(); |
|
253 QActionGroup *audioDevicesGroup = new QActionGroup(this); |
|
254 audioDevicesGroup->setExclusive(true); |
|
255 |
|
256 if (audioSource->isAvailable()) { |
|
257 QList<QString> devices = audioSource->audioInputs(); |
|
258 for (int i=0; i<devices.size(); i++) { |
|
259 QString description = audioSource->audioDescription(devices.at(i)); |
|
260 QAction *audioDeviceAction = new QAction(devices.at(i)+" "+description, audioDevicesGroup); |
|
261 audioDeviceAction->setData(devices.at(i)); |
|
262 audioDeviceAction->setCheckable(true); |
|
263 |
|
264 ui->actionAudio->menu()->addAction(audioDeviceAction); |
|
265 |
|
266 if (devices.at(i) == audioSource->activeAudioInput()) |
|
267 audioDeviceAction->setChecked(true); |
|
268 } |
|
269 } else { |
|
270 qWarning() << "No audio device for camera service available"; |
|
271 } |
|
272 |
|
273 connect(audioDevicesGroup, SIGNAL(triggered(QAction*)), this, SLOT(updateAudioDevice(QAction*))); |
|
274 } |
|
275 |
|
276 void CameraCapture::updateRecordTime() |
|
277 { |
|
278 QString str = QString("Recorded %1 sec").arg(mediaRecorder->duration()/1000); |
|
279 ui->statusbar->showMessage(str); |
|
280 } |
|
281 |
|
282 void CameraCapture::processCapturedImage(const QString& fname, const QImage& img) |
|
283 { |
|
284 ui->lastImagePreviewLabel->setPixmap( QPixmap::fromImage(img.scaledToWidth(128)) ); |
|
285 qDebug() << "image captured:" << fname; |
|
286 } |
|
287 |
|
288 void CameraCapture::settings() |
|
289 { |
|
290 Settings settingsDialog(mediaRecorder); |
|
291 settingsDialog.setAudioSettings(mediaRecorder->audioSettings()); |
|
292 settingsDialog.setVideoSettings(mediaRecorder->videoSettings()); |
|
293 settingsDialog.setFormat(mediaRecorder->containerMimeType()); |
|
294 |
|
295 if (settingsDialog.exec() == QDialog::Accepted) { |
|
296 mediaRecorder->setEncodingSettings( |
|
297 settingsDialog.audioSettings(), |
|
298 settingsDialog.videoSettings(), |
|
299 settingsDialog.format()); |
|
300 } |
|
301 } |
|
302 |
|
303 void CameraCapture::record() |
|
304 { |
|
305 int lastImage = 0; |
|
306 foreach( QString fileName, outputDir.entryList(QStringList() << "clip_*.mpg") ) { |
|
307 int imgNumber = fileName.mid(5, fileName.size()-9).toInt(); |
|
308 lastImage = qMax(lastImage, imgNumber); |
|
309 } |
|
310 |
|
311 QUrl location(QDir::toNativeSeparators(outputDir.canonicalPath()+ |
|
312 QString("/clip_%1.mpg").arg(lastImage+1,4,10,QLatin1Char('0')))); |
|
313 |
|
314 mediaRecorder->setOutputLocation(location); |
|
315 |
|
316 mediaRecorder->record(); |
|
317 updateRecordTime(); |
|
318 } |
|
319 |
|
320 void CameraCapture::pause() |
|
321 { |
|
322 mediaRecorder->pause(); |
|
323 } |
|
324 |
|
325 void CameraCapture::stop() |
|
326 { |
|
327 mediaRecorder->stop(); |
|
328 } |
|
329 |
|
330 void CameraCapture::takeImage() |
|
331 { |
|
332 if (m_autoFocus) { |
|
333 m_takeImage = true; |
|
334 camera->startFocusing(); |
|
335 } else { |
|
336 int lastImage = 0; |
|
337 foreach( QString fileName, outputDir.entryList(QStringList() << "img_*.jpg") ) { |
|
338 int imgNumber = fileName.mid(4, fileName.size()-8).toInt(); |
|
339 lastImage = qMax(lastImage, imgNumber); |
|
340 } |
|
341 |
|
342 imageCapture->capture(QString("img_%1.jpg").arg(lastImage+1, |
|
343 4, //fieldWidth |
|
344 10, |
|
345 QLatin1Char('0'))); |
|
346 } |
|
347 } |
|
348 |
|
349 void CameraCapture::toggleCamera() |
|
350 { |
|
351 if (camera->state() == QCamera::ActiveState){ |
|
352 camera->stop(); |
|
353 } |
|
354 else |
|
355 camera->start(); |
|
356 } |
|
357 |
|
358 void CameraCapture::updateCameraState(QCamera::State state) |
|
359 { |
|
360 qDebug() << "CameraCapture::updateCameraState(), state="<<state; |
|
361 if (state == QCamera::ActiveState) { |
|
362 ui->actionCamera->setEnabled(false); |
|
363 ui->actionAudio->setEnabled(false); |
|
364 ui->actionSettings->setEnabled(true); |
|
365 |
|
366 ui->startCameraButton->setText(tr("Stop Camera")); |
|
367 ui->startCameraButton->setChecked(true); |
|
368 //ui->imageCaptureBox->setEnabled(true); |
|
369 //ui->videoCaptureBox->setEnabled(true); |
|
370 } else { |
|
371 ui->actionCamera->setEnabled(true); |
|
372 ui->actionAudio->setEnabled(true); |
|
373 ui->actionSettings->setEnabled(true); |
|
374 |
|
375 ui->startCameraButton->setText(tr("Start Camera")); |
|
376 ui->startCameraButton->setChecked(false); |
|
377 //ui->imageCaptureBox->setEnabled(false); |
|
378 //ui->videoCaptureBox->setEnabled(false); |
|
379 } |
|
380 |
|
381 if (camera->isAvailable()) { |
|
382 ui->startCameraButton->setEnabled(true); |
|
383 } else { |
|
384 ui->startCameraButton->setEnabled(false); |
|
385 ui->startCameraButton->setText(tr("Camera is not available")); |
|
386 } |
|
387 } |
|
388 |
|
389 void CameraCapture::updateRecorderState(QMediaRecorder::State state) |
|
390 { |
|
391 switch (state) { |
|
392 case QMediaRecorder::StoppedState: |
|
393 ui->recordButton->setEnabled(true); |
|
394 ui->pauseButton->setEnabled(true); |
|
395 ui->stopButton->setEnabled(false); |
|
396 break; |
|
397 case QMediaRecorder::PausedState: |
|
398 ui->recordButton->setEnabled(true); |
|
399 ui->pauseButton->setEnabled(false); |
|
400 ui->stopButton->setEnabled(true); |
|
401 break; |
|
402 case QMediaRecorder::RecordingState: |
|
403 ui->recordButton->setEnabled(false); |
|
404 ui->pauseButton->setEnabled(true); |
|
405 ui->stopButton->setEnabled(true); |
|
406 break; |
|
407 } |
|
408 } |
|
409 |
|
410 |
|
411 void CameraCapture::displayErrorMessage() |
|
412 { |
|
413 QMessageBox::warning(this, "Capture error", mediaRecorder->errorString()); |
|
414 } |
|
415 |
|
416 void CameraCapture::updateCameraDevice(QAction *action) |
|
417 { |
|
418 qDebug() << "CameraCapture::updateCameraDevice(), action="<<action; |
|
419 qDebug() << "CameraCapture::updateCameraDevice(), device="<<action->data().toByteArray(); |
|
420 setCamera(action->data().toByteArray()); |
|
421 } |
|
422 |
|
423 void CameraCapture::updateAudioDevice(QAction *action) |
|
424 { |
|
425 audioSource->setAudioInput(action->data().toString()); |
|
426 } |
|
427 |
|
428 void CameraCapture::focusStatusChanged(QCamera::FocusStatus status) |
|
429 { |
|
430 qDebug() << "CameraCapture focus locked"; |
|
431 if (status == QCamera::FocusReached && m_takeImage) { |
|
432 int lastImage = 0; |
|
433 foreach( QString fileName, outputDir.entryList(QStringList() << "img_*.jpg") ) { |
|
434 int imgNumber = fileName.mid(4, fileName.size()-8).toInt(); |
|
435 lastImage = qMax(lastImage, imgNumber); |
|
436 } |
|
437 |
|
438 imageCapture->capture(QString("img_%1.jpg").arg(lastImage+1, |
|
439 4, //fieldWidth |
|
440 10, |
|
441 QLatin1Char('0'))); |
|
442 m_takeImage = false; |
|
443 } |
|
444 } |
|
445 |
|
446 void CameraCapture::zoomValueChanged(qreal value) |
|
447 { |
|
448 qDebug() << "CameraCapture zoom value changed to: " << value; |
|
449 } |
|
450 |
|
451 void CameraCapture::handleMediaKeyEvent(MediaKeysObserver::MediaKeys key) |
|
452 { |
|
453 switch (key) { |
|
454 case MediaKeysObserver::EVolIncKey: |
|
455 camera->zoomTo(0, camera->digitalZoom() + 5); |
|
456 break; |
|
457 case MediaKeysObserver::EVolDecKey: |
|
458 camera->zoomTo(0, camera->digitalZoom() - 5); |
|
459 break; |
|
460 default: |
|
461 break; |
|
462 } |
|
463 } |
|
464 |
|
465 void CameraCapture::error(QCamera::Error aError) |
|
466 { |
|
467 qDebug() << "CameraCapture error: " << aError; |
|
468 QMessageBox msgBox; |
|
469 msgBox.setStandardButtons(QMessageBox::Close); |
|
470 |
|
471 if (aError == QCamera::NoError) { |
|
472 msgBox.setText(tr("NoError")); |
|
473 } else if (aError == QCamera::NotReadyToCaptureError) { |
|
474 msgBox.setText(tr("NotReadyToCaptureError")); |
|
475 } else if (aError == QCamera::InvalidRequestError) { |
|
476 msgBox.setText(tr("InvalidRequestError")); |
|
477 } else if (aError == QCamera::ServiceMissingError) { |
|
478 msgBox.setText(tr("ServiceMissingError")); |
|
479 } else if (aError == QCamera::NotSupportedFeatureError) { |
|
480 msgBox.setText(tr("NotSupportedFeatureError")); |
|
481 } else if (aError == QCamera::CameraError) { |
|
482 msgBox.setText(tr("CameraError")); |
|
483 } |
|
484 else { |
|
485 msgBox.setText(tr("Other error")); |
|
486 } |
|
487 msgBox.exec(); |
|
488 } |
|