generateBarcodeLogic method

void generateBarcodeLogic(
  1. String msg,
  2. int errorCorrectionLevel, [
  3. bool autoECI = false
])

@param msg message to encode @param errorCorrectionLevel PDF417 error correction level to use @throws WriterException if the contents cannot be encoded in this format

Implementation

void generateBarcodeLogic(
  String msg,
  int errorCorrectionLevel, [
  bool autoECI = false,
]) {
  //1. step: High-level encoding
  final errorCorrectionCodeWords =
      PDF417ErrorCorrection.getErrorCorrectionCodewordCount(
    errorCorrectionLevel,
  );
  final highLevel = PDF417HighLevelEncoder.encodeHighLevel(
    msg,
    _compaction,
    _encoding,
    autoECI,
  );
  final sourceCodeWords = highLevel.length;

  final dimension = _determineDimensions(
    sourceCodeWords,
    errorCorrectionCodeWords,
  );

  final cols = dimension[0];
  final rows = dimension[1];

  final pad = _getNumberOfPadCodewords(
    sourceCodeWords,
    errorCorrectionCodeWords,
    cols,
    rows,
  );

  //2. step: construct data codewords
  if (sourceCodeWords + errorCorrectionCodeWords + 1 > 929) {
    // +1 for symbol length CW
    throw WriterException(
      'Encoded message contains too many code words, '
      'message too big (${msg.length} bytes)',
    );
  }
  final n = sourceCodeWords + pad + 1;
  final sb = StringBuffer();
  sb.writeCharCode(n);
  sb.write(highLevel);
  for (int i = 0; i < pad; i++) {
    sb.writeCharCode(900); //PAD characters
  }
  final dataCodewords = sb.toString();

  //3. step: Error correction
  final ec = PDF417ErrorCorrection.generateErrorCorrection(
    dataCodewords,
    errorCorrectionLevel,
  );

  //4. step: low-level encoding
  _barcodeMatrix = BarcodeMatrix(rows, cols);
  _encodeLowLevel(
    dataCodewords + ec,
    cols,
    rows,
    errorCorrectionLevel,
    _barcodeMatrix!,
  );
}