ScanPage.qml 5.26 KB
Newer Older
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
/*
 * Copyright © 2018-2020 Rodney Dawes
 * Copyright: 2013 Michael Zanetti <michael_zanetti@gmx.net>
 *
 * This project is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This project is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
import Ergo 0.0
import OAth 1.0
import QtGraphicalEffects 1.0
import QtMultimedia 5.8
import QtQuick 2.7
import QtQuick.Layouts 1.1
import QtQuick.Controls 2.2
import QtQuick.Window 2.0

import QZXing 2.3

Page {
    id: scanPage

    readonly property color bgColor: "#232323"
    readonly property color fgColor: "#efefef"

    signal close()
    signal save(Account account)

    header: Rectangle {
        width: parent.width
        height: units.dp(56)
        color: scanPage.bgColor

        AdaptiveToolbar {
            anchors.fill: parent
            height: units.dp(56)
            leadingActions: [
                Action {
                    color: scanPage.fgColor
                    iconName: "back"
                    shortcut: [StandardKey.Back, StandardKey.Cancel]
                    onTriggered: {
                        scanPage.close();
                    }
                }
            ]
            Text {
                anchors.fill: parent
                horizontalAlignment: Text.AlignLeft
                verticalAlignment: Text.AlignVCenter
                text: i18n.tr("Scan QR code")
                font.pixelSize: units.dp(24)
                color: scanPage.fgColor
            }
        }

        Rectangle {
            anchors.bottom: parent.bottom
            color: "#343434"
            width: parent.width
            height: units.dp(2)
        }
    }

    Camera {
        id: camera

        focus.focusMode: Camera.FocusMacro + Camera.FocusContinuous
        focus.focusPointMode: Camera.FocusPointCenter

        exposure.exposureMode: Camera.ExposureBarcode
        exposure.meteringMode: Camera.MeteringSpot

        imageProcessing.sharpeningLevel: 0.5
        imageProcessing.denoisingLevel: 0.25

        viewfinder.minimumFrameRate: 30.0
        viewfinder.maximumFrameRate: 30.0
    }

    Timer {
        id: captureTimer
        interval: 250
        repeat: true
        running: Qt.application.active
        onTriggered: {
            videoOutput.grabToImage(function(result) {
                qrCodeReader.decodeImage(result.image);
            });
        }
    }

    VideoOutput {
        id: videoOutput

        anchors.fill: parent
        fillMode: VideoOutput.PreserveAspectCrop
        source: camera
        focus: true
        orientation: Screen.primaryOrientation == Qt.PortraitOrientation ? -90 : 0
    }

    Rectangle {
        id: zoneOverlay
        anchors.fill: parent
        color: "#000000"
    }

    Item {
        id: zoneMask
        anchors.fill: parent

        Rectangle {
            color: "red"
            width: (parent.width > parent.height ? parent.height : parent.width) * 0.75
            height: width
            anchors.centerIn: parent
        }
    }

    OpacityMask {
        opacity: 0.93
        invert: true
        source: ShaderEffectSource {
            sourceItem: zoneOverlay
            hideSource: true
        }
        maskSource: ShaderEffectSource {
            sourceItem: zoneMask
            hideSource: true
        }
        anchors.fill: parent
    }

    QZXing {
        id: qrCodeReader

        enabledDecoders: QZXing.DecoderFormat_QR_CODE

        onTagFound: {
            captureTimer.stop();
            camera.stop();
            var account = AccountModel.createAccount(tag);
            if (account == null) {
                var popup = errorComponent.createObject(
                    scanPage,
                    {
                        summary: i18n.tr("Invalid Code Scanned"),
                        message: i18n.tr("The scanned QR code is not valid.")
                    }
                );
                popup.closed.connect(function() {
                    camera.start();
                    captureTimer.start();
                });
                popup.open();
            } else {
                scanPage.save(account);
            }
        }

        tryHarder: false
    }

    Text {
        id: scanLabel
        anchors {
            left: parent.left
            top: parent.top
            right: parent.right
            margins: units.dp(4)
        }

        opacity: 0.93
        width: parent.width
        padding: units.dp(4)
        y: units.dp(4)
        text: i18n.tr("Scan a QR Code containing account information")
        wrapMode: Text.WordWrap
        horizontalAlignment: Text.AlignHCenter
        verticalAlignment: Text.AlignVCenter
        font.pixelSize: units.dp(16)
        color: scanPage.fgColor
    }

    Component {
        id: errorComponent

        ErrorDialog {
            anchors.fill: parent
        }
    }
}