encodeContent method

  1. @override
List<bool> encodeContent(
  1. String contents, [
  2. EncodeHint? hints
])
override

@return a byte array of horizontal pixels (false = white, true = black)

Implementation

@override
List<bool> encodeContent(
  String contents, [
  EncodeHint? hints,
]) {
  final length = contents.length;
  switch (length) {
    case 7:
      // No check digit present, calculate it and add it
      int check;
      try {
        check = UPCEANReader.getStandardUPCEANChecksum(contents);
      } on FormatsException catch (fe) {
        throw ArgumentError(fe);
      }
      contents += check.toString();
      break;
    case 8:
      try {
        if (!UPCEANReader.checkStandardUPCEANChecksum(contents)) {
          throw ArgumentError('Contents do not pass checksum');
        }
      } on FormatsException catch (_) {
        throw ArgumentError('Illegal contents');
      }
      break;
    default:
      // IllegalArgumentException
      throw ArgumentError(
        'Requested contents should be 7 or 8 '
        'digits long, but got $length',
      );
  }

  OneDimensionalCodeWriter.checkNumeric(contents);

  final result = List.filled(_codeWidth, false);
  int pos = 0;

  pos += OneDimensionalCodeWriter.appendPattern(
    result,
    pos,
    UPCEANReader.startEndPattern,
    true,
  );

  for (int i = 0; i <= 3; i++) {
    final digit = int.parse(contents[i]);
    pos += OneDimensionalCodeWriter.appendPattern(
      result,
      pos,
      UPCEANReader.lPatterns[digit],
      false,
    );
  }

  pos += OneDimensionalCodeWriter.appendPattern(
    result,
    pos,
    UPCEANReader.middlePattern,
    false,
  );

  for (int i = 4; i <= 7; i++) {
    final digit = int.parse(contents[i]);
    pos += OneDimensionalCodeWriter.appendPattern(
      result,
      pos,
      UPCEANReader.lPatterns[digit],
      true,
    );
  }
  OneDimensionalCodeWriter.appendPattern(
    result,
    pos,
    UPCEANReader.startEndPattern,
    true,
  );

  return result;
}