summaryrefslogtreecommitdiffstats
path: root/src/multimedia/camera/qcamerafocus.cpp
blob: e73a8ea46c63e054c26dfe99ccd28ca445266489 (plain)
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "qcamerafocus.h"
#include "qmediaobject_p.h"

#include <qcamera.h>
#include <qcameracontrol.h>
#include <qcameraexposurecontrol.h>
#include <qcamerafocuscontrol.h>
#include <qcamerazoomcontrol.h>
#include <qmediarecordercontrol.h>
#include <qcameraimagecapturecontrol.h>
#include <qvideodeviceselectorcontrol.h>

#include <QtCore/QDebug>

QT_BEGIN_NAMESPACE

static void qRegisterCameraFocusMetaTypes()
{
    qRegisterMetaType<QCameraFocus::FocusModes>("QCameraFocus::FocusModes");
    qRegisterMetaType<QCameraFocus::FocusPointMode>("QCameraFocus::FocusPointMode");
}

Q_CONSTRUCTOR_FUNCTION(qRegisterCameraFocusMetaTypes)


class QCameraFocusFakeZoomControl : public QCameraZoomControl
{
public:
    QCameraFocusFakeZoomControl(QObject *parent) :
        QCameraZoomControl(parent) {}

    qreal maximumOpticalZoom() const override { return 1.0; }
    qreal maximumDigitalZoom() const override { return 1.0; }

    qreal requestedOpticalZoom() const override { return 1.0; }
    qreal requestedDigitalZoom() const override { return 1.0; }
    qreal currentOpticalZoom() const override { return 1.0; }
    qreal currentDigitalZoom() const override { return 1.0; }

    void zoomTo(qreal, qreal) override { qWarning("The camera doesn't support zooming."); }
};

class QCameraFocusFakeFocusControl : public QCameraFocusControl
{
public:
    QCameraFocusFakeFocusControl(QObject *parent) :
        QCameraFocusControl(parent) {}

    QCameraFocus::FocusModes focusMode() const override { return QCameraFocus::AutoFocus; }
    void setFocusMode(QCameraFocus::FocusModes) override { qWarning("Focus mode selection is not supported"); }
    bool isFocusModeSupported(QCameraFocus::FocusModes) const override { return false; }

    QCameraFocus::FocusPointMode focusPointMode() const override { return QCameraFocus::FocusPointAuto; }
    void setFocusPointMode(QCameraFocus::FocusPointMode) override { qWarning("Focus points mode selection is not supported"); }
    bool isFocusPointModeSupported(QCameraFocus::FocusPointMode) const override { return false; }
    QPointF customFocusPoint() const override { return QPointF(0.5,0.5); }
    void setCustomFocusPoint(const QPointF &) override { qWarning("Focus points selection is not supported"); }

    QCameraFocusZoneList focusZones() const override { return QCameraFocusZoneList(); }
};



class QCameraFocusZoneData : public QSharedData
{
public:
    QCameraFocusZoneData():
        status(QCameraFocusZone::Invalid)
    {

    }

    QCameraFocusZoneData(const QRectF &_area, QCameraFocusZone::FocusZoneStatus _status):
        area(_area),
        status(_status)
    {

    }


    QCameraFocusZoneData(const QCameraFocusZoneData &other):
        QSharedData(other),
        area(other.area),
        status(other.status)
    {
    }

    QCameraFocusZoneData& operator=(const QCameraFocusZoneData &other)
    {
        area = other.area;
        status = other.status;
        return *this;
    }

    QRectF area;
    QCameraFocusZone::FocusZoneStatus status;
};


/*!
    \class QCameraFocusZone

    \brief The QCameraFocusZone class provides information on zones used for autofocusing a camera.

    \inmodule QtMultimedia
    \ingroup multimedia
    \ingroup multimedia_camera

    For cameras that support autofocusing, in order for a camera to autofocus on
    part of a sensor frame, it considers different zones within the frame.  Which
    zones to use, and where the zones are located vary between different cameras.

    This class exposes what zones are used by a particular camera, and a list of the
    zones can be retrieved by a \l QCameraFocus instance.

    You can use this information to present visual feedback - for example, drawing
    rectangles around areas of the camera frame that are in focus, or changing the
    color of a zone as it comes into focus.

    \snippet multimedia-snippets/camera.cpp Camera focus zones

    \sa QCameraFocus
*/

