barcode.dart 16.2 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 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689
import 'dart:typed_data';
import 'dart:ui';

import '../util.dart';

/// Represents a single recognized barcode and its value.
class Barcode {
  /// Returns four corner points in clockwise direction starting with top-left.
  ///
  /// Due to the possible perspective distortions, this is not necessarily a rectangle.
  ///
  /// Returns null if the corner points can not be determined.
  final List<Offset>? corners;

  /// Returns barcode format
  final BarcodeFormat format;

  /// Returns raw bytes as it was encoded in the barcode.
  ///
  /// Returns null if the raw bytes can not be determined.
  final Uint8List rawBytes;

  /// Returns barcode value as it was encoded in the barcode. Structured values are not parsed, for example: 'MEBKM:TITLE:Google;URL://www.google.com;;'.
  ///
  /// It's only available when the barcode is encoded in the UTF-8 format, and for non-UTF8 ones use [rawBytes] instead.
  ///
  /// Returns null if the raw value can not be determined.
  final String rawValue;

  /// Returns format type of the barcode value.
  ///
  /// For example, TYPE_TEXT, TYPE_PRODUCT, TYPE_URL, etc.
  ///
  /// 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.
  ///
  /// 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.
  final BarcodeType type;

  /// Gets parsed calendar event details.
  final CalendarEvent? calendarEvent;

  /// Gets parsed contact details.
  final ContactInfo? contactInfo;

  /// Gets parsed driver license details.
  final DriverLicense? driverLicense;

  /// Gets parsed email details.
  final Email? email;

  /// Gets parsed geo coordinates.
  final GeoPoint? geoPoint;

  /// Gets parsed phone number details.
  final Phone? phone;

  /// Gets parsed SMS details.
  final SMS? sms;

  /// Gets parsed URL bookmark details.
  final UrlBookmark? url;

  /// Gets parsed WiFi AP details.
  final WiFi? wifi;

  /// Create a [Barcode] from native data.
  Barcode.fromNative(Map<dynamic, dynamic> data)
      : corners = toCorners(data['corners']),
        format = toFormat(data['format']),
        rawBytes = data['rawBytes'],
        rawValue = data['rawValue'],
        type = BarcodeType.values[data['type']],
        calendarEvent = toCalendarEvent(data['calendarEvent']),
        contactInfo = toContactInfo(data['contactInfo']),
        driverLicense = toDriverLicense(data['driverLicense']),
        email = toEmail(data['email']),
        geoPoint = toGeoPoint(data['geoPoint']),
        phone = toPhone(data['phone']),
        sms = toSMS(data['sms']),
        url = toUrl(data['url']),
        wifi = toWiFi(data['wifi']);
}

/// A calendar event extracted from QRCode.
class CalendarEvent {
  /// Gets the description of the calendar event.
  ///
  /// Returns null if not available.
  final String? description;

  /// Gets the start date time of the calendar event.
  ///
  /// Returns null if not available.
  final DateTime? start;

  /// Gets the end date time of the calendar event.
  ///
  /// Returns null if not available.
  final DateTime? end;

  /// Gets the location of the calendar event.
  ///
  /// Returns null if not available.
  final String? location;

  /// Gets the organizer of the calendar event.
  ///
  /// Returns null if not available.
  final String? organizer;

  /// Gets the status of the calendar event.
  ///
  /// Returns null if not available.
  final String? status;

  /// Gets the summary of the calendar event.
  ///
  /// Returns null if not available.
  final String? summary;

  /// Create a [CalendarEvent] from native data.
  CalendarEvent.fromNative(Map<dynamic, dynamic> data)
      : description = data['description'],
        start = DateTime.tryParse(data['start']),
        end = DateTime.tryParse(data['end']),
        location = data['location'],
        organizer = data['organizer'],
        status = data['status'],
        summary = data['summary'];
}

/// A person's or organization's business card. For example a VCARD.
class ContactInfo {
  /// Gets contact person's addresses.
  ///
  /// Returns an empty list if nothing found.
  final List<Address> addresses;

  /// Gets contact person's emails.
  ///
  /// Returns an empty list if nothing found.
  final List<Email> emails;

