-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathmain.dart
More file actions
501 lines (464 loc) · 16.8 KB
/
Copy pathmain.dart
File metadata and controls
501 lines (464 loc) · 16.8 KB
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
import 'dart:io';
import 'package:flutter/material.dart';
import "dart:async";
import 'package:flutter/services.dart';
import "dart:convert";
import 'package:image_picker/image_picker.dart';
/// import the blinkid_flutter package
import 'package:blinkid_flutter/blinkid_flutter.dart';
import 'blinkid_result_builder.dart';
import 'module_settings_panel.dart';
import 'optional_scan_settings_panel.dart';
import 'scanning_modules_config.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String sdkLicenseKey = "";
String resultString = "";
String firstDocumentImageBase64 = "";
String secondDocumentImageBase64 = "";
String faceImageBase64 = "";
String signatureImageBase64 = "";
String firstInputImageBase64 = "";
String secondInputImageBase64 = "";
String barcodeInputImageBase64 = "";
/// Initialize the BlinkID plugin
///
/// It will be used both for the default UX scan (performScan method),
/// and the DirectAPI scan (directApiMultiSideScan and directApiSingleSideScan methods).
final blinkIdPlugin = BlinkIdFlutter();
final _modulesConfig = ScanningModulesConfig();
static const String? _microblinkProxyUrl = null;
BlinkIdSdkSettings _buildSdkSettings() {
final sdkSettings = BlinkIdSdkSettings(
licenseKey: sdkLicenseKey,
microblinkProxyUrl: _microblinkProxyUrl,
downloadResources: true
);
return sdkSettings;
}
BlinkIdSessionSettings _buildSessionSettings() =>
_modulesConfig.toSessionSettings();
void _logScanConfiguration(String action) {
final sessionSettings = _buildSessionSettings();
final scanningSettings = sessionSettings.scanningSettings;
debugPrint('[BlinkIdSample] $action');
debugPrint('[BlinkIdSample] scanningMode: ${sessionSettings.scanningMode}');
debugPrint(
'[BlinkIdSample] stepTimeoutDuration: ${sessionSettings.stepTimeoutDuration}, '
'inactivityTimeoutDuration: ${sessionSettings.inactivityTimeoutDuration}',
);
debugPrint(
'[BlinkIdSample] showOnboardingDialog: ${_modulesConfig.showOnboardingDialog}',
);
debugPrint(
'[BlinkIdSample] modules enabled: '
'documentCapture=${_modulesConfig.documentCaptureEnabled}, '
'barcode=${_modulesConfig.barcodeEnabled}, '
'mrz=${_modulesConfig.mrzEnabled}, '
'viz=${_modulesConfig.vizEnabled}',
);
debugPrint(
'[BlinkIdSample] scanningSettings JSON: ${jsonEncode(scanningSettings.toJson())}',
);
debugPrint(
'[BlinkIdSample] full sessionSettings JSON: ${jsonEncode(sessionSettings.toJson())}',
);
}
@override
void initState() {
super.initState();
/// Add a valid license key, based on the platform
/// A valid license key can be obtained from the Microblink Developer Hub, here: https://developer.microblink.com
if (Platform.isAndroid) {
sdkLicenseKey =
"sRwCABVjb20ubWljcm9ibGluay5zYW1wbGUAbGV5SkRjbVZoZEdWa1QyNGlPakUzTnpreE1ESXpOVGMxT1RBc0lrTnlaV0YwWldSR2IzSWlPaUprWkdRd05qWmxaaTAxT0RJekxUUXdNRGd0T1RRNE1DMDFORFU0WWpBeFlUVTJZamdpZlE9PRXlOs6VFBOfXCx1+6HuENpn05k2kl20pJr4kQ4S1sMxuSzZ+B8YhC9rYMsFXr3HSskFmMFwEe+44OQ1ZE2sm9iHUpxNBmVGpgBTKPOrc2vquGbpqmFwm1feyTL9Aw==";
} else if (Platform.isIOS) {
sdkLicenseKey =
"sRwCABVjb20ubWljcm9ibGluay5zYW1wbGUBbGV5SkRjbVZoZEdWa1QyNGlPakUzTnpreE56VTBNekE0T1Rrc0lrTnlaV0YwWldSR2IzSWlPaUprWkdRd05qWmxaaTAxT0RJekxUUXdNRGd0T1RRNE1DMDFORFU0WWpBeFlUVTJZamdpZlE9PaObKYfb4FlwqmqVoofXLicsmElmnSm1gmoXWaFx8MgdmmJRSLpdAfP6uV5xAr3K4rColEBYQ38GNh+FT081yjXPFB16LwdVhDiJcEK07cTBG5hQPXRy8+hoJJ1U7w==";
}
// If necessary, the SDK can be pre-loaded with the necessary resources before the scanning session starts.
// This will decreasing the SDK loading time when starting a scanning session (since the resources will be downloaded and the license verified).
// blinkIdPlugin.loadBlinkIdSdk(
// blinkidSdkSettings: BlinkIdSdkSettings(licenseKey: sdkLicenseKey),
// );
}
Future<void> performScan() async {
try {
_logScanConfiguration('Scan with camera');
final sdkSettings = _buildSdkSettings();
final sessionSettings = _buildSessionSettings();
final uiSettings = _modulesConfig.toUxSettings();
final classFilter = _modulesConfig.toClassFilter();
final redactionSettingsResolver =
_modulesConfig.toRedactionSettingsResolver();
/// Call the 'performScan' method and handle the results
/// Check how the results are handled in the blinkid_result_builder.dart file
await blinkIdPlugin
.performScan(
blinkIdSdkSettings: sdkSettings,
blinkIdSessionSettings: sessionSettings,
blinkidScanningUxSettings: uiSettings,
classFilter: classFilter,
redactionSettingsResolver: redactionSettingsResolver,
)
.then((result) {
resetImages();
setState(() {
if (result != null) {
resultString = BlinkIdResultBuilder.getIdResultString(result);
setImages(result);
}
});
})
.catchError((scanningError) {
setState(() {
if (scanningError is PlatformException) {
final errorMessage = scanningError.message;
resultString = "BlinkID scanning error: $errorMessage";
resetImages();
}
});
});
} catch (blinkidScanningError) {
if (blinkidScanningError is PlatformException) {
final errorMessage = blinkidScanningError.message;
setState(() {
resultString = "BlinkID scanning error: $errorMessage";
resetImages();
});
}
}
}
Future<void> directApiMultiSideScan() async {
try {
_logScanConfiguration('DirectAPI MultiSide');
/// Get the front and the back side of the document with the pickMultiImage method
/// First select the front and the then back side of the image
final images = await ImagePicker().pickMultiImage();
/// Convert the first picked image to the Base64 format
String frontImageBase64 = base64Encode(await images[0].readAsBytes());
/// Get the second selected image as the back side of the document
/// Convert the picked image to the Base64 format
String backImageBase64 = base64Encode(await images[1].readAsBytes());
final sdkSettings = _buildSdkSettings();
final sessionSettings = _buildSessionSettings();
/// Call the 'performDirectApiScan' method and handle the results
/// Check how the results are handled in the blinkid_result_builder.dart file
await blinkIdPlugin
.performDirectApiScan(
blinkIdSdkSettings: sdkSettings,
blinkIdSessionSettings: sessionSettings,
firstImage: frontImageBase64,
secondImage: backImageBase64,
)
.then((result) {
setState(() {
resetImages();
if (result != null) {
resultString = BlinkIdResultBuilder.getIdResultString(result);
setImages(result);
}
});
})
.catchError((scanningError) {
setState(() {
if (scanningError is PlatformException) {
final errorMessage = scanningError.message;
resultString = "BlinkID scanning error: $errorMessage";
resetImages();
}
});
});
} catch (blinkidScanningError) {
if (blinkidScanningError is PlatformException) {
final errorMessage = blinkidScanningError.message;
setState(() {
resultString = "BlinkID scanning error: $errorMessage";
resetImages();
});
}
}
}
Future<void> directApiSingleSideScan() async {
try {
_logScanConfiguration('DirectAPI SingleSide');
/// Get either the front or the back side of the document with the pickImage method
final image = await ImagePicker().pickImage(source: ImageSource.gallery);
if (image == null) return;
/// Convert the picked image to the Base64 format
String imageBase64 = base64Encode(await image.readAsBytes());
final sdkSettings = _buildSdkSettings();
final sessionSettings = _buildSessionSettings();
/// Call the 'performDirectApiScan' method and handle the results
/// Check how the results are handled in the blinkid_result_builder.dart file
await blinkIdPlugin
.performDirectApiScan(
blinkIdSdkSettings: sdkSettings,
blinkIdSessionSettings: sessionSettings,
firstImage: imageBase64,
)
.then((result) {
setState(() {
resetImages();
if (result != null) {
resultString = BlinkIdResultBuilder.getIdResultString(result);
setImages(result);
}
});
})
.catchError((scanningError) {
setState(() {
if (scanningError is PlatformException) {
final errorMessage = scanningError.message;
resultString = "BlinkID scanning error: $errorMessage";
resetImages();
}
});
});
} catch (blinkidScanningError) {
if (blinkidScanningError is PlatformException) {
final errorMessage = blinkidScanningError.message;
setState(() {
resultString = "BlinkID scanning error: $errorMessage";
resetImages();
});
}
}
}
void setImages(BlinkIdScanningResult? result) {
if (result?.firstDocumentImage != null) {
firstDocumentImageBase64 = result!.firstDocumentImage!;
}
if (result?.secondDocumentImage != null) {
secondDocumentImageBase64 = result!.secondDocumentImage!;
}
if (result?.faceImage != null) {
faceImageBase64 = result!.faceImage!.image!;
}
if (result?.signatureImage != null) {
signatureImageBase64 = result!.signatureImage!.image!;
}
if (result?.firstInputImage != null) {
firstInputImageBase64 = result!.firstInputImage!;
}
if (result?.secondInputImage != null) {
secondInputImageBase64 = result!.secondInputImage!;
}
if (result?.barcodeInputImage != null) {
barcodeInputImageBase64 = result!.barcodeInputImage!;
}
}
void resetImages() {
firstDocumentImageBase64 = "";
secondDocumentImageBase64 = "";
firstInputImageBase64 = "";
secondInputImageBase64 = "";
faceImageBase64 = "";
signatureImageBase64 = "";
barcodeInputImageBase64 = "";
}
@override
Widget build(BuildContext context) {
Widget firstDocumentImage = Container();
if (firstDocumentImageBase64 != "") {
firstDocumentImage = Column(
children: <Widget>[
Text("First document image:"),
Image.memory(
Base64Decoder().convert(firstDocumentImageBase64),
height: 180,
width: 350,
),
],
);
}
Widget secondDocumentImage = Container();
if (secondDocumentImageBase64 != "") {
secondDocumentImage = Column(
children: <Widget>[
Text("Second document image:"),
Image.memory(
Base64Decoder().convert(secondDocumentImageBase64),
height: 180,
width: 350,
),
],
);
}
Widget firstInputImage = Container();
if (firstInputImageBase64 != "") {
firstInputImage = Column(
children: <Widget>[
Text("First input image:"),
Image.memory(
Base64Decoder().convert(firstInputImageBase64),
height: 180,
width: 350,
),
],
);
}
Widget secondInputImage = Container();
if (secondInputImageBase64 != "") {
secondInputImage = Column(
children: <Widget>[
Text("Second input image:"),
Image.memory(
Base64Decoder().convert(secondInputImageBase64),
height: 180,
width: 350,
),
],
);
}
Widget barcodeInputImage = Container();
if (barcodeInputImageBase64 != "") {
barcodeInputImage = Column(
children: <Widget>[
Text("Barcode input image:"),
Image.memory(
Base64Decoder().convert(barcodeInputImageBase64),
height: 180,
width: 350,
),
],
);
}
Widget faceImage = Container();
if (faceImageBase64 != "") {
faceImage = Column(
children: <Widget>[
Text("Face Image:"),
Image.memory(
Base64Decoder().convert(faceImageBase64),
height: 150,
width: 100,
),
],
);
}
Widget signatureImage = Container();
if (signatureImageBase64 != "") {
signatureImage = Column(
children: <Widget>[
Text("Signature Image:"),
Image.memory(
Base64Decoder().convert(signatureImageBase64),
height: 150,
width: 100,
),
],
);
}
Future<void> showAlertDialog(
BuildContext context,
String title,
String message,
) async {
await showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text(title),
content: Text(message),
actions: <Widget>[
TextButton(
onPressed: () {
Navigator.of(context).pop();
},
child: Text('OK'),
),
],
);
},
);
}
return MaterialApp(
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: const Color(0xFF1565C0)),
useMaterial3: true,
),
home: Scaffold(
appBar: AppBar(title: const Text("BlinkID Sample")),
body: GestureDetector(
onTap: () => FocusManager.instance.primaryFocus?.unfocus(),
behavior: HitTestBehavior.translucent,
child: SingleChildScrollView(
keyboardDismissBehavior:
ScrollViewKeyboardDismissBehavior.onDrag,
padding: const EdgeInsets.all(16.0),
child: Builder(
builder: (BuildContext context) {
return Column(
children: <Widget>[
ModuleSettingsPanel(
config: _modulesConfig,
onChanged: () => setState(() {}),
),
OptionalScanSettingsPanel(
config: _modulesConfig,
onChanged: () => setState(() {}),
),
const SizedBox(height: 8),
Padding(
padding: const EdgeInsets.only(bottom: 16.0),
child: ElevatedButton(
onPressed: () => performScan(),
child: Text("Scan with camera"),
),
),
Padding(
padding: EdgeInsets.only(bottom: 16.0),
child: ElevatedButton(
onPressed: () {
showAlertDialog(
context,
'DirectAPI MultiSide instructions',
'Select two images for processing.\nThe first selected image needs to be front side of the document.\nThe second image needs to be the back side of the document.',
).then((_) {
directApiMultiSideScan();
});
},
child: Text("DirectAPI MultiSide"),
),
),
Padding(
padding: EdgeInsets.only(bottom: 16.0),
child: ElevatedButton(
onPressed: () {
showAlertDialog(
context,
'DirectAPI SingleSide instructions',
'Select one image for processing.\nThe image can be either the front or the back side of the document.',
).then((_) {
directApiSingleSideScan();
});
},
child: Text("DirectAPI SingleSide"),
),
),
Text(resultString),
firstDocumentImage,
secondDocumentImage,
firstInputImage,
secondInputImage,
barcodeInputImage,
faceImage,
signatureImage,
],
);
},
),
),
),
),
);
}
}