-
Notifications
You must be signed in to change notification settings - Fork 5
/
example.qml
285 lines (242 loc) · 7.54 KB
/
example.qml
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
import QtQuick 2.6
import QtQuick.Controls 2.0
import QtQuick.Layouts 1.3
import QtMultimedia 5.5
import bar.code 1.0
Item {
id: cameraItem
focus: true;
property bool scanOnly: true;
property bool imageCapture: false
property bool oneShot: false;
property bool cameraSelectionEnabled: true
property bool autoStart: false
property string barcode;
// Metadata
property string metaSubject: ""
// Flash, for simplicity we just use Off/Auto
property bool flash: false
signal barcodeFound(string data);
signal imageCaptured(string path)
// Emit after oneshot decoding
signal decodeDone()
Camera {
id: camera
deviceId: "/dev/video1" // XXX
captureMode: scanOnly ? Camera.CaptureViewfinder : Camera.CaptureStillImage;
onErrorStringChanged: console.debug("Error: "+errorString)
onCameraStateChanged: {
console.debug("State: "+cameraState)
switch (cameraState) {
case Camera.ActiveState:
console.debug("DigitalZoom: "+maximumDigitalZoom)
console.debug("OpticalZoom: "+maximumOpticalZoom)
break;
}
}
onCameraStatusChanged: console.debug("Status: "+cameraStatus)
onDigitalZoomChanged: console.debug(digitalZoom)
focus {
focusMode: Camera.FocusContinuous
}
metaData.subject: metaSubject
imageCapture {
onImageCaptured: {
console.debug("Image captured!")
console.debug(camera.imageCapture.capturedImagePath)
previewImage.source=preview
}
onCaptureFailed: {
console.debug("Capture failed")
}
onImageSaved: {
console.debug("Image saved: "+path)
cameraItem.imageCaptured(path)
}
}
flash.mode: cameraItem.flash ? Camera.FlashAuto : Camera.FlashOff
Component.onCompleted: {
console.debug("Camera is: "+deviceId)
console.debug("Camera orientation is: "+orientation)
// digitalZoom=1.0;
}
}
BarcodeScanner {
id: scanner
//enabledFormats: BarcodeScanner.BarCodeFormat_2D | BarcodeScanner.BarCodeFormat_1D
enabledFormats: BarcodeScanner.BarCodeFormat_1D
rotate: camera.orientation!=0 ? true : false;
onTagFound: {
console.debug("TAG: "+tag);
cameraItem.barcode=tag;
barcodeFound(tag)
}
onDecodingStarted: {
}
onDecodingFinished: {
if (succeeded && cameraItem.oneShot) {
camera.stop();
decodeDone();
}
}
}
ColumnLayout {
spacing: 8
anchors.fill: parent
VideoOutput {
id: videoOutput
source: camera
autoOrientation: true
Layout.fillWidth: true
Layout.fillHeight: true
Layout.alignment: Qt.AlignHCenter
Layout.minimumWidth: 320
Layout.minimumHeight: 320
fillMode: Image.PreserveAspectFit
filters: imageCapture ? [] : [ scanner ]
Image {
id: previewImage
fillMode: Image.PreserveAspectFit
width: 128
height: 128
anchors.left: parent.left
anchors.top: parent.top
opacity: 0.75
}
MouseArea {
anchors.fill: parent
onClicked: {
console.debug("*** Camera click")
if (scanOnly)
return;
captureImage();
}
onPressAndHold: {
console.debug("*** Camera press'n'hold")
if (camera.lockStatus==Camera.Unlocked)
camera.searchAndLock();
else
camera.unlock();
}
}
BusyIndicator {
anchors.centerIn: parent
visible: running
running: camera.lockStatus==Camera.Searching
}
}
Text {
id: barcodeText
Layout.fillWidth: true
color: "red"
text: cameraItem.barcode
font.pointSize: 22
horizontalAlignment: Text.AlignHCenter
}
}
Popup {
id: cameraPopup
modal: true
width: parent.width/2
height: parent.height/2
ListView {
id: cameraList
anchors.fill: parent
clip: true
model: QtMultimedia.availableCameras
delegate: Text {
id: c
text: modelData.displayName
font.bold: true
horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignVCenter
elide: Text.ElideRight
font.pixelSize: 22
MouseArea {
anchors.fill: parent
onClicked: {
camera.deviceId=modelData.deviceId
cameraPopup.close();
}
}
}
}
}
RowLayout {
Layout.fillWidth: true
Layout.minimumHeight: 32
anchors.bottom: parent.bottom
anchors.horizontalCenter: parent.horizontalCenter
Button
{
enabled: camera.cameraStatus==Camera.ActiveStatus && !scanOnly
visible: !scanOnly
Layout.fillHeight: true
Layout.fillWidth: true
text: "Capture"
onClicked: captureImage();
}
Button {
text: "Try again"
Layout.fillHeight: true
Layout.fillWidth: true
visible: !oneShot && scanOnly
enabled: camera.cameraStatus!=Camera.ActiveStatus
onClicked: camera.start()
}
Button {
text: "Cameras"
visible: cameraSelectionEnabled
onClicked: {
cameraPopup.open();
}
}
Button {
text: cameraItem.flash ? "Auto" : "Off"
onClicked: {
cameraItem.flash=!cameraItem.flash
}
}
Button {
text: getFocusTitle(camera.lockStatus)
enabled: camera.cameraStatus==Camera.ActiveStatus && Camera.lockStatus!=Camera.Searching
onClicked: {
if (camera.lockStatus==Camera.Unlocked)
camera.searchAndLock();
else
camera.unlock();
}
Layout.fillHeight: true
Layout.fillWidth: true
function getFocusTitle(cls) {
switch (cls) {
case Camera.Unlocked:
return qsTr("Focus")
case Camera.Searching:
return qsTr("Focusing")
default:
return qsTr("Unlock")
}
}
}
}
Component.onCompleted: {
console.debug("Camera standby")
if (autoStart)
startCamera();
}
Component.onDestruction: {
stopCamera();
}
function captureImage() {
camera.imageCapture.capture();
}
function startCamera() {
console.debug("Start camera")
camera.start();
}
function stopCamera() {
console.debug("Stop camera")
camera.stop();
}
}