-
Notifications
You must be signed in to change notification settings - Fork 12
/
main.dart
306 lines (270 loc) · 8.1 KB
/
main.dart
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
//
// main.dart
// DocumentReader
//
// Created by Pavel Masiuk on 21.09.2023.
// Copyright © 2023 Regula. All rights reserved.
//
import 'package:flutter/material.dart';
import 'package:flutter_document_reader_api_example/extra/rfid_custom_ui.dart';
import 'dart:async';
import 'package:image_picker/image_picker.dart';
import 'package:flutter/services.dart';
import 'package:flutter_document_reader_api_beta/flutter_document_reader_api.dart';
void main() => runApp(MyApp());
class MyAppState extends State<MyApp> {
var documentReader = DocumentReader.instance;
var status = "Loading...";
var portrait = Image.asset('assets/images/portrait.png');
var docImage = Image.asset('assets/images/id.png');
var selectedScenario = Scenario.MRZ;
List<DocReaderScenario> scenarios = [];
Object setStatus(String s) => {setState(() => status = s)};
var doRfid = false;
var isReadingRfid = false;
var rfidCustomUiExample = RFIDCustomUI.empty();
var rfidOption = RfidOption.Basic;
var colorPrimary = Colors.blue;
void init() async {
super.initState();
if (!await initializeReader()) return;
setStatus("Ready");
setState(() => scenarios = documentReader.availableScenarios);
}
void handleCompletion(
DocReaderAction action, Results? results, DocReaderException? error) {
if (error != null) print(error.message);
if (action.stopped() && !shouldRfid(results)) {
displayResults(results);
} else if (action.finished() && shouldRfid(results)) {
readRfid();
}
}
void displayResults(Results? results) async {
isReadingRfid = false;
clearResults();
if (results == null) return;
var name =
await results.textFieldValueByType(FieldType.SURNAME_AND_GIVEN_NAMES);
var newDocImage =
await results.graphicFieldImageByType(GraphicFieldType.DOCUMENT_IMAGE);
var newPortrait =
await results.graphicFieldImageByType(GraphicFieldType.PORTRAIT);
setState(() {
status = name ?? "Ready";
if (newDocImage != null) docImage = Image.memory(newDocImage);
if (newPortrait != null) portrait = Image.memory(newPortrait);
});
}
void clearResults() {
setState(() {
status = "Ready";
docImage = Image.asset('assets/images/id.png');
portrait = Image.asset('assets/images/portrait.png');
});
}
void readRfid() {
isReadingRfid = true;
if (rfidOption == RfidOption.Basic) basicRfid();
if (rfidOption == RfidOption.Advanced) advancedRfid();
if (rfidOption == RfidOption.Custom) rfidCustomUiExample.run();
}
void basicRfid() {
documentReader.rfid(RFIDConfig(handleCompletion));
}
void advancedRfid() {
var config = RFIDConfig(handleCompletion);
config.onChipDetected = () => print("Chip detected, reading rfid.");
config.onRetryReadChip = (error) async {
var message = await error.code.getTranslation();
print("Reading interrupted: $message. Retrying...");
};
documentReader.rfid(config);
}
bool shouldRfid(Results? results) =>
doRfid && !isReadingRfid && results != null && results.chipPage != 0;
Widget ui() {
return Column(
children: [
documentImages(),
scenarioSelector(),
rfidCheckbox(),
scanButtons()
],
);
}
Widget documentImages() {
return Container(
padding: const EdgeInsets.only(top: 10, bottom: 10),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
image("Portrait", 150, 150, portrait.image),
image("Document image", 150, 200, docImage.image),
],
),
);
}
Widget scenarioSelector() {
return Expanded(
child: Container(
color: const Color.fromARGB(5, 10, 10, 10),
child: ListView.builder(
itemCount: scenarios.length,
itemBuilder: (_, int index) => radioButton(index),
),
),
);
}
Widget rfidCheckbox() {
var rfidCheckboxTitle = "Process rfid reading";
if (!documentReader.isRFIDAvailableForUse) {
rfidCheckboxTitle += " (unavailable)";
}
return CheckboxListTile(
value: doRfid,
title: Text(rfidCheckboxTitle),
onChanged: (bool? value) {
setState(() => doRfid = value! && documentReader.isRFIDAvailableForUse);
},
);
}
Widget scanButtons() {
return Container(
padding: const EdgeInsets.only(top: 10, bottom: 25),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
button("Scan document", () {
clearResults();
documentReader.scan(
ScannerConfig.withScenario(selectedScenario),
handleCompletion,
);
}),
button("Scan image", () async {
clearResults();
documentReader.recognize(
RecognizeConfig.withScenario(
selectedScenario,
RecognizeData.withImages(await getImages()),
),
handleCompletion,
);
})
],
),
);
}
Widget image(
String title,
double height,
double width,
ImageProvider image,
) {
return Column(
children: <Widget>[
Text(title),
Image(
height: height,
width: width,
image: image,
)
],
);
}
Widget radioButton(int index) {
Radio radio = Radio(
value: scenarios[index].name,
groupValue: selectedScenario.value,
onChanged: (value) => setState(() {
selectedScenario = Scenario.getByValue(value)!;
}),
);
return Container(
padding: const EdgeInsets.only(left: 40),
child: ListTile(
leading: radio,
title: GestureDetector(
onTap: () => radio.onChanged!(scenarios[index].name),
child: Text(scenarios[index].caption),
),
),
);
}
Widget button(String text, VoidCallback onPress) {
return Container(
width: 160,
height: 40,
padding: const EdgeInsets.fromLTRB(5, 0, 5, 0),
transform: Matrix4.translationValues(0, -7.5, 0),
child: TextButton(
style: ButtonStyle(
foregroundColor: MaterialStateProperty.all<Color>(colorPrimary),
backgroundColor: MaterialStateProperty.all<Color>(Colors.black12),
),
onPressed: onPress,
child: Text(text),
),
);
}
Future<bool> initializeReader() async {
setStatus("Initializing...");
ByteData license = await rootBundle.load("assets/regula.license");
var initConfig = InitConfig(license);
initConfig.delayedNNLoad = true;
var (success, error) = await documentReader.initializeReader(initConfig);
if (!success) {
setStatus(error!.message);
printError(error);
}
rfidCustomUiExample = RFIDCustomUI(setState, setStatus, displayResults);
return success;
}
void printError(DocReaderException error) =>
print("Error: \n code: ${error.code}\n message: ${error.message}");
Future<List<Uint8List>> getImages() async {
setStatus("Processing image...");
List<XFile> files = await ImagePicker().pickMultiImage();
List<Uint8List> result = [];
for (XFile file in files) {
result.add(await file.readAsBytes());
}
return result;
}
@override
Widget build(_) {
final ThemeData theme = ThemeData();
return MaterialApp(
theme: theme.copyWith(
colorScheme: theme.colorScheme
.copyWith(primary: colorPrimary, surfaceTint: colorPrimary),
),
home: Scaffold(
appBar: AppBar(title: Center(child: Text(status))),
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
rfidCustomUiExample.build(),
Visibility(
visible: !rfidCustomUiExample.isShowing,
child: Expanded(
child: ui(),
),
)
],
),
),
);
}
@override
void initState() {
super.initState();
init();
}
}
enum RfidOption { Basic, Advanced, Custom }
class MyApp extends StatefulWidget {
@override
MyAppState createState() => MyAppState();
}