public static IBarcode Encode(string content, UpcENumberSystem numberSystem) { if (content == null) { throw new ArgumentNullException(nameof(content)); } if (!Regex.IsMatch(content, @"^[0-9]*$")) { throw new InvalidOperationException("Can only encode numerical digits (0-9)"); } if (numberSystem != UpcENumberSystem.Zero && numberSystem != UpcENumberSystem.One) { throw new InvalidOperationException("Only number systems 0 and 1 are supported by UPC E"); } if (content.Length != 6) { throw new InvalidOperationException("Invalid content length. Should be 6"); } BitList bitlist = EncodeUpcE(content, numberSystem); return(new Base1DCode(bitlist, BarcodeType.UPCE, content, Constants.Margin)); }
private static string GetUpcAFromUpcE(string content, UpcENumberSystem numberSystem) { var firstChar = numberSystem == UpcENumberSystem.Zero ? '0' : '1'; var upcA = firstChar.ToString(); switch (content.Last()) { case '0': case '1': case '2': upcA += $"{content.Substring(0, 2)}{content.Last()}0000{content.Substring(2, 3)}"; break; case '3': upcA += $"{content.Substring(0, 3)}00000{content.Substring(3, 2)}"; break; case '4': upcA += $"{content.Substring(0, 4)}00000{content.Substring(4, 1)}"; break; case '5': case '6': case '7': case '8': case '9': upcA += $"{content.Substring(0, 5)}0000{content.Last()}"; break; } upcA += CalculatedUpcAChecksum(upcA); return(upcA); }
public void Encode(string testCode, UpcENumberSystem numberSystem, string testResult, string kind) { IBarcode code = UpcEEncoder.Encode(testCode, numberSystem); code.Content.Should().Be(testCode); code.Bounds.X.Should().Be(testResult.Length); code.Bounds.Y.Should().Be(1); code.Metadata.CodeKind.Should().Be(kind); code.Metadata.Dimensions.Should().Be(1); string encoded = string.Empty; int i = 0; foreach (var r in testResult) { encoded += code.At(i++, 0) ? "1" : "0"; } encoded.Should().Be(testResult); }
private static BitList EncodeUpcE(string content, UpcENumberSystem numberSystem) { var result = new BitList(); // Find the correct parity pattern // To find it we need the check digit of the UPC-A for which this UPC-E barcode encodes var upcA = GetUpcAFromUpcE(content, numberSystem); var upcACheckDigit = upcA.Last(); Constants.ParityPatterns parityPatternTable = Constants.ParityPatternTable[upcACheckDigit]; Constants.Parity[] parityPattern = numberSystem == UpcENumberSystem.Zero ? parityPatternTable.NumberSystemZero : parityPatternTable.NumberSystemOne; // Start bars result.AddBit(true, false, true); // Data bars for (int i = 0; i < content.Length; i++) { char c = content[i]; Constants.EncodedNumber num = Constants.EncodingTable[c]; Constants.Parity parity = parityPattern[i]; if (parity == Constants.Parity.Even) { result.AddBit(num.Even); } else { result.AddBit(num.Odd); } } // Stop bars result.AddBit(false, true, false, true, false, true); return(result); }