  /// Gets contact person's name.
  ///
  /// Returns null if not available.
  final PersonName? name;

  /// Gets contact person's organization.
  ///
  /// Returns null if not available.
  final String organization;

  /// Gets contact person's phones.
  ///
  /// Returns an empty list if nothing found.
  final List<Phone> phones;

  /// Gets contact person's title.
  ///
  /// Returns null if not available.
  final String title;

  /// Gets contact person's urls.
  ///
  /// Returns an empty list if nothing found.
  final List<String> urls;

  /// Create a [ContactInfo] from native data.
  ContactInfo.fromNative(Map<dynamic, dynamic> data)
      : addresses = List.unmodifiable(
            data['addresses'].map((e) => Address.fromNative(e))),
        emails =
            List.unmodifiable(data['emails'].map((e) => Email.fromNative(e))),
        name = toName(data['name']),
        organization = data['organization'],
        phones =
            List.unmodifiable(data['phones'].map((e) => Phone.fromNative(e))),
        title = data['title'],
        urls = List.unmodifiable(data['urls']);
}

/// An address.
class Address {
  /// Gets formatted address, multiple lines when appropriate. This field always contains at least one line.
  final List<String> addressLines;

  /// Gets type of the address.
  final AddressType type;

  /// Create a [Address] from native data.
  Address.fromNative(Map<dynamic, dynamic> data)
      : addressLines = List.unmodifiable(data['addressLines']),
        type = AddressType.values[data['type']];
}

/// A person's name, both formatted version and individual name components.
class PersonName {
  /// Gets first name.
  ///
  /// Returns null if not available.
  final String first;

  /// Gets middle name.
  ///
  /// Returns null if not available.
  final String middle;

  /// Gets last name.
  ///
  /// Returns null if not available.
  final String last;

  /// Gets prefix of the name.
  ///
  /// Returns null if not available.
  final String prefix;

  /// Gets suffix of the person's name.
  ///
  /// Returns null if not available.
  final String suffix;

  /// Gets the properly formatted name.
  ///
  /// Returns null if not available.
  final String formattedName;

  /// Designates a text string to be set as the kana name in the phonebook. Used for Japanese contacts.
  ///
  /// Returns null if not available.
  final String pronunciation;

  /// Create a [PersonName] from native data.
  PersonName.fromNative(Map<dynamic, dynamic> data)
      : first = data['first'],
        middle = data['middle'],
        last = data['last'],
        prefix = data['prefix'],
        suffix = data['suffix'],
        formattedName = data['formattedName'],
        pronunciation = data['pronunciation'];
}

/// A driver license or ID card.
class DriverLicense {
  /// Gets city of holder's address.
  ///
  /// Returns null if not available.
  final String addressCity;

  /// Gets state of holder's address.
  ///
  /// Returns null if not available.
  final String addressState;

  /// Gets holder's street address.
  ///
  /// Returns null if not available.
  final String addressStreet;

  /// Gets postal code of holder's address.
  ///
  /// Returns null if not available.
  final String addressZip;

  /// Gets birth date of the holder.
  ///
  /// Returns null if not available.
  final String birthDate;

  /// Gets "DL" for driver licenses, "ID" for ID cards.
  ///
  /// Returns null if not available.
  final String documentType;

  /// Gets expiry date of the license.
  ///
  /// Returns null if not available.
  final String expiryDate;

  /// Gets holder's first name.
  ///
  /// Returns null if not available.
  final String firstName;

  /// Gets holder's gender. 1 - male, 2 - female.
  ///
  /// Returns null if not available.
  final String gender;

  /// Gets issue date of the license.
  ///
  /// The date format depends on the issuing country. MMDDYYYY for the US, YYYYMMDD for Canada.
  ///
  /// Returns null if not available.
  final String issueDate;

  /// Gets the three-letter country code in which DL/ID was issued.
  ///
  /// Returns null if not available.
  final String issuingCountry;

  /// Gets holder's last name.
  ///
  /// Returns null if not available.
  final String lastName;

  /// Gets driver license ID number.
  ///
  /// Returns null if not available.
  final String licenseNumber;