/*!
    \enum QCameraFocusZone::FocusZoneStatus

    \value Invalid      This zone is not valid
    \value Unused       This zone may be used for autofocusing, but is not currently.
    \value Selected     This zone is currently being used for autofocusing, but is not in focus.
    \value Focused      This zone is being used for autofocusing and is currently in focus.
*/

/*!
 * \internal
 * Creates a new, empty QCameraFocusZone.
 */
QCameraFocusZone::QCameraFocusZone()
    :d(new QCameraFocusZoneData)
{

}

/*!
 * \internal
 * Creates a new QCameraFocusZone with the supplied \a area and \a status.
 */
QCameraFocusZone::QCameraFocusZone(const QRectF &area, QCameraFocusZone::FocusZoneStatus status)
    :d(new QCameraFocusZoneData(area, status))
{
}

/*!
 * Creates a new QCameraFocusZone as a copy of \a other.
 */
QCameraFocusZone::QCameraFocusZone(const QCameraFocusZone &other)
    :d(other.d)
{

}

/*!
 * Destroys this QCameraFocusZone.
 */
QCameraFocusZone::~QCameraFocusZone()
{

}

/*!
 * Assigns \a other to this QCameraFocusZone.
 */
QCameraFocusZone& QCameraFocusZone::operator=(const QCameraFocusZone &other)
{
    d = other.d;
    return *this;
}

/*!
 * Returns true if this focus zone is the same as \a other.
 */
bool QCameraFocusZone::operator==(const QCameraFocusZone &other) const
{
    return d == other.d ||
           (d->area == other.d->area && d->status == other.d->status);
}

/*!
 * Returns true if this focus zone is not the same as \a other.
 */
bool QCameraFocusZone::operator!=(const QCameraFocusZone &other) const
{
    return !(*this == other);
}

/*!
 * Returns true if this focus zone has a valid area and status.
 */
bool QCameraFocusZone::isValid() const
{
    return d->status != Invalid && !d->area.isValid();
}

/*!
 * Returns the area of the camera frame that this focus zone encompasses.
 *
 * Coordinates are in frame relative coordinates - \c QPointF(0,0) is the top
 * left of the frame, and \c QPointF(1,1) is the bottom right.
 */
QRectF QCameraFocusZone::area() const
{
    return d->area;
}

/*!
 * Returns the current status of this focus zone.
 */
QCameraFocusZone::FocusZoneStatus QCameraFocusZone::status() const
{
    return d->status;
}

/*!
 * \internal
 * Sets the current status of this focus zone to \a status.
 */
void QCameraFocusZone::setStatus(QCameraFocusZone::FocusZoneStatus status)
{
    d->status = status;
}


/*!
    \class QCameraFocus

    \brief The QCameraFocus class provides an interface for focus and zoom related camera settings.

    \inmodule QtMultimedia
    \ingroup multimedia
    \ingroup multimedia_camera

    On hardware that supports it, this class lets you adjust the focus
    or zoom (both optical and digital).  This also includes things
    like "Macro" mode for close up work (e.g. reading barcodes, or
    recognising letters), or "touch to focus" - indicating an
    interesting area of the viewfinder for the hardware to attempt
    to focus on.

    \snippet multimedia-snippets/camera.cpp Camera custom zoom

    Zooming can be accomplished in a number of ways - usually the more
    expensive but higher quality approach is an optical zoom, which allows
    using the full extent of the camera sensor to gather image pixels. In
    addition it is possible to digitally zoom, which will generally just
    enlarge part of the sensor frame and throw away other parts.  If the
    camera hardware supports optical zoom this should generally always
    be used first.  The \l maximumOpticalZoom() method allows this to be
    checked.  The \l zoomTo() method allows changing both optical and
    digital zoom at once.

    \snippet multimedia-snippets/camera.cpp Camera combined zoom

    \section2 Some notes on autofocus
    Some hardware supports a movable focus lens assembly, and typically
    this hardware also supports automatically focusing via some heuristic.
    You can influence this via the \l FocusPointMode setting - typically
    the center of the frame is brought into focus, but some hardware
    also supports focusing on any faces detected in the frame, or on
    a specific point (usually provided by a user in a "touch to focus"
    scenario).

    This class (in combination with \l QCameraFocusZone)
    can expose information on what parts of the camera sensor image
    are in focus or are being used for autofocusing via the \l focusZones()
    property:

    \snippet multimedia-snippets/camera.cpp Camera focus zones

    \sa QCameraFocusZone
*/


