/// <summary> /// </summary> /// <returns> /// a byte array of horizontal pixels (false = white, true = black) /// </returns> public override bool[] encode(String contents) { int length = contents.Length; switch (length) { case 7: // No check digit present, calculate it and add it var check = UPCEANReader.getStandardUPCEANChecksum(contents); if (check == null) { throw new ArgumentException("Checksum can't be calculated"); } contents += check.Value; break; case 8: try { if (!UPCEANReader.checkStandardUPCEANChecksum(contents)) { throw new ArgumentException("Contents do not pass checksum"); } } catch (FormatException ignored) { throw new ArgumentException("Illegal contents", ignored); } break; default: throw new ArgumentException("Requested contents should be 7 (without checksum digit) or 8 digits long, but got " + contents.Length); } var result = new bool[CODE_WIDTH]; int pos = 0; pos += appendPattern(result, pos, UPCEANReader.START_END_PATTERN, true); for (int i = 0; i <= 3; i++) { int digit = Int32.Parse(contents.Substring(i, 1)); pos += appendPattern(result, pos, UPCEANReader.L_PATTERNS[digit], false); } pos += appendPattern(result, pos, UPCEANReader.MIDDLE_PATTERN, false); for (int i = 4; i <= 7; i++) { int digit = Int32.Parse(contents.Substring(i, 1)); pos += appendPattern(result, pos, UPCEANReader.L_PATTERNS[digit], true); } appendPattern(result, pos, UPCEANReader.START_END_PATTERN, true); return(result); }
private int decodeMiddle(BitArray row, int[] startRange, StringBuilder resultString) { int[] counters = decodeMiddleCounters; counters[0] = 0; counters[1] = 0; counters[2] = 0; counters[3] = 0; int end = row.Size; int rowOffset = startRange[1]; int lgPatternFound = 0; for (int x = 0; x < 5 && rowOffset < end; x++) { int bestMatch; if (!UPCEANReader.decodeDigit(row, counters, rowOffset, UPCEANReader.L_AND_G_PATTERNS, out bestMatch)) { return(-1); } resultString.Append((char)('0' + bestMatch % 10)); foreach (int counter in counters) { rowOffset += counter; } if (bestMatch >= 10) { lgPatternFound |= 1 << (4 - x); } if (x != 4) { // Read off separator if not last rowOffset = row.getNextSet(rowOffset); rowOffset = row.getNextUnset(rowOffset); } } if (resultString.Length != 5) { return(-1); } int checkDigit; if (!determineCheckDigit(lgPatternFound, out checkDigit)) { return(-1); } if (extensionChecksum(resultString.ToString()) != checkDigit) { return(-1); } return(rowOffset); }
internal Result decodeRow(int rowNumber, BitArray row, int rowOffset) { int[] extensionStartRange = UPCEANReader.findGuardPattern(row, rowOffset, false, EXTENSION_START_PATTERN); if (extensionStartRange == null) { return(null); } var result = fiveSupport.decodeRow(rowNumber, row, extensionStartRange); if (result == null) { result = twoSupport.decodeRow(rowNumber, row, extensionStartRange); } return(result); }
private int decodeMiddle(BitArray row, int[] startRange, StringBuilder resultString) { int[] counters = decodeMiddleCounters; counters[0] = 0; counters[1] = 0; counters[2] = 0; counters[3] = 0; int end = row.Size; int rowOffset = startRange[1]; int checkParity = 0; for (int x = 0; x < 2 && rowOffset < end; x++) { int bestMatch; if (!UPCEANReader.decodeDigit(row, counters, rowOffset, UPCEANReader.L_AND_G_PATTERNS, out bestMatch)) { return(-1); } resultString.Append((char)('0' + bestMatch % 10)); foreach (int counter in counters) { rowOffset += counter; } if (bestMatch >= 10) { checkParity |= 1 << (1 - x); } if (x != 1) { // Read off separator if not last rowOffset = row.getNextSet(rowOffset); rowOffset = row.getNextUnset(rowOffset); } } if (resultString.Length != 2) { return(-1); } if (int.Parse(resultString.ToString()) % 4 != checkParity) { return(-1); } return(rowOffset); }
public override bool[] encode(String contents) { int length = contents.Length; switch (length) { case 7: // No check digit present, calculate it and add it var check = UPCEANReader.getStandardUPCEANChecksum(UPCEReader.convertUPCEtoUPCA(contents)); if (check == null) { throw new ArgumentException("Checksum can't be calculated"); } contents += check.Value; break; case 8: try { if (!UPCEANReader.checkStandardUPCEANChecksum(contents)) { throw new ArgumentException("Contents do not pass checksum"); } } catch (FormatException ignored) { throw new ArgumentException("Illegal contents", ignored); } break; default: throw new ArgumentException("Requested contents should be 8 digits long, but got " + length); } int firstDigit = int.Parse(contents.Substring(0, 1)); if (firstDigit != 0 && firstDigit != 1) { throw new ArgumentException("Number system must be 0 or 1"); } var checkDigit = int.Parse(contents.Substring(7, 1)); var parities = UPCEReader.NUMSYS_AND_CHECK_DIGIT_PATTERNS[firstDigit][checkDigit]; var result = new bool[CODE_WIDTH]; var pos = 0; pos += appendPattern(result, pos, UPCEANReader.START_END_PATTERN, true); for (var i = 1; i <= 6; i++) { var digit = int.Parse(contents.Substring(i, 1)); if ((parities >> (6 - i) & 1) == 1) { digit += 10; } pos += appendPattern(result, pos, UPCEANReader.L_AND_G_PATTERNS[digit], false); } appendPattern(result, pos, UPCEANReader.END_PATTERN, false); return(result); }
/// <summary> /// Encode the contents to byte array expression of one-dimensional barcode. /// Start code and end code should be included in result, and side margins should not be included. /// <returns>a {@code boolean[]} of horizontal pixels (false = white, true = black)</returns> /// </summary> /// <param name="contents"></param> /// <returns></returns> public override bool[] encode(String contents) { int length = contents.Length; switch (length) { case 12: // No check digit present, calculate it and add it var check = UPCEANReader.getStandardUPCEANChecksum(contents); if (check == null) { throw new ArgumentException("Checksum can't be calculated"); } contents += check.Value; break; case 13: try { if (!UPCEANReader.checkStandardUPCEANChecksum(contents)) { throw new ArgumentException("Contents do not pass checksum"); } } catch (FormatException ignored) { throw new ArgumentException("Illegal contents", ignored); } break; default: throw new ArgumentException("Requested contents should be 12 (without checksum digit) or 13 digits long, but got " + contents.Length); } int firstDigit = Int32.Parse(contents.Substring(0, 1)); int parities = EAN13Reader.FIRST_DIGIT_ENCODINGS[firstDigit]; var result = new bool[CODE_WIDTH]; int pos = 0; pos += appendPattern(result, pos, UPCEANReader.START_END_PATTERN, true); // See EAN13Reader for a description of how the first digit & left bars are encoded for (int i = 1; i <= 6; i++) { int digit = Int32.Parse(contents.Substring(i, 1)); if ((parities >> (6 - i) & 1) == 1) { digit += 10; } pos += appendPattern(result, pos, UPCEANReader.L_AND_G_PATTERNS[digit], false); } pos += appendPattern(result, pos, UPCEANReader.MIDDLE_PATTERN, false); for (int i = 7; i <= 12; i++) { int digit = Int32.Parse(contents.Substring(i, 1)); pos += appendPattern(result, pos, UPCEANReader.L_PATTERNS[digit], true); } appendPattern(result, pos, UPCEANReader.START_END_PATTERN, true); return(result); }