  /// Gets holder's middle name.
  ///
  /// Returns null if not available.
  final String middleName;

  /// Create a [DriverLicense] from native data.
  DriverLicense.fromNative(Map<dynamic, dynamic> data)
      : addressCity = data['addressCity'],
        addressState = data['addressState'],
        addressStreet = data['addressStreet'],
        addressZip = data['addressZip'],
        birthDate = data['birthDate'],
        documentType = data['documentType'],
        expiryDate = data['expiryDate'],
        firstName = data['firstName'],
        gender = data['gender'],
        issueDate = data['issueDate'],
        issuingCountry = data['issuingCountry'],
        lastName = data['lastName'],
        licenseNumber = data['licenseNumber'],
        middleName = data['middleName'];
}

/// An email message from a 'MAILTO:' or similar QRCode type.
class Email {
  /// Gets email's address.
  ///
  /// Returns null if not available.
  final String address;

  /// Gets email's body.
  ///
  /// Returns null if not available.
  final String body;

  /// Gets email's subject.
  ///
  /// Returns null if not available.
  final String subject;

  /// Gets type of the email.
  ///
  /// See also [EmailType].
  final EmailType type;

  /// Create a [Email] from native data.
  Email.fromNative(Map<dynamic, dynamic> data)
      : address = data['address'],
        body = data['body'],
        subject = data['subject'],
        type = EmailType.values[data['type']];
}

/// GPS coordinates from a 'GEO:' or similar QRCode type.
class GeoPoint {
  /// Gets the latitude.
  final double latitude;

  /// Gets the longitude.
  final double longitude;

  /// Create a [GeoPoint] from native data.
  GeoPoint.fromNative(Map<dynamic, dynamic> data)
      : latitude = data['latitude'],
        longitude = data['longitude'];
}

/// Phone number info.
class Phone {
  /// Gets phone number.
  ///
  /// Returns null if not available.
  final String number;

  /// Gets type of the phone number.
  ///
  /// See also [PhoneType].
  final PhoneType type;

  /// Create a [Phone] from native data.
  Phone.fromNative(Map<dynamic, dynamic> data)
      : number = data['number'],
        type = PhoneType.values[data['type']];
}

/// A sms message from a 'SMS:' or similar QRCode type.
class SMS {
  /// Gets the message content of the sms.
  ///
  /// Returns null if not available.
  final String message;

  /// Gets the phone number of the sms.
  ///
  /// Returns null if not available.
  final String phoneNumber;

  /// Create a [SMS] from native data.
  SMS.fromNative(Map<dynamic, dynamic> data)
      : message = data['message'],
        phoneNumber = data['phoneNumber'];
}

/// A URL and title from a 'MEBKM:' or similar QRCode type.
class UrlBookmark {
  /// Gets the title of the bookmark.
  ///
  /// Returns null if not available.
  final String title;

  /// Gets the url of the bookmark.
  ///
  /// Returns null if not available.
  final String url;

  /// Create a [UrlBookmark] from native data.
  UrlBookmark.fromNative(Map<dynamic, dynamic> data)
      : title = data['title'],
        url = data['url'];
}

/// A wifi network parameters from a 'WIFI:' or similar QRCode type.
class WiFi {
  /// Gets the encryption type of the WIFI.
  ///
  /// See all [EncryptionType].
  final EncryptionType encryptionType;

  /// Gets the ssid of the WIFI.
  ///
  /// Returns null if not available.
  final String ssid;

  /// Gets the password of the WIFI.
  ///
  /// Returns null if not available.
  final String password;

  /// Create a [WiFi] from native data.
  WiFi.fromNative(Map<dynamic, dynamic> data)
      : encryptionType = EncryptionType.values[data['encryptionType']],
        ssid = data['ssid'],
        password = data['password'];
}

enum BarcodeFormat {
  /// Barcode format unknown to the current SDK.
  ///
  /// Constant Value: -1
  unknown,

  /// Barcode format constant representing the union of all supported formats.
  ///
  /// Constant Value: 0
  all,

  /// Barcode format constant for Code 128.
  ///
  /// Constant Value: 1
  code128,

