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 connect(videoDevicesGroup, SIGNAL(triggered(QAction*)), this, SLOT(updateCameraDevice(QAction*))); |
|
90 |
|
91 ui->actionAudio->setMenu(new QMenu(this)); |
|
92 |
|
93 setCamera(cameraDevice); |
|
94 } |
|
95 |
|
96 CameraCapture::~CameraCapture() |
|
97 { |
|
98 delete mediaRecorder; |
|
99 delete videoWidget; |
|
100 delete camera; |
|
101 } |
|
102 |
|
103 void CameraCapture::setCamera(const QByteArray &cameraDevice) |
|
104 { |
|
105 delete imageCapture; |
|
106 delete mediaRecorder; |
|
107 delete videoWidget; |
|
108 delete camera; |
|
109 |
|
110 if (cameraDevice.isEmpty()) |
|
111 camera = new QCamera; |
|
112 else |
|
113 camera = new QCamera(cameraDevice); |
|
114 |
|
115 connect(camera, SIGNAL(stateChanged(QCamera::State)), this, SLOT(updateCameraState(QCamera::State))); |
|
116 |
|
117 mediaRecorder = new QMediaRecorder(camera); |
|
118 connect(mediaRecorder, SIGNAL(stateChanged(QMediaRecorder::State)), this, SLOT(updateRecorderState(QMediaRecorder::State))); |
|
119 |
|
120 imageCapture = new QStillImageCapture(camera); |
|
121 |
|
122 audioSource = new QAudioCaptureSource(camera); |
|
123 connect(audioSource, SIGNAL(availableAudioInputsChanged()), SLOT(updateAudioDevices())); |
|
124 |
|
125 mediaRecorder->setOutputLocation(QUrl("test.mkv")); |
|
126 |
|
127 connect(mediaRecorder, SIGNAL(durationChanged(qint64)), this, SLOT(updateRecordTime())); |
|
128 connect(mediaRecorder, SIGNAL(error(QMediaRecorder::Error)), this, SLOT(displayErrorMessage())); |
|
129 |
|
130 camera->setMetaData(QtMedia::Title, QVariant(QLatin1String("Test Title"))); |
|
131 |
|
132 videoWidget = new QVideoWidget; |
|
133 videoWidget->setMediaObject(camera); |
|
134 ui->stackedWidget->addWidget(videoWidget); |
|
135 |
|
136 updateCameraState(camera->state()); |
|
137 updateRecorderState(mediaRecorder->state()); |
|
138 updateAudioDevices(); |
|
139 |
|
140 connect(imageCapture, SIGNAL(readyForCaptureChanged(bool)), ui->imageCaptureBox, SLOT(setEnabled(bool))); |
|
141 connect(imageCapture, SIGNAL(imageCaptured(QString,QImage)), this, SLOT(processCapturedImage(QString,QImage))); |
|
142 |
|
143 } |
|
144 |
|
145 void CameraCapture::updateAudioDevices() |
|
146 { |
|
147 ui->actionAudio->menu()->clear(); |
|
148 QActionGroup *audioDevicesGroup = new QActionGroup(this); |
|
149 audioDevicesGroup->setExclusive(true); |
|
150 |
|
151 if (audioSource->isAvailable()) { |
|
152 QList<QString> devices = audioSource->audioInputs(); |
|
153 for (int i=0; i<devices.size(); i++) { |
|
154 QString description = audioSource->audioDescription(devices.at(i)); |
|
155 QAction *audioDeviceAction = new QAction(devices.at(i)+" "+description, audioDevicesGroup); |
|
156 audioDeviceAction->setData(devices.at(i)); |
|
157 audioDeviceAction->setCheckable(true); |
|
158 |
|
159 ui->actionAudio->menu()->addAction(audioDeviceAction); |
|
160 |
|
161 if (devices.at(i) == audioSource->activeAudioInput()) |
|
162 audioDeviceAction->setChecked(true); |
|
163 } |
|
164 } else { |
|
165 qWarning() << "No audio device for camera service available"; |
|
166 } |
|
167 |
|
168 connect(audioDevicesGroup, SIGNAL(triggered(QAction*)), this, SLOT(updateAudioDevice(QAction*))); |
|
169 } |
|
170 |
|
171 void CameraCapture::updateRecordTime() |
|
172 { |
|
173 QString str = QString("Recorded %1 sec").arg(mediaRecorder->duration()/1000); |
|
174 ui->statusbar->showMessage(str); |
|
175 } |
|
176 |
|
177 void CameraCapture::processCapturedImage(const QString& fname, const QImage& img) |
|
178 { |
|
179 ui->lastImagePreviewLabel->setPixmap( QPixmap::fromImage(img.scaledToWidth(128)) ); |
|
180 qDebug() << "image captured:" << fname; |
|
181 } |
|
182 |
|
183 void CameraCapture::settings() |
|
184 { |
|
185 Settings settingsDialog(mediaRecorder); |
|
186 |
|
187 settingsDialog.setAudioSettings(mediaRecorder->audioSettings()); |
|
188 settingsDialog.setVideoSettings(mediaRecorder->videoSettings()); |
|
189 settingsDialog.setFormat(mediaRecorder->containerMimeType()); |
|
190 |
|
191 if (settingsDialog.exec()) { |
|
192 mediaRecorder->setEncodingSettings( |
|
193 settingsDialog.audioSettings(), |
|
194 settingsDialog.videoSettings(), |
|
195 settingsDialog.format()); |
|
196 } |
|
197 } |
|
198 |
|
199 void CameraCapture::record() |
|
200 { |
|
201 mediaRecorder->record(); |
|
202 updateRecordTime(); |
|
203 } |
|
204 |
|
205 void CameraCapture::pause() |
|
206 { |
|
207 mediaRecorder->pause(); |
|
208 } |
|
209 |
|
210 void CameraCapture::stop() |
|
211 { |
|
212 mediaRecorder->stop(); |
|
213 } |
|
214 |
|
215 void CameraCapture::takeImage() |
|
216 { |
|
217 int lastImage = 0; |
|
218 foreach( QString fileName, outputDir.entryList(QStringList() << "img_*.jpg") ) { |
|
219 int imgNumber = fileName.mid(4, fileName.size()-8).toInt(); |
|
220 lastImage = qMax(lastImage, imgNumber); |
|
221 } |
|
222 |
|
223 imageCapture->capture(QString("img_%1.jpg").arg(lastImage+1, |
|
224 4, //fieldWidth |
|
225 10, |
|
226 QLatin1Char('0'))); |
|
227 } |
|
228 |
|
229 void CameraCapture::toggleCamera() |
|
230 { |
|
231 if (camera->state() == QCamera::ActiveState) |
|
232 camera->stop(); |
|
233 else |
|
234 camera->start(); |
|
235 } |
|
236 |
|
237 void CameraCapture::updateCameraState(QCamera::State state) |
|
238 { |
|
239 if (state == QCamera::ActiveState) { |
|
240 ui->actionCamera->setEnabled(false); |
|
241 ui->actionAudio->setEnabled(false); |
|
242 ui->actionSettings->setEnabled(true); |
|
243 |
|
244 ui->startCameraButton->setText(tr("Stop Camera")); |
|
245 ui->startCameraButton->setChecked(true); |
|
246 ui->imageCaptureBox->setEnabled(true); |
|
247 ui->videoCaptureBox->setEnabled(true); |
|
248 } else { |
|
249 ui->actionCamera->setEnabled(true); |
|
250 ui->actionAudio->setEnabled(true); |
|
251 ui->actionSettings->setEnabled(true); |
|
252 |
|
253 ui->startCameraButton->setText(tr("Start Camera")); |
|
254 ui->startCameraButton->setChecked(false); |
|
255 ui->imageCaptureBox->setEnabled(false); |
|
256 ui->videoCaptureBox->setEnabled(false); |
|
257 } |
|
258 |
|
259 if (camera->isAvailable()) { |
|
260 ui->startCameraButton->setEnabled(true); |
|
261 } else { |
|
262 ui->startCameraButton->setEnabled(false); |
|
263 ui->startCameraButton->setText(tr("Camera is not available")); |
|
264 } |
|
265 } |
|
266 |
|
267 void CameraCapture::updateRecorderState(QMediaRecorder::State state) |
|
268 { |
|
269 switch (state) { |
|
270 case QMediaRecorder::StoppedState: |
|
271 ui->recordButton->setEnabled(true); |
|
272 ui->pauseButton->setEnabled(true); |
|
273 ui->stopButton->setEnabled(false); |
|
274 break; |
|
275 case QMediaRecorder::PausedState: |
|
276 ui->recordButton->setEnabled(true); |
|
277 ui->pauseButton->setEnabled(false); |
|
278 ui->stopButton->setEnabled(true); |
|
279 break; |
|
280 case QMediaRecorder::RecordingState: |
|
281 ui->recordButton->setEnabled(false); |
|
282 ui->pauseButton->setEnabled(true); |
|
283 ui->stopButton->setEnabled(true); |
|
284 break; |
|
285 } |
|
286 } |
|
287 |
|
288 |
|
289 void CameraCapture::displayErrorMessage() |
|
290 { |
|
291 QMessageBox::warning(this, "Capture error", mediaRecorder->errorString()); |
|
292 } |
|
293 |
|
294 void CameraCapture::updateCameraDevice(QAction *action) |
|
295 { |
|
296 setCamera(action->data().toByteArray()); |
|
297 } |
|
298 |
|
299 void CameraCapture::updateAudioDevice(QAction *action) |
|
300 { |
|
301 audioSource->setAudioInput(action->data().toString()); |
|
302 } |
|
303 |
|