-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathdynamsoft_barcode.dart
More file actions
373 lines (291 loc) · 10.8 KB
/
dynamsoft_barcode.dart
File metadata and controls
373 lines (291 loc) · 10.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
import 'dart:typed_data';
/// Represents the result of a barcode scan, including format, text, coordinates, and angle.
///
/// This class stores barcode details such as the decoded text, format, bounding box coordinates,
/// rotation angle, and raw barcode bytes.
///
/// ### Example Usage:
/// ```dart
/// BarcodeResult result = BarcodeResult.fromJson(jsonData);
/// print(result.text);
/// ```
class BarcodeResult {
/// Barcode format (e.g., "QR Code", "Code 128").
final String format;
/// Decoded text from the barcode.
final String text;
/// X-coordinate of the first corner point.
final int x1;
/// Y-coordinate of the first corner point.
final int y1;
/// X-coordinate of the second corner point.
final int x2;
/// Y-coordinate of the second corner point.
final int y2;
/// X-coordinate of the third corner point.
final int x3;
/// Y-coordinate of the third corner point.
final int y3;
/// X-coordinate of the fourth corner point.
final int x4;
/// Y-coordinate of the fourth corner point.
final int y4;
/// Rotation angle of the barcode in degrees.
final int angle;
/// Raw barcode data in bytes.
final Uint8List barcodeBytes;
/// Error code.
final int errorCode;
/// Error message.
final String errorMsg;
/// Constructs a [BarcodeResult] with the given properties.
BarcodeResult(
this.format,
this.text,
this.x1,
this.y1,
this.x2,
this.y2,
this.x3,
this.y3,
this.x4,
this.y4,
this.angle,
this.barcodeBytes,
this.errorCode,
this.errorMsg,
);
/// Creates a [BarcodeResult] instance from a JSON object.
factory BarcodeResult.fromJson(Map<dynamic, dynamic> json) {
String format = json['format'];
int x1 = json['x1'];
int y1 = json['y1'];
int x2 = json['x2'];
int y2 = json['y2'];
int x3 = json['x3'];
int y3 = json['y3'];
int x4 = json['x4'];
int y4 = json['y4'];
int angle = json['angle'];
int errorCode = json['errorCode'];
String errorMsg = json['errorMsg'];
// Convert barcode byte data
List<Object?> rawBytes = json['barcodeBytes'];
Uint8List barcodeBytes =
Uint8List.fromList(rawBytes.map((e) => e as int).toList());
String text = String.fromCharCodes(barcodeBytes);
return BarcodeResult(
format,
text,
x1,
y1,
x2,
y2,
x3,
y3,
x4,
y4,
angle,
barcodeBytes,
errorCode,
errorMsg,
);
}
/// Converts this object to a JSON-compatible [Map].
Map<String, dynamic> toJson() => {
'format': format,
'text': text,
'x1': x1,
'y1': y1,
'x2': x2,
'y2': y2,
'x3': x3,
'y3': y3,
'x4': x4,
'y4': y4,
'angle': angle,
'barcodeBytes': barcodeBytes,
'errorCode': errorCode,
'errorMsg': errorMsg,
};
}
/// Converts a list of barcode scan results from JSON format to [BarcodeResult] objects.
///
/// This function is used to transform the decoded barcode results into a list of
/// [BarcodeResult] instances.
///
/// ### Example Usage:
/// ```dart
/// List<BarcodeResult> results = convertResults(jsonData);
/// print(results[0].text);
/// ```
List<BarcodeResult> convertResults(List<Map<dynamic, dynamic>> jsonResults) {
return jsonResults.map((data) => BarcodeResult.fromJson(data)).toList();
}
/// Defines supported barcode format constants for use with
/// [FlutterBarcodeSdk.setBarcodeFormats].
///
/// These constants can be combined using the bitwise OR operator (`|`) to
/// specify multiple barcode formats simultaneously.
///
/// ### Example
/// ```dart
/// // Detect only QR Code and Code 128
/// await barcodeReader.setBarcodeFormats(
/// BarcodeFormat.QR_CODE | BarcodeFormat.CODE_128,
/// );
/// ```
///
/// ### Web Platform Support
///
/// On web, barcode format values are automatically converted to JavaScript
/// `BigInt` to preserve their full 64-bit precision. This allows formats
/// with values exceeding `0x7FFFFFFF` (such as postal codes, DotCode, and
/// Pharmacode) to work correctly.
///
/// **Note:** When combining formats using Dart's bitwise OR operator (`|`)
/// in your code, the operation is performed in Dart before being sent to
/// JavaScript, so all formats can be safely combined regardless of their
/// magnitude.
class BarcodeFormat {
BarcodeFormat._();
// ---------------------------------------------------------------------------
// Special / Aggregate Values
// ---------------------------------------------------------------------------
/// No barcode format.
static const int NULL = 0x00;
/// All supported barcode formats (`0xFFFFFFFEFFFFFFFF` unsigned).
///
/// This uses the signed 64-bit equivalent so that the constant compiles
/// on both native and web platforms.
static const int ALL = -4294967297; // 0xFFFFFFFEFFFFFFFF
/// Default barcode format set covering the most commonly used symbologies.
static const int DEFAULT = 0xFE3BFFFF;
/// Combined value of all 1D (linear) barcode formats: [CODE_39],
/// [CODE_128], [CODE_93], [CODABAR], [ITF], [EAN_13], [EAN_8], [UPC_A],
/// [UPC_E], [INDUSTRIAL_25], [CODE_39_EXTENDED], [MSI_CODE], [CODE_11].
static const int ONED = 0x003007FF;
/// Combined value of all GS1 DataBar formats: [GS1_DATABAR_OMNIDIRECTIONAL],
/// [GS1_DATABAR_TRUNCATED], [GS1_DATABAR_STACKED],
/// [GS1_DATABAR_STACKED_OMNIDIRECTIONAL], [GS1_DATABAR_EXPANDED],
/// [GS1_DATABAR_EXPANDED_STACKED], [GS1_DATABAR_LIMITED].
static const int GS1_DATABAR = 0x0003F800;
/// Combined value of all postal code formats: [USPSINTELLIGENTMAIL],
/// [POSTNET], [PLANET], [AUSTRALIANPOST], [RM4SCC], [KIX].
static const int POSTALCODE = 0x3F0000000000000;
/// Combined value of [PHARMACODE_ONE_TRACK] and [PHARMACODE_TWO_TRACK].
static const int PHARMACODE = 0xC00000000;
// ---------------------------------------------------------------------------
// 1D / Linear Barcodes
// ---------------------------------------------------------------------------
/// Code 39 barcode format.
static const int CODE_39 = 0x1;
/// Code 128 barcode format.
static const int CODE_128 = 0x2;
/// Code 93 barcode format.
static const int CODE_93 = 0x4;
/// Codabar barcode format.
static const int CODABAR = 0x8;
/// Interleaved 2 of 5 (ITF) barcode format.
static const int ITF = 0x10;
/// EAN-13 barcode format.
static const int EAN_13 = 0x20;
/// EAN-8 barcode format.
static const int EAN_8 = 0x40;
/// UPC-A barcode format.
static const int UPC_A = 0x80;
/// UPC-E barcode format.
static const int UPC_E = 0x100;
/// Industrial 2 of 5 barcode format.
static const int INDUSTRIAL_25 = 0x200;
/// Code 39 Extended barcode format.
static const int CODE_39_EXTENDED = 0x400;
/// Code 32 barcode format (Italian Pharmacode).
static const int CODE_32 = 0x1000000;
/// MSI (Modified Plessey) barcode format.
static const int MSI_CODE = 0x100000;
/// Code 11 barcode format.
static const int CODE_11 = 0x200000;
/// Two-digit add-on supplement for UPC/EAN barcodes.
static const int TWO_DIGIT_ADD_ON = 0x400000;
/// Five-digit add-on supplement for UPC/EAN barcodes.
static const int FIVE_DIGIT_ADD_ON = 0x800000;
/// Matrix 2 of 5 barcode format.
static const int MATRIX_25 = 0x1000000000;
/// Telepen barcode format.
///
/// Designed primarily for library and membership systems, Telepen can
/// encode the full ASCII character set.
static const int TELEPEN = 0x2000000000;
/// Telepen Numeric barcode format.
///
/// A variation of Telepen optimized for encoding numeric data only.
static const int TELEPEN_NUMERIC = 0x4000000000;
// ---------------------------------------------------------------------------
// GS1 DataBar Formats
// ---------------------------------------------------------------------------
/// GS1 DataBar Omnidirectional barcode format.
static const int GS1_DATABAR_OMNIDIRECTIONAL = 0x800;
/// GS1 DataBar Truncated barcode format.
static const int GS1_DATABAR_TRUNCATED = 0x1000;
/// GS1 DataBar Stacked barcode format.
static const int GS1_DATABAR_STACKED = 0x2000;
/// GS1 DataBar Stacked Omnidirectional barcode format.
static const int GS1_DATABAR_STACKED_OMNIDIRECTIONAL = 0x4000;
/// GS1 DataBar Expanded barcode format.
static const int GS1_DATABAR_EXPANDED = 0x8000;
/// GS1 DataBar Expanded Stacked barcode format.
static const int GS1_DATABAR_EXPANDED_STACKED = 0x10000;
/// GS1 DataBar Limited barcode format.
static const int GS1_DATABAR_LIMITED = 0x20000;
// ---------------------------------------------------------------------------
// 2D Barcodes
// ---------------------------------------------------------------------------
/// PDF417 barcode format.
static const int PDF417 = 0x02000000;
/// QR Code barcode format.
static const int QR_CODE = 0x04000000;
/// DataMatrix barcode format.
static const int DATAMATRIX = 0x08000000;
/// Aztec Code barcode format.
static const int AZTEC = 0x10000000;
/// MaxiCode barcode format (modes 2–5).
static const int MAXICODE = 0x20000000;
/// Micro QR Code barcode format.
static const int MICRO_QR = 0x40000000;
/// Micro PDF417 barcode format.
static const int MICRO_PDF417 = 0x00080000;
/// GS1 Composite Code barcode format.
static const int GS1_COMPOSITE = 0x80000000;
/// DotCode barcode format.
static const int DOTCODE = 0x200000000;
// ---------------------------------------------------------------------------
// Patch / Special Codes
// ---------------------------------------------------------------------------
/// Patch Code barcode format.
static const int PATCHCODE = 0x00040000;
/// Non-standard barcode format.
static const int NONSTANDARD_BARCODE = 0x100000000;
// ---------------------------------------------------------------------------
// Postal Codes
// ---------------------------------------------------------------------------
/// USPS Intelligent Mail barcode format.
static const int USPSINTELLIGENTMAIL = 0x10000000000000;
/// POSTNET barcode format.
static const int POSTNET = 0x20000000000000;
/// PLANET barcode format.
static const int PLANET = 0x40000000000000;
/// Australia Post barcode format.
static const int AUSTRALIANPOST = 0x80000000000000;
/// Royal Mail 4-State Customer Code (RM4SCC) barcode format.
static const int RM4SCC = 0x100000000000000;
/// KIX (Klant Index) barcode format used by Royal Dutch TPG Post.
static const int KIX = 0x200000000000000;
// ---------------------------------------------------------------------------
// Pharmacode
// ---------------------------------------------------------------------------
/// Pharmacode (one-track) barcode format.
static const int PHARMACODE_ONE_TRACK = 0x400000000;
/// Pharmacode (two-track) barcode format.
static const int PHARMACODE_TWO_TRACK = 0x800000000;
}