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 "qandroidcamerazoomcontrol.h"
41 
42 #include "qandroidcamerasession.h"
43 #include "androidcamera.h"
44 #include "qandroidmultimediautils.h"
45 #include <qmath.h>
46 
47 QT_BEGIN_NAMESPACE
48 
QAndroidCameraZoomControl(QAndroidCameraSession * session)49 QAndroidCameraZoomControl::QAndroidCameraZoomControl(QAndroidCameraSession *session)
50     : QCameraZoomControl()
51     , m_cameraSession(session)
52     , m_maximumZoom(1.0)
53     , m_requestedZoom(1.0)
54     , m_currentZoom(1.0)
55 {
56     connect(m_cameraSession, SIGNAL(opened()),
57             this, SLOT(onCameraOpened()));
58 }
59 
maximumOpticalZoom() const60 qreal QAndroidCameraZoomControl::maximumOpticalZoom() const
61 {
62     // Optical zoom not supported
63     return 1.0;
64 }
65 
maximumDigitalZoom() const66 qreal QAndroidCameraZoomControl::maximumDigitalZoom() const
67 {
68     return m_maximumZoom;
69 }
70 
requestedOpticalZoom() const71 qreal QAndroidCameraZoomControl::requestedOpticalZoom() const
72 {
73     // Optical zoom not supported
74     return 1.0;
75 }
76 
requestedDigitalZoom() const77 qreal QAndroidCameraZoomControl::requestedDigitalZoom() const
78 {
79     return m_requestedZoom;
80 }
81 
currentOpticalZoom() const82 qreal QAndroidCameraZoomControl::currentOpticalZoom() const
83 {
84     // Optical zoom not supported
85     return 1.0;
86 }
87 
currentDigitalZoom() const88 qreal QAndroidCameraZoomControl::currentDigitalZoom() const
89 {
90     return m_currentZoom;
91 }
92 
zoomTo(qreal optical,qreal digital)93 void QAndroidCameraZoomControl::zoomTo(qreal optical, qreal digital)
94 {
95     Q_UNUSED(optical);
96 
97     if (!qFuzzyCompare(m_requestedZoom, digital)) {
98         m_requestedZoom = digital;
99         emit requestedDigitalZoomChanged(m_requestedZoom);
100     }
101 
102     if (m_cameraSession->camera()) {
103         digital = qBound(qreal(1), digital, m_maximumZoom);
104         int validZoomIndex = qt_findClosestValue(m_zoomRatios, qRound(digital * 100));
105         qreal newZoom = m_zoomRatios.at(validZoomIndex) / qreal(100);
106         if (!qFuzzyCompare(m_currentZoom, newZoom)) {
107             m_cameraSession->camera()->setZoom(validZoomIndex);
108             m_currentZoom = newZoom;
109             emit currentDigitalZoomChanged(m_currentZoom);
110         }
111     }
112 }
113 
onCameraOpened()114 void QAndroidCameraZoomControl::onCameraOpened()
115 {
116     if (m_cameraSession->camera()->isZoomSupported()) {
117         m_zoomRatios = m_cameraSession->camera()->getZoomRatios();
118         qreal maxZoom = m_zoomRatios.last() / qreal(100);
119         if (m_maximumZoom != maxZoom) {
120             m_maximumZoom = maxZoom;
121             emit maximumDigitalZoomChanged(m_maximumZoom);
122         }
123         zoomTo(1, m_requestedZoom);
124     } else {
125         m_zoomRatios.clear();
126         if (!qFuzzyCompare(m_maximumZoom, qreal(1))) {
127             m_maximumZoom = 1.0;
128             emit maximumDigitalZoomChanged(m_maximumZoom);
129         }
130     }
131 }
132 
133 QT_END_NAMESPACE
134