Showing
2 changed files
with
82 additions
and
122 deletions
| @@ -27,9 +27,6 @@ Bugs fixed: | @@ -27,9 +27,6 @@ Bugs fixed: | ||
| 27 | * Fixed scan timeout not working on iOS. (thanks @navaronbracke !) | 27 | * Fixed scan timeout not working on iOS. (thanks @navaronbracke !) |
| 28 | * Fixed a crash on iOS when the device is nil. (thanks @navaronbracke !) | 28 | * Fixed a crash on iOS when the device is nil. (thanks @navaronbracke !) |
| 29 | * Fixed a case of an unhandled exception when starting the scanner. (thanks @navaronbracke !) | 29 | * Fixed a case of an unhandled exception when starting the scanner. (thanks @navaronbracke !) |
| 30 | -* Fixed the default values for the `format` and `type` arguments of the Barcode constructor. | ||
| 31 | - These now use `BarcodeFormat.unknown` and `BarcodeType.unknown`, rather than `BarcodeFormat.ean13` and `BarcodeType.text`. | ||
| 32 | - (thanks @navaronbracke !) | ||
| 33 | 30 | ||
| 34 | Improvements: | 31 | Improvements: |
| 35 | * Improved MacOS memory footprint by using a background queue. (thanks @ryanduffyne !) | 32 | * Improved MacOS memory footprint by using a background queue. (thanks @ryanduffyne !) |
| 1 | import 'dart:typed_data'; | 1 | import 'dart:typed_data'; |
| 2 | import 'dart:ui'; | 2 | import 'dart:ui'; |
| 3 | 3 | ||
| 4 | +import 'package:mobile_scanner/src/barcode_utility.dart'; | ||
| 4 | import 'package:mobile_scanner/src/enums/barcode_format.dart'; | 5 | import 'package:mobile_scanner/src/enums/barcode_format.dart'; |
| 5 | import 'package:mobile_scanner/src/enums/barcode_type.dart'; | 6 | import 'package:mobile_scanner/src/enums/barcode_type.dart'; |
| 6 | import 'package:mobile_scanner/src/objects/calendar_event.dart'; | 7 | import 'package:mobile_scanner/src/objects/calendar_event.dart'; |
| @@ -15,147 +16,109 @@ import 'package:mobile_scanner/src/objects/wifi.dart'; | @@ -15,147 +16,109 @@ import 'package:mobile_scanner/src/objects/wifi.dart'; | ||
| 15 | 16 | ||
| 16 | /// Represents a single recognized barcode and its value. | 17 | /// Represents a single recognized barcode and its value. |
| 17 | class Barcode { | 18 | class Barcode { |
| 18 | - /// Creates a new [Barcode] instance. | ||
| 19 | - const Barcode({ | ||
| 20 | - this.calendarEvent, | ||
| 21 | - this.contactInfo, | ||
| 22 | - this.corners = const <Offset>[], | ||
| 23 | - this.displayValue, | ||
| 24 | - this.driverLicense, | ||
| 25 | - this.email, | ||
| 26 | - this.format = BarcodeFormat.unknown, | ||
| 27 | - this.geoPoint, | ||
| 28 | - this.phone, | ||
| 29 | - this.rawBytes, | ||
| 30 | - this.rawValue, | ||
| 31 | - this.sms, | ||
| 32 | - this.type = BarcodeType.unknown, | ||
| 33 | - this.url, | ||
| 34 | - this.wifi, | ||
| 35 | - }); | 19 | + /// Returns four corner points in clockwise direction starting with top-left. |
| 20 | + /// | ||
| 21 | + /// Due to the possible perspective distortions, this is not necessarily a rectangle. | ||
| 22 | + /// | ||
| 23 | + /// Returns null if the corner points can not be determined. | ||
| 24 | + final List<Offset> corners; | ||
| 36 | 25 | ||
| 37 | - /// Creates a new [Barcode] instance from the given [data]. | ||
| 38 | - factory Barcode.fromNative(Map<Object?, Object?> data) { | ||
| 39 | - final Map<Object?, Object?>? calendarEvent = | ||
| 40 | - data['calendarEvent'] as Map<Object?, Object?>?; | ||
| 41 | - final List<Object?>? corners = data['corners'] as List<Object?>?; | ||
| 42 | - final Map<Object?, Object?>? contactInfo = | ||
| 43 | - data['contactInfo'] as Map<Object?, Object?>?; | ||
| 44 | - final Map<Object?, Object?>? driverLicense = | ||
| 45 | - data['driverLicense'] as Map<Object?, Object?>?; | ||
| 46 | - final Map<Object?, Object?>? email = | ||
| 47 | - data['email'] as Map<Object?, Object?>?; | ||
| 48 | - final Map<Object?, Object?>? geoPoint = | ||
| 49 | - data['geoPoint'] as Map<Object?, Object?>?; | ||
| 50 | - final Map<Object?, Object?>? phone = | ||
| 51 | - data['phone'] as Map<Object?, Object?>?; | ||
| 52 | - final Map<Object?, Object?>? sms = data['sms'] as Map<Object?, Object?>?; | ||
| 53 | - final Map<Object?, Object?>? url = data['url'] as Map<Object?, Object?>?; | ||
| 54 | - final Map<Object?, Object?>? wifi = data['wifi'] as Map<Object?, Object?>?; | ||
| 55 | - | ||
| 56 | - return Barcode( | ||
| 57 | - calendarEvent: calendarEvent == null | ||
| 58 | - ? null | ||
| 59 | - : CalendarEvent.fromNative(calendarEvent), | ||
| 60 | - contactInfo: | ||
| 61 | - contactInfo == null ? null : ContactInfo.fromNative(contactInfo), | ||
| 62 | - corners: corners == null | ||
| 63 | - ? const <Offset>[] | ||
| 64 | - : List.unmodifiable( | ||
| 65 | - corners.cast<Map<String, double>>().map((Map<String, double> e) { | ||
| 66 | - return Offset(e['x']!, e['y']!); | ||
| 67 | - }), | ||
| 68 | - ), | ||
| 69 | - displayValue: data['displayValue'] as String?, | ||
| 70 | - driverLicense: driverLicense == null | ||
| 71 | - ? null | ||
| 72 | - : DriverLicense.fromNative(driverLicense), | ||
| 73 | - email: email == null ? null : Email.fromNative(email), | ||
| 74 | - format: BarcodeFormat.fromRawValue(data['format'] as int? ?? -1), | ||
| 75 | - geoPoint: geoPoint == null ? null : GeoPoint.fromNative(geoPoint), | ||
| 76 | - phone: phone == null ? null : Phone.fromNative(phone), | ||
| 77 | - rawBytes: data['rawBytes'] as Uint8List?, | ||
| 78 | - rawValue: data['rawValue'] as String?, | ||
| 79 | - sms: sms == null ? null : SMS.fromNative(sms), | ||
| 80 | - type: BarcodeType.fromRawValue(data['type'] as int? ?? 0), | ||
| 81 | - url: url == null ? null : UrlBookmark.fromNative(url), | ||
| 82 | - wifi: wifi == null ? null : WiFi.fromNative(wifi), | ||
| 83 | - ); | ||
| 84 | - } | ||
| 85 | - | ||
| 86 | - /// The calendar event that is embedded in the barcode. | ||
| 87 | - final CalendarEvent? calendarEvent; | 26 | + /// Returns barcode format |
| 27 | + final BarcodeFormat format; | ||
| 88 | 28 | ||
| 89 | - /// The contact information that is embedded in the barcode. | ||
| 90 | - final ContactInfo? contactInfo; | 29 | + /// Returns raw bytes as it was encoded in the barcode. |
| 30 | + /// | ||
| 31 | + /// Returns null if the raw bytes can not be determined. | ||
| 32 | + final Uint8List? rawBytes; | ||
| 91 | 33 | ||
| 92 | - /// The four corner points of the barcode, | ||
| 93 | - /// in clockwise order, starting with the top-left point. | 34 | + /// Returns barcode value as it was encoded in the barcode. Structured values are not parsed, for example: 'MEBKM:TITLE:Google;URL://www.google.com;;'. |
| 94 | /// | 35 | /// |
| 95 | - /// Due to the possible perspective distortions, this is not necessarily a rectangle. | 36 | + /// It's only available when the barcode is encoded in the UTF-8 format, and for non-UTF8 ones use [rawBytes] instead. |
| 96 | /// | 37 | /// |
| 97 | - /// This list is empty if the corners can not be determined. | ||
| 98 | - final List<Offset> corners; | 38 | + /// Returns null if the raw value can not be determined. |
| 39 | + final String? rawValue; | ||
| 99 | 40 | ||
| 100 | - /// The barcode value in a user-friendly format. | 41 | + /// Returns barcode value in a user-friendly format. |
| 101 | /// | 42 | /// |
| 102 | - /// This value may omit some of the information encoded in the barcode. | ||
| 103 | - /// For example, if [rawValue] returns `MEBKM:TITLE:Google;URL://www.google.com;;`, | ||
| 104 | - /// the display value might be `//www.google.com`. | 43 | + /// This method may omit some of the information encoded in the barcode. For example, if [rawValue] returns 'MEBKM:TITLE:Google;URL://www.google.com;;', the display value might be '//www.google.com'. |
| 105 | /// | 44 | /// |
| 106 | - /// This value may be multiline if line breaks are encoded in the barcode. | ||
| 107 | - /// This value may include the supplement value. | 45 | + /// This value may be multiline, for example, when line breaks are encoded into the original TEXT barcode value. May include the supplement value. |
| 108 | /// | 46 | /// |
| 109 | - /// This is null if there is no user-friendly value for the given barcode. | 47 | + /// Returns null if nothing found. |
| 110 | final String? displayValue; | 48 | final String? displayValue; |
| 111 | 49 | ||
| 112 | - /// The driver license information that is embedded in the barcode. | 50 | + /// Returns format type of the barcode value. |
| 51 | + /// | ||
| 52 | + /// For example, TYPE_TEXT, TYPE_PRODUCT, TYPE_URL, etc. | ||
| 53 | + /// | ||
| 54 | + /// If the value structure cannot be parsed, TYPE_TEXT will be returned. If the recognized structure type is not defined in your current version of SDK, TYPE_UNKNOWN will be returned. | ||
| 55 | + /// | ||
| 56 | + /// Note that the built-in parsers only recognize a few popular value structures. For your specific use case, you might want to directly consume rawValue and implement your own parsing logic. | ||
| 57 | + final BarcodeType type; | ||
| 58 | + | ||
| 59 | + /// Gets parsed calendar event details. | ||
| 60 | + final CalendarEvent? calendarEvent; | ||
| 61 | + | ||
| 62 | + /// Gets parsed contact details. | ||
| 63 | + final ContactInfo? contactInfo; | ||
| 64 | + | ||
| 65 | + /// Gets parsed driver license details. | ||
| 113 | final DriverLicense? driverLicense; | 66 | final DriverLicense? driverLicense; |
| 114 | 67 | ||
| 115 | - /// The email message that is embedded in the barcode. | 68 | + /// Gets parsed email details. |
| 116 | final Email? email; | 69 | final Email? email; |
| 117 | 70 | ||
| 118 | - /// The format of the barcode. | ||
| 119 | - final BarcodeFormat format; | ||
| 120 | - | ||
| 121 | - /// The geographic point that is embedded in the barcode. | 71 | + /// Gets parsed geo coordinates. |
| 122 | final GeoPoint? geoPoint; | 72 | final GeoPoint? geoPoint; |
| 123 | 73 | ||
| 124 | - /// The phone number that is embedded in the barcode. | 74 | + /// Gets parsed phone number details. |
| 125 | final Phone? phone; | 75 | final Phone? phone; |
| 126 | 76 | ||
| 127 | - /// The raw bytes of the barcode. | ||
| 128 | - /// | ||
| 129 | - /// This is null if the raw bytes are not available. | ||
| 130 | - final Uint8List? rawBytes; | ||
| 131 | - | ||
| 132 | - /// The raw value of `UTF-8` encoded barcodes. | ||
| 133 | - /// | ||
| 134 | - /// Structured values are not parsed, | ||
| 135 | - /// for example: 'MEBKM:TITLE:Google;URL://www.google.com;;'. | ||
| 136 | - /// | ||
| 137 | - /// For non-UTF-8 barcodes, prefer using [rawBytes] instead. | ||
| 138 | - /// | ||
| 139 | - /// This is null if the raw value is not available. | ||
| 140 | - final String? rawValue; | ||
| 141 | - | ||
| 142 | - /// The SMS message that is embedded in the barcode. | 77 | + /// Gets parsed SMS details. |
| 143 | final SMS? sms; | 78 | final SMS? sms; |
| 144 | 79 | ||
| 145 | - /// The type of the [format] of the barcode. | ||
| 146 | - /// | ||
| 147 | - /// For types that are recognized, | ||
| 148 | - /// but could not be parsed correctly, [BarcodeType.text] will be returned. | ||
| 149 | - /// | ||
| 150 | - /// For types that are not recognised, [BarcodeType.unknown] will be returned. | ||
| 151 | - /// | ||
| 152 | - /// If a given barcode was not correctly identified, | ||
| 153 | - /// consider parsing [rawValue] manually instead. | ||
| 154 | - final BarcodeType type; | ||
| 155 | - | ||
| 156 | - /// The URL bookmark that is embedded in the barcode. | 80 | + /// Gets parsed URL bookmark details. |
| 157 | final UrlBookmark? url; | 81 | final UrlBookmark? url; |
| 158 | 82 | ||
| 159 | - /// The Wireless network information that is embedded in the barcode. | 83 | + /// Gets parsed WiFi AP details. |
| 160 | final WiFi? wifi; | 84 | final WiFi? wifi; |
| 85 | + | ||
| 86 | + Barcode({ | ||
| 87 | + this.corners = const <Offset>[], | ||
| 88 | + this.format = BarcodeFormat.ean13, | ||
| 89 | + this.rawBytes, | ||
| 90 | + this.type = BarcodeType.text, | ||
| 91 | + this.calendarEvent, | ||
| 92 | + this.contactInfo, | ||
| 93 | + this.driverLicense, | ||
| 94 | + this.email, | ||
| 95 | + this.geoPoint, | ||
| 96 | + this.phone, | ||
| 97 | + this.sms, | ||
| 98 | + this.url, | ||
| 99 | + this.wifi, | ||
| 100 | + this.displayValue, | ||
| 101 | + required this.rawValue, | ||
| 102 | + }); | ||
| 103 | + | ||
| 104 | + /// Create a [Barcode] from native data. | ||
| 105 | + Barcode.fromNative(Map data) | ||
| 106 | + : corners = toCorners( | ||
| 107 | + (data['corners'] as List?)?.cast<Map<Object?, Object?>>(), | ||
| 108 | + ) ?? | ||
| 109 | + const <Offset>[], | ||
| 110 | + format = toFormat(data['format'] as int), | ||
| 111 | + rawBytes = data['rawBytes'] as Uint8List?, | ||
| 112 | + rawValue = data['rawValue'] as String?, | ||
| 113 | + displayValue = data['displayValue'] as String?, | ||
| 114 | + type = BarcodeType.values[data['type'] as int], | ||
| 115 | + calendarEvent = toCalendarEvent(data['calendarEvent'] as Map?), | ||
| 116 | + contactInfo = toContactInfo(data['contactInfo'] as Map?), | ||
| 117 | + driverLicense = toDriverLicense(data['driverLicense'] as Map?), | ||
| 118 | + email = toEmail(data['email'] as Map?), | ||
| 119 | + geoPoint = toGeoPoint(data['geoPoint'] as Map?), | ||
| 120 | + phone = toPhone(data['phone'] as Map?), | ||
| 121 | + sms = toSMS(data['sms'] as Map?), | ||
| 122 | + url = toUrl(data['url'] as Map?), | ||
| 123 | + wifi = toWiFi(data['wifi'] as Map?); | ||
| 161 | } | 124 | } |
-
Please register or login to post a comment