class QCameraFocusPrivate : public QMediaObjectPrivate
{
    Q_DECLARE_NON_CONST_PUBLIC(QCameraFocus)
public:
    void initControls();

    QCameraFocus *q_ptr;

    QCamera *camera;

    QCameraFocusControl *focusControl;
    QCameraZoomControl *zoomControl;
    bool available;
};


void QCameraFocusPrivate::initControls()
{
    Q_Q(QCameraFocus);

    focusControl = nullptr;
    zoomControl = nullptr;

    QMediaService *service = camera->service();
    if (service) {
        focusControl = qobject_cast<QCameraFocusControl *>(service->requestControl(QCameraFocusControl_iid));
        zoomControl = qobject_cast<QCameraZoomControl *>(service->requestControl(QCameraZoomControl_iid));
    }

    available = focusControl != nullptr;

    if (!focusControl)
        focusControl = new QCameraFocusFakeFocusControl(q);

    if (!zoomControl)
        zoomControl = new QCameraFocusFakeZoomControl(q);

    q->connect(focusControl, SIGNAL(focusZonesChanged()), q, SIGNAL(focusZonesChanged()));

    q->connect(zoomControl, SIGNAL(currentOpticalZoomChanged(qreal)),
               q, SIGNAL(opticalZoomChanged(qreal)));
    q->connect(zoomControl, SIGNAL(currentDigitalZoomChanged(qreal)),
               q, SIGNAL(digitalZoomChanged(qreal)));
    q->connect(zoomControl, SIGNAL(maximumOpticalZoomChanged(qreal)),
               q, SIGNAL(maximumOpticalZoomChanged(qreal)));
    q->connect(zoomControl, SIGNAL(maximumDigitalZoomChanged(qreal)),
               q, SIGNAL(maximumDigitalZoomChanged(qreal)));
}

/*!
    \internal
    Construct a QCameraFocus for \a camera.
*/

QCameraFocus::QCameraFocus(QCamera *camera):
    QObject(camera), d_ptr(new QCameraFocusPrivate)
{
    Q_D(QCameraFocus);
    d->camera = camera;
    d->q_ptr = this;
    d->initControls();
}


/*!
    Destroys the camera focus object.
*/

QCameraFocus::~QCameraFocus()
{
    delete d_ptr;
}

/*!
    Returns true if focus related settings are supported by this camera.

    You may need to also check if any specific features are supported.
*/
bool QCameraFocus::isAvailable() const
{
    return d_func()->available;
}

/*!
  \property QCameraFocus::focusMode
  \brief the current camera focus mode.


  This controls the way the camera lens assembly is configured.

  \sa QCameraFocus::isFocusModeSupported()
*/

QCameraFocus::FocusModes QCameraFocus::focusMode() const
{
    return d_func()->focusControl->focusMode();
}

void QCameraFocus::setFocusMode(QCameraFocus::FocusModes mode)
{
    d_func()->focusControl->setFocusMode(mode);
}

/*!
    Returns true if the focus \a mode is supported by camera.
*/

bool QCameraFocus::isFocusModeSupported(FocusModes mode) const
{
    return d_func()->focusControl->isFocusModeSupported(mode);
}

/*!
  \property QCameraFocus::focusPointMode
  \brief the current camera focus point selection mode.

  If the camera focus mode is set to use an autofocusing mode,
  this property controls the way the camera will select areas
  of the frame to use for autofocusing.

  \sa QCameraFocus::isFocusPointModeSupported()
*/

QCameraFocus::FocusPointMode QCameraFocus::focusPointMode() const
{
    return d_func()->focusControl->focusPointMode();
}

void QCameraFocus::setFocusPointMode(QCameraFocus::FocusPointMode mode)
{
    d_func()->focusControl->setFocusPointMode(mode);
}

/*!
  Returns true if focus point \a mode is supported.
 */
bool QCameraFocus::isFocusPointModeSupported(QCameraFocus::FocusPointMode mode) const
{
    return d_func()->focusControl->isFocusPointModeSupported(mode);
}

