RsaDriversLicense.fromBarcodeBytes constructor

RsaDriversLicense.fromBarcodeBytes(
  1. Uint8List bytes
)

Returns a DriversLicense instance from the bytes read from the barcode of the DriversLicense.

IMPORTANT: bytes is the RAW bytes from the barcode. Some barcode scanner plugins expose the String of the barcode which has been decoded using UTF encoding - this corrupts the raw bytes when encoding the string to bytes again. Try to use a barcode scanner which exposes the raw bytes directly (ie. before any encoding/decoding takes place).

See:

Implementation

factory RsaDriversLicense.fromBarcodeBytes(Uint8List bytes) {
  try {
    bytes = _decodeDrivers(bytes);
    var section1 = bytes.sublist(10, 10 + bytes[5]);
    var section2 = bytes.sublist(10 + bytes[5], 10 + bytes[5] + bytes[7]);
    // TODO: Determine length of section 3 (Image Section) and extract image.
    var section3;

    var section1Values = _getSection1Values(section1);
    var section2Values = _getSection2Values(section2);

    var idNumber = section1Values[14];
    var firstNames = section1Values[5];
    var surname = section1Values[4];
    var gender;
    section2Values[11] == '01' ? gender = 'M' : gender = 'F';
    var birthDate = section2Values[8];
    var issueDates = List<DateTime?>.from(section2Values.sublist(1, 5));
    issueDates.removeWhere((date) => date == null);
    var licenseNumber = section1Values[13];
    var vehicleCodes = section1Values.sublist(0, 4);
    vehicleCodes.removeWhere((code) => code.isEmpty);
    var prdpCode = section1Values[6];
    var idCountryOfIssue = section1Values[7];
    var licenseCountryOfIssue = section1Values[8];
    var vehicleRestrictions = section1Values.sublist(9, 13);
    vehicleRestrictions.removeWhere((code) => code.isEmpty);
    var idNumberType = section2Values[0];
    var driverRestrictions = section2Values[5];
    var prdpExpiry = section2Values[6];
    var licenseIssueNumber = section2Values[7];
    var validFrom = section2Values[9];
    var validTo = section2Values[10];
    var imageData = section3;

    return RsaDriversLicense(
      idNumber: idNumber,
      firstNames: firstNames,
      surname: surname,
      gender: gender,
      birthDate: birthDate,
      issueDates: issueDates.cast(),
      licenseNumber: licenseNumber,
      vehicleCodes: vehicleCodes,
      prdpCode: prdpCode,
      idCountryOfIssue: idCountryOfIssue,
      licenseCountryOfIssue: licenseCountryOfIssue,
      vehicleRestrictions: vehicleRestrictions,
      idNumberType: idNumberType,
      driverRestrictions: driverRestrictions,
      prdpExpiry: prdpExpiry,
      licenseIssueNumber: licenseIssueNumber,
      validFrom: validFrom,
      validTo: validTo,
      imageData: imageData,
    );
  } catch (e) {
    throw FormatException(
        'Could not instantiate Drivers License from bytes: $e');
  }
}