  /// Barcode format constant for Code 39.
  ///
  /// Constant Value: 2
  code39,

  /// Barcode format constant for Code 93.
  ///
  /// Constant Value: 4
  code93,

  /// Barcode format constant for Codabar.
  ///
  /// Constant Value: 8
  codebar,

  /// Barcode format constant for Data Matrix.
  ///
  /// Constant Value: 16
  data_matrix,

  /// Barcode format constant for EAN-13.
  ///
  /// Constant Value: 32
  ean13,

  /// Barcode format constant for EAN-8.
  ///
  /// Constant Value: 64
  ean8,

  /// Barcode format constant for ITF (Interleaved Two-of-Five).
  ///
  /// Constant Value: 128
  itf,

  /// Barcode format constant for QR Code.
  ///
  /// Constant Value: 256
  qr_code,

  /// Barcode format constant for UPC-A.
  ///
  /// Constant Value: 512
  upc_a,

  /// Barcode format constant for UPC-E.
  ///
  /// Constant Value: 1024
  upc_e,

  /// Barcode format constant for PDF-417.
  ///
  /// Constant Value: 2048
  pdf417,

  /// Barcode format constant for AZTEC.
  ///
  /// Constant Value: 4096
  aztec,
}

/// Address type constants.
enum AddressType {
  /// Unknown address type.
  ///
  /// Constant Value: 0
  unknown,

  /// Work address.
  ///
  /// Constant Value: 1
  work,

  /// Home address.
  ///
  /// Constant Value: 2
  home,
}

/// Barcode value type constants
enum BarcodeType {
  /// Barcode value type unknown, which indicates the current version of SDK cannot recognize the structure of the barcode. Developers can inspect the raw value instead.
  ///
  /// Constant Value: 0
  unknown,

  /// Barcode value type constant for contact information.
  ///
  /// Constant Value: 1
  contactInfo,

  /// Barcode value type constant for email message details.
  ///
  /// Constant Value: 2
  email,

  /// Barcode value type constant for ISBNs.
  ///
  /// Constant Value: 3
  isbn,

  /// Barcode value type constant for phone numbers.
  ///
  /// Constant Value: 4
  phone,

  /// Barcode value type constant for product codes.
  ///
  /// Constant Value: 5
  product,

  /// Barcode value type constant for SMS details.
  ///
  /// Constant Value: 6
  sms,

  /// Barcode value type constant for plain text.
  ///
  ///Constant Value: 7
  text,

  /// Barcode value type constant for URLs/bookmarks.
  ///
  /// Constant Value: 8
  url,

  /// Barcode value type constant for WiFi access point details.
  ///
  /// Constant Value: 9
  wifi,

  /// Barcode value type constant for geographic coordinates.
  ///
  /// Constant Value: 10
  geo,

  /// Barcode value type constant for calendar events.
  ///
  /// Constant Value: 11
  calendarEvent,

  /// Barcode value type constant for driver's license data.
  ///
  /// Constant Value: 12
  driverLicense,
}

/// Email format type constants.
enum EmailType {
  /// Unknown email type.
  ///
  /// Constant Value: 0
  unknown,

  /// Work email.
  ///
  /// Constant Value: 1
  work,

  /// Home email.
  ///
  /// Constant Value: 2
  home,
}

/// Phone number format type constants.
enum PhoneType {
  /// Unknown phone type.
  ///
  /// Constant Value: 0
  unknown,

  /// Work phone.
  ///
  /// Constant Value: 1
  work,

  /// Home phone.
  ///
  /// Constant Value: 2
  home,

  /// Fax machine.
  ///
  /// Constant Value: 3
  fax,

  /// Mobile phone.
  ///
  /// Constant Value: 4
  mobile,
}

/// Wifi encryption type constants.
enum EncryptionType {
  /// Unknown encryption type.
  ///
  /// Constant Value: 0
  none,

  /// Not encrypted.
  ///
  /// Constant Value: 1
  open,

  /// WPA level encryption.
  ///
  /// Constant Value: 2
  wpa,

  /// WEP level encryption.
  ///
  /// Constant Value: 3
  wep,
}