1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#include "qcamerainfo.h"
41
42#include "qcamera_p.h"
43#include "qmediaserviceprovider_p.h"
44
45#include <qvideodeviceselectorcontrol.h>
46#include <qcamerainfocontrol.h>
47
48QT_BEGIN_NAMESPACE
49
50/*!
51 \class QCameraInfo
52 \brief The QCameraInfo class provides general information about camera devices.
53 \since 5.3
54 \inmodule QtMultimedia
55 \ingroup multimedia
56 \ingroup multimedia_camera
57
58 QCameraInfo lets you query for camera devices that are currently available on the system.
59
60 The static functions defaultCamera() and availableCameras() provide you a list of all
61 available cameras.
62
63 This example prints the name of all available cameras:
64
65 \snippet multimedia-snippets/camera.cpp Camera listing
66
67 A QCameraInfo can be used to construct a QCamera. The following example instantiates a QCamera
68 whose camera device is named 'mycamera':
69
70 \snippet multimedia-snippets/camera.cpp Camera selection
71
72 You can also use QCameraInfo to get general information about a camera device such as
73 description, physical position on the system, or camera sensor orientation.
74
75 \snippet multimedia-snippets/camera.cpp Camera info
76
77 \sa QCamera
78*/
79
80class QCameraInfoPrivate
81{
82public:
83 QCameraInfoPrivate() : isNull(true), position(QCamera::UnspecifiedPosition), orientation(0)
84 { }
85
86 bool isNull;
87 QString deviceName;
88 QString description;
89 QCamera::Position position;
90 int orientation;
91};
92
93/*!
94 Constructs a camera info object for \a camera.
95
96 You can use it to query information about the \a camera object passed as argument.
97
98 If the \a camera is invalid, for example when no camera device is available on the system,
99 the QCameraInfo object will be invalid and isNull() will return true.
100*/
101QCameraInfo::QCameraInfo(const QCamera &camera)
102 : d(new QCameraInfoPrivate)
103{
104 const QVideoDeviceSelectorControl *deviceControl = camera.d_func()->deviceControl;
105 if (deviceControl && deviceControl->deviceCount() > 0) {
106 const int selectedDevice = deviceControl->selectedDevice();
107 d->deviceName = deviceControl->deviceName(index: selectedDevice);
108 d->description = deviceControl->deviceDescription(index: selectedDevice);
109 d->isNull = false;
110 }
111
112 const QCameraInfoControl *infoControl = camera.d_func()->infoControl;
113 if (infoControl) {
114 d->position = infoControl->cameraPosition(deviceName: d->deviceName);
115 d->orientation = infoControl->cameraOrientation(deviceName: d->deviceName);
116 d->isNull = false;
117 }
118}
119
120/*!
121 Constructs a camera info object from a camera device \a name.
122
123 If no such device exists, the QCameraInfo object will be invalid and isNull() will return true.
124*/
125QCameraInfo::QCameraInfo(const QByteArray &name)
126 : d(new QCameraInfoPrivate)
127{
128 if (!name.isNull()) {
129 QMediaServiceProvider *provider = QMediaServiceProvider::defaultServiceProvider();
130 const QByteArray service(Q_MEDIASERVICE_CAMERA);
131 if (provider->devices(serviceType: service).contains(t: name)) {
132 d->deviceName = QString::fromLatin1(str: name);
133 d->description = provider->deviceDescription(serviceType: service, device: name);
134 d->position = provider->cameraPosition(device: name);
135 d->orientation = provider->cameraOrientation(device: name);
136 d->isNull = false;
137 }
138 }
139}
140
141/*!
142 Constructs a copy of \a other.
143*/
144QCameraInfo::QCameraInfo(const QCameraInfo &other)
145 : d(other.d)
146{
147}
148
149/*!
150 Destroys the QCameraInfo.
151*/
152QCameraInfo::~QCameraInfo()
153{
154}
155
156/*!
157 Returns true if this QCameraInfo is equal to \a other.
158*/
159bool QCameraInfo::operator==(const QCameraInfo &other) const
160{
161 if (d == other.d)
162 return true;
163
164 return (d->deviceName == other.d->deviceName
165 && d->description == other.d->description
166 && d->position == other.d->position
167 && d->orientation == other.d->orientation);
168}
169
170/*!
171 Returns true if this QCameraInfo is null or invalid.
172*/
173bool QCameraInfo::isNull() const
174{
175 return d->isNull;
176}
177
178/*!
179 Returns the device name of the camera
180
181 This is a unique ID to identify the camera and may not be human-readable.
182*/
183QString QCameraInfo::deviceName() const
184{
185 return d->deviceName;
186}
187
188/*!
189 Returns the human-readable description of the camera.
190*/
191QString QCameraInfo::description() const
192{
193 return d->description;
194}
195
196/*!
197 Returns the physical position of the camera on the hardware system.
198*/
199QCamera::Position QCameraInfo::position() const
200{
201 return d->position;
202}
203
204/*!
205 Returns the physical orientation of the camera sensor.
206
207 The value is the orientation angle (clockwise, in steps of 90 degrees) of the camera sensor
208 in relation to the display in its natural orientation.
209
210 You can show the camera image in the correct orientation by rotating it by this value in the
211 anti-clockwise direction.
212
213 For example, suppose a mobile device which is naturally in portrait orientation. The back-facing
214 camera is mounted in landscape. If the top side of the camera sensor is aligned with the
215 right edge of the screen in natural orientation, the value should be 270. If the top side of a
216 front-facing camera sensor is aligned with the right of the screen, the value should be 90.
217*/
218int QCameraInfo::orientation() const
219{
220 return d->orientation;
221}
222
223/*!
224 Returns the default camera on the system.
225
226 The returned object should be checked using isNull() before being used, in case there is no
227 default camera or no cameras at all.
228
229 \sa availableCameras()
230*/
231QCameraInfo QCameraInfo::defaultCamera()
232{
233 return QCameraInfo(QMediaServiceProvider::defaultServiceProvider()->defaultDevice(Q_MEDIASERVICE_CAMERA));
234}
235
236/*!
237 Returns a list of available cameras on the system which are located at \a position.
238
239 If \a position is not specified or if the value is QCamera::UnspecifiedPosition, a list of
240 all available cameras will be returned.
241*/
242QList<QCameraInfo> QCameraInfo::availableCameras(QCamera::Position position)
243{
244 QList<QCameraInfo> cameras;
245
246 const QMediaServiceProvider *provider = QMediaServiceProvider::defaultServiceProvider();
247 const QByteArray service(Q_MEDIASERVICE_CAMERA);
248 const QList<QByteArray> devices = provider->devices(serviceType: service);
249 for (int i = 0; i < devices.count(); ++i) {
250 const QByteArray &name = devices.at(i);
251 if (position == QCamera::UnspecifiedPosition
252 || position == provider->cameraPosition(device: name)) {
253 cameras.append(t: QCameraInfo(name));
254 }
255 }
256
257 return cameras;
258}
259
260/*!
261 Sets the QCameraInfo object to be equal to \a other.
262*/
263QCameraInfo& QCameraInfo::operator=(const QCameraInfo& other)
264{
265 d = other.d;
266 return *this;
267}
268
269/*!
270 \fn QCameraInfo::operator!=(const QCameraInfo &other) const
271
272 Returns true if this QCameraInfo is different from \a other.
273*/
274
275#ifndef QT_NO_DEBUG_STREAM
276QDebug operator<<(QDebug d, const QCameraInfo &camera)
277{
278 d.maybeSpace() << QStringLiteral("QCameraInfo(deviceName=%1, position=%2, orientation=%3)")
279 .arg(a: camera.deviceName())
280 .arg(a: QString::fromLatin1(str: QCamera::staticMetaObject.enumerator(index: QCamera::staticMetaObject.indexOfEnumerator(name: "Position"))
281 .valueToKey(value: camera.position())))
282 .arg(a: camera.orientation());
283 return d.space();
284}
285#endif
286
287QT_END_NAMESPACE
288

source code of qtmultimedia/src/multimedia/camera/qcamerainfo.cpp