/*!
  \property QCameraFocus::customFocusPoint

  This property represents the position of the custom focus point, in relative frame coordinates:
  QPointF(0,0) points to the left top frame point, QPointF(0.5,0.5) points to the frame center.

  The custom focus point property is used only in \c FocusPointCustom focus mode.
 */

QPointF QCameraFocus::customFocusPoint() const
{
    return d_func()->focusControl->customFocusPoint();
}

void QCameraFocus::setCustomFocusPoint(const QPointF &point)
{
    d_func()->focusControl->setCustomFocusPoint(point);
}

/*!
  \property QCameraFocus::focusZones

  Returns the list of active focus zones.

  If QCamera::FocusPointAuto or QCamera::FocusPointFaceDetection focus mode is selected
  this method returns the list of zones the camera is actually focused on.

  The coordinates system is the same as for custom focus points:
  QPointF(0,0) points to the left top frame point, QPointF(0.5,0.5) points to the frame center.
 */
QCameraFocusZoneList QCameraFocus::focusZones() const
{
    return d_func()->focusControl->focusZones();
}

/*!
    Returns the maximum optical zoom.

    This will be \c 1.0 on cameras that do not support optical zoom.
*/

qreal QCameraFocus::maximumOpticalZoom() const
{
    return d_func()->zoomControl->maximumOpticalZoom();
}

/*!
    Returns the maximum digital zoom

    This will be \c 1.0 on cameras that do not support digital zoom.
*/

qreal QCameraFocus::maximumDigitalZoom() const
{
    return d_func()->zoomControl->maximumDigitalZoom();
}

/*!
  \property QCameraFocus::opticalZoom
  \brief the current optical zoom value.

  \sa QCameraFocus::digitalZoom
*/

qreal QCameraFocus::opticalZoom() const
{
    return d_func()->zoomControl->currentOpticalZoom();
}

/*!
  \property QCameraFocus::digitalZoom
  \brief the current digital zoom value.

  \sa QCameraFocus::opticalZoom
*/
qreal QCameraFocus::digitalZoom() const
{
    return d_func()->zoomControl->currentDigitalZoom();
}


/*!
    Set the camera \a optical and \a digital zoom values.

    Since there may be a physical component to move, the change in
    zoom value may not be instantaneous.

*/
void QCameraFocus::zoomTo(qreal optical, qreal digital)
{
    d_func()->zoomControl->zoomTo(optical, digital);
}

/*!
    \enum QCameraFocus::FocusMode

    \value ManualFocus          Manual or fixed focus mode.
    \value HyperfocalFocus      Focus to hyperfocal distance, with the maximum depth of field achieved.
                                All objects at distances from half of this
                                distance out to infinity will be acceptably sharp.
    \value InfinityFocus        Focus strictly to infinity.
    \value AutoFocus            One-shot auto focus mode.
    \value ContinuousFocus      Continuous auto focus mode.
    \value MacroFocus           One shot auto focus to objects close to camera.
*/

/*!
    \enum QCameraFocus::FocusPointMode

    \value FocusPointAuto       Automatically select one or multiple focus points.
    \value FocusPointCenter     Focus to the frame center.
    \value FocusPointFaceDetection Focus on faces in the frame.
    \value FocusPointCustom     Focus to the custom point, defined by QCameraFocus::customFocusPoint property.
*/

/*!
    \fn void QCameraFocus::opticalZoomChanged(qreal value)

    Signal emitted when optical zoom value changes to new \a value.
*/

/*!
    \fn void QCameraFocus::digitalZoomChanged(qreal value)

    Signal emitted when digital zoom value changes to new \a value.
*/

/*!
    \fn void QCameraFocus::maximumOpticalZoomChanged(qreal zoom)

    Signal emitted when the maximum supported optical \a zoom value changed.
*/

/*!
    \fn void QCameraFocus::maximumDigitalZoomChanged(qreal zoom)

    Signal emitted when the maximum supported digital \a zoom value changed.

    The maximum supported zoom value can depend on other camera settings,
    like capture mode or resolution.
*/



/*!
  \fn QCameraFocus::focusZonesChanged()

  This signal is emitted when the set of zones used in autofocusing is changed.

  This can change when a zone is focused or loses focus, or new focus zones
  have been detected.
*/


#include "moc_qcamerafocus.cpp"
QT_END_NAMESPACE