From 93b554f3b5be2ac13018f611f7914d1540251194 Mon Sep 17 00:00:00 2001 From: Brad Barnhill Date: Sat, 17 Aug 2024 16:23:28 -0500 Subject: [PATCH] address project wide lint warnings, remove useless comments, remove broken reference --- BarcodeStandard/BarcodeCommon.cs | 18 +- BarcodeStandard/BarcodeLib.cs | 444 +++++++----------- BarcodeStandard/BarcodeStandard.csproj | 16 +- BarcodeStandard/IBarcode.cs | 2 +- BarcodeStandard/Labels.cs | 114 +++-- BarcodeStandard/Symbologies/Codabar.cs | 26 +- BarcodeStandard/Symbologies/Code11.cs | 18 +- BarcodeStandard/Symbologies/Code128.cs | 101 ++-- BarcodeStandard/Symbologies/Code39.cs | 36 +- BarcodeStandard/Symbologies/EAN13.cs | 28 +- BarcodeStandard/Symbologies/EAN8.cs | 16 +- BarcodeStandard/Symbologies/FIM.cs | 6 +- BarcodeStandard/Symbologies/IATA2of5.cs | 4 +- BarcodeStandard/Symbologies/ISBN.cs | 8 +- BarcodeStandard/Symbologies/ITF14.cs | 18 +- .../Symbologies/Interleaved2of5.cs | 14 +- BarcodeStandard/Symbologies/JAN13.cs | 4 +- BarcodeStandard/Symbologies/MSI.cs | 18 +- BarcodeStandard/Symbologies/Pharmacode.cs | 16 +- BarcodeStandard/Symbologies/Postnet.cs | 18 +- BarcodeStandard/Symbologies/Standard2of5.cs | 6 +- BarcodeStandard/Symbologies/Telepen.cs | 43 +- BarcodeStandard/Symbologies/UPCA.cs | 183 +------- BarcodeStandard/Symbologies/UPCE.cs | 28 +- BarcodeStandard/Symbologies/UPCSupplement2.cs | 20 +- BarcodeStandard/Symbologies/UPCSupplement5.cs | 22 +- .../BarcodeStandardExample.csproj | 8 +- BarcodeStandardExample/TestApp.cs | 30 +- .../BarcodeStandardTests.csproj | 16 +- 29 files changed, 505 insertions(+), 776 deletions(-) diff --git a/BarcodeStandard/BarcodeCommon.cs b/BarcodeStandard/BarcodeCommon.cs index 485460e..fa9d847 100644 --- a/BarcodeStandard/BarcodeCommon.cs +++ b/BarcodeStandard/BarcodeCommon.cs @@ -6,7 +6,7 @@ namespace BarcodeStandard internal abstract class BarcodeCommon { public string RawData { get; protected set; } = ""; - public List Errors { get; } = new List(); + public List Errors { get; } = []; protected void Error(string errorMessage) { @@ -27,16 +27,12 @@ internal static bool IsNumericOnly(string s) internal static int GetAlignmentShiftAdjustment(Barcode barcode) { - switch (barcode.Alignment) + return barcode.Alignment switch { - case AlignmentPositions.Left: - return 0; - case AlignmentPositions.Right: - return (barcode.Width % barcode.EncodedValue.Length); - case AlignmentPositions.Center: - default: - return (barcode.Width % barcode.EncodedValue.Length) / 2; - }//switch + AlignmentPositions.Left => 0, + AlignmentPositions.Right => (barcode.Width % barcode.EncodedValue.Length), + _ => (barcode.Width % barcode.EncodedValue.Length) / 2, + }; } }//BarcodeVariables abstract class -}//namespace +} diff --git a/BarcodeStandard/BarcodeLib.cs b/BarcodeStandard/BarcodeLib.cs index af151aa..f144697 100644 --- a/BarcodeStandard/BarcodeLib.cs +++ b/BarcodeStandard/BarcodeLib.cs @@ -40,7 +40,7 @@ public class Barcode : IDisposable { #region Variables private IBarcode _iBarcode = new Blank(); - private static readonly XmlSerializer SaveDataXmlSerializer = new XmlSerializer(typeof(SaveData)); + private static readonly XmlSerializer SaveDataXmlSerializer = new(typeof(SaveData)); #endregion #region Constructors @@ -179,11 +179,10 @@ public byte[] EncodedImageBytes if (EncodedImage == null) return null; - using (var ms = new MemoryStream()) - { - EncodedImage.Encode(ImageFormat, 100).SaveTo(ms); - return ms.ToArray(); - }//using + using var ms = new MemoryStream(); + EncodedImage.Encode(ImageFormat, 100).SaveTo(ms); + return ms.ToArray(); + } } /// @@ -312,107 +311,38 @@ public string GenerateBarcode(string rawData = "") EncodedValue = ""; CountryAssigningManufacturerCode = "N/A"; - - switch (EncodedType) - { - case Type.Ucc12: - case Type.UpcA: //Encode_UPCA(); - _iBarcode = new UPCA(RawData); - break; - case Type.Ucc13: - case Type.Ean13: //Encode_EAN13(); - _iBarcode = new EAN13(RawData, DisableEan13CountryException); - break; - case Type.Interleaved2Of5Mod10: - case Type.Interleaved2Of5: //Encode_Interleaved2of5(); - _iBarcode = new Interleaved2of5(RawData, EncodedType); - break; - case Type.Industrial2Of5Mod10: - case Type.Industrial2Of5: - case Type.Standard2Of5Mod10: - case Type.Standard2Of5: //Encode_Standard2of5(); - _iBarcode = new Standard2of5(RawData, EncodedType); - break; - case Type.IATA2of5: //Encode_IATA2of5(); - _iBarcode = new IATA2of5(RawData); - break; - case Type.Logmars: - case Type.Code39: //Encode_Code39(); - _iBarcode = new Code39(RawData); - break; - case Type.Code39Extended: - _iBarcode = new Code39(RawData, true); - break; - case Type.Code39Mod43: - _iBarcode = new Code39(RawData, false, true); - break; - case Type.Codabar: //Encode_Codabar(); - _iBarcode = new Codabar(RawData); - break; - case Type.PostNet: //Encode_PostNet(); - _iBarcode = new Postnet(RawData); - break; - case Type.Isbn: - case Type.Bookland: //Encode_ISBN_Bookland(); - _iBarcode = new ISBN(RawData); - break; - case Type.Jan13: //Encode_JAN13(); - _iBarcode = new JAN13(RawData); - break; - case Type.UpcSupplemental2Digit: //Encode_UPCSupplemental_2(); - _iBarcode = new UPCSupplement2(RawData); - break; - case Type.MsiMod10: - case Type.Msi2Mod10: - case Type.MsiMod11: - case Type.MsiMod11Mod10: - case Type.ModifiedPlessey: //Encode_MSI(); - _iBarcode = new MSI(RawData, EncodedType); - break; - case Type.UpcSupplemental5Digit: //Encode_UPCSupplemental_5(); - _iBarcode = new UPCSupplement5(RawData); - break; - case Type.UpcE: //Encode_UPCE(); - _iBarcode = new UPCE(RawData); - break; - case Type.Ean8: //Encode_EAN8(); - _iBarcode = new EAN8(RawData); - break; - case Type.Usd8: - case Type.Code11: //Encode_Code11(); - _iBarcode = new Code11(RawData); - break; - case Type.Code128: //Encode_Code128(); - _iBarcode = new Code128(RawData); - break; - case Type.Code128A: - _iBarcode = new Code128(RawData, Code128.TYPES.A); - break; - case Type.Code128B: - _iBarcode = new Code128(RawData, Code128.TYPES.B); - break; - case Type.Code128C: - _iBarcode = new Code128(RawData, Code128.TYPES.C); - break; - case Type.Itf14: - _iBarcode = new ITF14(RawData); - break; - case Type.Code93: - _iBarcode = new Code93(RawData); - break; - case Type.Telepen: - _iBarcode = new Telepen(RawData); - break; - case Type.Fim: - _iBarcode = new FIM(RawData); - break; - case Type.Pharmacode: - _iBarcode = new Pharmacode(RawData); - break; - - default: throw new Exception("EENCODE-2: Unsupported encoding type specified."); - }//switch + _iBarcode = EncodedType switch + { + Type.Ucc12 or Type.UpcA => new UPCA(RawData), + Type.Ucc13 or Type.Ean13 => new EAN13(RawData, DisableEan13CountryException), + Type.Interleaved2Of5Mod10 or Type.Interleaved2Of5 => new Interleaved2of5(RawData, EncodedType), + Type.Industrial2Of5Mod10 or Type.Industrial2Of5 or Type.Standard2Of5Mod10 or Type.Standard2Of5 => new Standard2of5(RawData, EncodedType), + Type.IATA2of5 => new IATA2of5(RawData), + Type.Logmars or Type.Code39 => new Code39(RawData), + Type.Code39Extended => new Code39(RawData, true), + Type.Code39Mod43 => new Code39(RawData, false, true), + Type.Codabar => new Codabar(RawData), + Type.PostNet => new Postnet(RawData), + Type.Isbn or Type.Bookland => new ISBN(RawData), + Type.Jan13 => new JAN13(RawData), + Type.UpcSupplemental2Digit => new UPCSupplement2(RawData), + Type.MsiMod10 or Type.Msi2Mod10 or Type.MsiMod11 or Type.MsiMod11Mod10 or Type.ModifiedPlessey => new MSI(RawData, EncodedType), + Type.UpcSupplemental5Digit => new UPCSupplement5(RawData), + Type.UpcE => new UPCE(RawData), + Type.Ean8 => new EAN8(RawData), + Type.Usd8 or Type.Code11 => new Code11(RawData), + Type.Code128 => new Code128(RawData), + Type.Code128A => new Code128(RawData, Code128.TYPES.A), + Type.Code128B => new Code128(RawData, Code128.TYPES.B), + Type.Code128C => new Code128(RawData, Code128.TYPES.C), + Type.Itf14 => new ITF14(RawData), + Type.Code93 => new Code93(RawData), + Type.Telepen => new Telepen(RawData), + Type.Fim => new FIM(RawData), + Type.Pharmacode => new Pharmacode(RawData), + _ => throw new Exception("EENCODE-2: Unsupported encoding type specified."), + }; return _iBarcode.Encoded_Value; } #endregion @@ -487,7 +417,7 @@ private SKBitmap Generate_Image() canvas.DrawLine(new SKPoint((pos * iBarWidth) + shiftAdjustment + bearerwidth + iquietzone, 0), new SKPoint((pos * iBarWidth) + shiftAdjustment + bearerwidth + iquietzone, Height), paint); pos++; - }//while + } //bearer bars paint.StrokeWidth = (float)ilHeight / 8; @@ -498,7 +428,7 @@ private SKBitmap Generate_Image() canvas.DrawLine(new SKPoint(0, ilHeight), new SKPoint(bitmap.Width, ilHeight), paint);//bottom canvas.DrawLine(new SKPoint(0, 0), new SKPoint(0, ilHeight), paint);//left canvas.DrawLine(new SKPoint(bitmap.Width, 0), new SKPoint(bitmap.Width, ilHeight), paint);//right - }//using + } if (IncludeLabel) Labels.Label_ITF14(this, bitmap); @@ -537,20 +467,18 @@ private SKBitmap Generate_Image() var barwidth = iBarWidth; //lines are fBarWidth wide so draw the appropriate color line vertically - using (var paintFore = new SKPaint()) + using var paintFore = new SKPaint(); + paintFore.ColorF = ForeColor; + paintFore.StrokeWidth = barwidth; + while (pos < EncodedValue.Length) { - paintFore.ColorF = ForeColor; - paintFore.StrokeWidth = barwidth; - while (pos < EncodedValue.Length) + if (EncodedValue[pos] == '1') { - if (EncodedValue[pos] == '1') - { - canvas.DrawLine(new SKPoint(pos * iBarWidth + shiftAdjustment + halfBarWidth, topLabelAdjustment), new SKPoint(pos * iBarWidth + shiftAdjustment + halfBarWidth, ilHeight + topLabelAdjustment), paintFore); - } + canvas.DrawLine(new SKPoint(pos * iBarWidth + shiftAdjustment + halfBarWidth, topLabelAdjustment), new SKPoint(pos * iBarWidth + shiftAdjustment + halfBarWidth, ilHeight + topLabelAdjustment), paintFore); + } - pos++; - }//while - }//using + pos++; + } } if (IncludeLabel) @@ -598,19 +526,17 @@ private SKBitmap Generate_Image() //clears the image and colors the entire background canvas.Clear((SKColor)BackColor); - using (var paint = new SKPaint()) + using var paint = new SKPaint(); + paint.ColorF = ForeColor; + paint.StrokeWidth = iBarWidth; + while (pos < EncodedValue.Length) { - paint.ColorF = ForeColor; - paint.StrokeWidth = iBarWidth; - while (pos < EncodedValue.Length) + if (EncodedValue[pos] == '1') { - if (EncodedValue[pos] == '1') - { - canvas.DrawLine(new SKPoint(pos * iBarWidth + shiftAdjustment + halfBarWidth, topLabelAdjustment), new SKPoint(pos * iBarWidth + shiftAdjustment + halfBarWidth, ilHeight + topLabelAdjustment), paint); - } - - pos++; + canvas.DrawLine(new SKPoint(pos * iBarWidth + shiftAdjustment + halfBarWidth, topLabelAdjustment), new SKPoint(pos * iBarWidth + shiftAdjustment + halfBarWidth, ilHeight + topLabelAdjustment), paint); } + + pos++; } } @@ -654,45 +580,43 @@ private SKBitmap Generate_Image() canvas.Clear((SKColor)BackColor); var barWidth = iBarWidth / iBarWidthModifier; - + //lines are fBarWidth wide so draw the appropriate color line vertically - using (var backPaint = new SKPaint()) + using var backPaint = new SKPaint(); + backPaint.ColorF = BackColor; + backPaint.StrokeWidth = barWidth; + using var forePaint = new SKPaint(); + forePaint.ColorF = ForeColor; + forePaint.StrokeWidth = barWidth; + while (pos < EncodedValue.Length) { - backPaint.ColorF = BackColor; - backPaint.StrokeWidth = barWidth; - using (var forePaint = new SKPaint()) + if (EncodedType == Type.PostNet) + { + //draw half bars in postnet + var y = 0f; + if (EncodedValue[pos] == '0') + y = ilHeight - ilHeight * 0.4f; + + canvas.DrawLine(new SKPoint(pos * iBarWidth + shiftAdjustment + halfBarWidth, ilHeight), new SKPoint(pos * iBarWidth + shiftAdjustment + halfBarWidth, y), forePaint); + } + else { - forePaint.ColorF = ForeColor; - forePaint.StrokeWidth = barWidth; - while (pos < EncodedValue.Length) - { - if (EncodedType == Type.PostNet) - { - //draw half bars in postnet - var y = 0f; - if (EncodedValue[pos] == '0') - y = ilHeight - ilHeight * 0.4f; - - canvas.DrawLine(new SKPoint(pos * iBarWidth + shiftAdjustment + halfBarWidth, ilHeight), new SKPoint(pos * iBarWidth + shiftAdjustment + halfBarWidth, y), forePaint); - }//if - else - { - if (EncodedValue[pos] == '1') - canvas.DrawLine(new SKPoint(pos * iBarWidth + shiftAdjustment + halfBarWidth, 0f), new SKPoint(pos * iBarWidth + shiftAdjustment + halfBarWidth, ilHeight), forePaint); - } - pos++; - }//while - }//using - }//using - }//using + if (EncodedValue[pos] == '1') + canvas.DrawLine(new SKPoint(pos * iBarWidth + shiftAdjustment + halfBarWidth, 0f), new SKPoint(pos * iBarWidth + shiftAdjustment + halfBarWidth, ilHeight), forePaint); + } + pos++; + } + + + } if (IncludeLabel) { Labels.Label_Generic(this, bitmap); - }//if + } break; - }//switch - }//switch + } + } EncodedImage = SKImage.FromBitmap(bitmap); @@ -715,19 +639,18 @@ public byte[] GetImageData(SaveTypes savetype) if (EncodedImage != null) { //Save the image to a memory stream so that we can get a byte array! - using (var ms = new MemoryStream()) - { - SaveImage(ms, savetype); - imageData = ms.ToArray(); - ms.Flush(); - ms.Close(); - }//using - }//if - }//try + using var ms = new MemoryStream(); + SaveImage(ms, savetype); + imageData = ms.ToArray(); + ms.Flush(); + ms.Close(); + + } + } catch (Exception ex) { throw new Exception("EGETIMAGEDATA-1: Could not retrieve image data. " + ex.Message); - }//catch + } return imageData; } /// @@ -740,17 +663,15 @@ public void SaveImage(string filename, SaveTypes fileType) try { if (EncodedImage == null) return; - using (Stream fs = File.OpenWrite(filename)) - { - var data = EncodedImage.Encode(GetSaveType(fileType), 100); - data.SaveTo(fs); - } - //if - }//try + using Stream fs = File.OpenWrite(filename); + var data = EncodedImage.Encode(GetSaveType(fileType), 100); + data.SaveTo(fs); + + } catch (Exception ex) { throw new Exception("ESAVEIMAGE-1: Could not save image.\n\n=======================\n\n" + ex.Message); - }//catch + } }//SaveImage(string, SaveTypes) /// /// Saves an encoded image to a specified stream. @@ -762,23 +683,22 @@ public void SaveImage(Stream stream, SaveTypes fileType) try { EncodedImage?.Encode(GetSaveType(fileType), 100).SaveTo(stream); - }//try + } catch (Exception ex) { throw new Exception("ESAVEIMAGE-2: Could not save image.\n\n=======================\n\n" + ex.Message); - }//catch + } }//SaveImage(Stream, SaveTypes) private SKEncodedImageFormat GetSaveType(SaveTypes fileType) { - switch (fileType) + return fileType switch { - case SaveTypes.Jpg: return SKEncodedImageFormat.Jpeg; - case SaveTypes.Png: return SKEncodedImageFormat.Png; - case SaveTypes.Webp: return SKEncodedImageFormat.Webp; - case SaveTypes.Unspecified: - default: return ImageFormat; - }//switch + SaveTypes.Jpg => SKEncodedImageFormat.Jpeg, + SaveTypes.Png => SKEncodedImageFormat.Png, + SaveTypes.Webp => SKEncodedImageFormat.Webp, + _ => ImageFormat, + }; } #endregion @@ -787,20 +707,22 @@ private SKEncodedImageFormat GetSaveType(SaveTypes fileType) private SaveData GetSaveData(bool includeImage = true) { - var saveData = new SaveData(); - saveData.Type = EncodedType.ToString(); - saveData.RawData = RawData; - saveData.EncodedValue = EncodedValue; - saveData.EncodingTime = EncodingTime; - saveData.IncludeLabel = IncludeLabel; - saveData.Forecolor = ForeColor.ToString(); - saveData.Backcolor = BackColor.ToString(); - saveData.CountryAssigningManufacturingCode = CountryAssigningManufacturerCode; - saveData.ImageWidth = Width; - saveData.ImageHeight = Height; - saveData.LabelFont = LabelFont.ToString(); - saveData.ImageFormat = ImageFormat.ToString(); - saveData.Alignment = (int)Alignment; + var saveData = new SaveData + { + Type = EncodedType.ToString(), + RawData = RawData, + EncodedValue = EncodedValue, + EncodingTime = EncodingTime, + IncludeLabel = IncludeLabel, + Forecolor = ForeColor.ToString(), + Backcolor = BackColor.ToString(), + CountryAssigningManufacturingCode = CountryAssigningManufacturerCode, + ImageWidth = Width, + ImageHeight = Height, + LabelFont = LabelFont.ToString(), + ImageFormat = ImageFormat.ToString(), + Alignment = (int)Alignment + }; //get image in base 64 if (!includeImage) return saveData; @@ -808,7 +730,7 @@ private SaveData GetSaveData(bool includeImage = true) { EncodedImage.Encode(ImageFormat, 100).SaveTo(ms); saveData.Image = Convert.ToBase64String(ms.ToArray(), Base64FormattingOptions.None); - }//using + } return saveData; } public string ToJson(Boolean includeImage = true) @@ -825,65 +747,56 @@ public string ToXml(Boolean includeImage = true) { try { - using (var xml = GetSaveData(includeImage)) - { - using (var sw = new Utf8StringWriter()) - { - SaveDataXmlSerializer.Serialize(sw, xml); - return sw.ToString(); - } - }//using - }//try + using var xml = GetSaveData(includeImage); + using var sw = new Utf8StringWriter(); + SaveDataXmlSerializer.Serialize(sw, xml); + return sw.ToString(); + } catch (Exception ex) { throw new Exception("EGETXML-2: " + ex.Message); - }//catch - }//else + } + } } public static SaveData FromJson(Stream jsonStream) { using (jsonStream) { - if (jsonStream is MemoryStream) + if (jsonStream is MemoryStream stream) { - return JsonSerializer.Deserialize(((MemoryStream)jsonStream).ToArray()); + return JsonSerializer.Deserialize(stream.ToArray()); } - using (var memoryStream = new MemoryStream()) - { - jsonStream.CopyTo(memoryStream); - return JsonSerializer.Deserialize(memoryStream.ToArray()); - } + using var memoryStream = new MemoryStream(); + jsonStream.CopyTo(memoryStream); + return JsonSerializer.Deserialize(memoryStream.ToArray()); } } public static SaveData FromXml(Stream xmlStream) { try { - using (var reader = XmlReader.Create(xmlStream)) - { - return (SaveData)SaveDataXmlSerializer.Deserialize(reader); - } - }//try + using var reader = XmlReader.Create(xmlStream); + return (SaveData)SaveDataXmlSerializer.Deserialize(reader); + } catch (Exception ex) { throw new Exception("EGETIMAGEFROMXML-1: " + ex.Message); - }//catch + } } public static SKImage GetImageFromSaveData(SaveData saveData) { try { //loading it to memory stream and then to image object - using (var ms = new MemoryStream(Convert.FromBase64String(saveData.Image))) - { - return SKImage.FromBitmap(SKBitmap.Decode(ms)); - }//using - }//try + using var ms = new MemoryStream(Convert.FromBase64String(saveData.Image)); + return SKImage.FromBitmap(SKBitmap.Decode(ms)); + + } catch (Exception ex) { throw new Exception("EGETIMAGEFROMXML-1: " + ex.Message); - }//catch + } } public class Utf8StringWriter : StringWriter @@ -901,10 +814,9 @@ public class Utf8StringWriter : StringWriter /// Image representing the barcode. public static SKImage DoEncode(Type iType, string data) { - using (var b = new Barcode()) - { - return b.Encode(iType, data); - }//using + using var b = new Barcode(); + return b.Encode(iType, data); + } /// /// Encodes the raw data into binary form representing bars and spaces. Also generates an Image of the barcode. @@ -915,12 +827,11 @@ public static SKImage DoEncode(Type iType, string data) /// Image representing the barcode. public static SKImage DoEncode(Type iType, string data, out string xml) { - using (var b = new Barcode()) - { - var i = b.Encode(iType, data); - xml = b.ToXml(); - return i; - }//using + using var b = new Barcode(); + var i = b.Encode(iType, data); + xml = b.ToXml(); + return i; + } /// /// Encodes the raw data into binary form representing bars and spaces. Also generates an Image of the barcode. @@ -931,11 +842,10 @@ public static SKImage DoEncode(Type iType, string data, out string xml) /// Image representing the barcode. public static SKImage DoEncode(Type iType, string data, bool includeLabel) { - using (var b = new Barcode()) - { - b.IncludeLabel = includeLabel; - return b.Encode(iType, data); - }//using + using var b = new Barcode(); + b.IncludeLabel = includeLabel; + return b.Encode(iType, data); + } /// /// Encodes the raw data into binary form representing bars and spaces. Also generates an Image of the barcode. @@ -948,11 +858,10 @@ public static SKImage DoEncode(Type iType, string data, bool includeLabel) /// Image representing the barcode. public static SKImage DoEncode(Type iType, string data, bool includeLabel, int width, int height) { - using (var b = new Barcode()) - { - b.IncludeLabel = includeLabel; - return b.Encode(iType, data, width, height); - }//using + using var b = new Barcode(); + b.IncludeLabel = includeLabel; + return b.Encode(iType, data, width, height); + } /// /// Encodes the raw data into binary form representing bars and spaces. Also generates an Image of the barcode. @@ -965,11 +874,10 @@ public static SKImage DoEncode(Type iType, string data, bool includeLabel, int w /// Image representing the barcode. public static SKImage DoEncode(Type iType, string data, bool includeLabel, Color drawColor, Color backColor) { - using (var b = new Barcode()) - { - b.IncludeLabel = includeLabel; - return b.Encode(iType, data, new SKColor(drawColor.R, drawColor.G, drawColor.B, drawColor.A), new SKColor(backColor.R, backColor.G, backColor.B, backColor.A)); - }//using + using var b = new Barcode(); + b.IncludeLabel = includeLabel; + return b.Encode(iType, data, new SKColor(drawColor.R, drawColor.G, drawColor.B, drawColor.A), new SKColor(backColor.R, backColor.G, backColor.B, backColor.A)); + } /// /// Encodes the raw data into binary form representing bars and spaces. Also generates an Image of the barcode. @@ -984,11 +892,10 @@ public static SKImage DoEncode(Type iType, string data, bool includeLabel, Color /// Image representing the barcode. public static SKImage DoEncode(Type iType, string data, bool includeLabel, Color drawColor, Color backColor, int width, int height) { - using (var b = new Barcode()) - { - b.IncludeLabel = includeLabel; - return b.Encode(iType, data, new SKColor(drawColor.R, drawColor.G, drawColor.B, drawColor.A), new SKColor(backColor.R, backColor.G, backColor.B, backColor.A), width, height); - }//using + using var b = new Barcode(); + b.IncludeLabel = includeLabel; + return b.Encode(iType, data, new SKColor(drawColor.R, drawColor.G, drawColor.B, drawColor.A), new SKColor(backColor.R, backColor.G, backColor.B, backColor.A), width, height); + } /// /// Encodes the raw data into binary form representing bars and spaces. Also generates an Image of the barcode. @@ -1004,13 +911,12 @@ public static SKImage DoEncode(Type iType, string data, bool includeLabel, Color /// Image representing the barcode. public static SKImage DoEncode(Type iType, string data, bool includeLabel, Color drawColor, Color backColor, int width, int height, out string xml) { - using (var b = new Barcode()) - { - b.IncludeLabel = includeLabel; - var i = b.Encode(iType, data, new SKColor(drawColor.R, drawColor.G, drawColor.B, drawColor.A), new SKColor(backColor.R, backColor.G, backColor.B, backColor.A), width, height); - xml = b.ToXml(); - return i; - }//using + using var b = new Barcode(); + b.IncludeLabel = includeLabel; + var i = b.Encode(iType, data, new SKColor(drawColor.R, drawColor.G, drawColor.B, drawColor.A), new SKColor(backColor.R, backColor.G, backColor.B, backColor.A), width, height); + xml = b.ToXml(); + return i; + } #region IDisposable Support diff --git a/BarcodeStandard/BarcodeStandard.csproj b/BarcodeStandard/BarcodeStandard.csproj index 5f79c8a..12878be 100644 --- a/BarcodeStandard/BarcodeStandard.csproj +++ b/BarcodeStandard/BarcodeStandard.csproj @@ -2,7 +2,7 @@ true - 3.1.3 + 3.1.4 BarcodeLib Pnuema Productions BarcodeLib @@ -18,8 +18,8 @@ LICENSE.txt upca.jpg - 3.1.3.0 - 3.1.3.0 + 3.1.4.0 + 3.1.4.0 netstandard2.0 latest true @@ -28,14 +28,14 @@ - + - - - - + + + + diff --git a/BarcodeStandard/IBarcode.cs b/BarcodeStandard/IBarcode.cs index b571c02..1b1b60b 100644 --- a/BarcodeStandard/IBarcode.cs +++ b/BarcodeStandard/IBarcode.cs @@ -24,4 +24,4 @@ List Errors }//Errors }//interface -}//namespace +} diff --git a/BarcodeStandard/Labels.cs b/BarcodeStandard/Labels.cs index ec92cf4..f3a7c01 100644 --- a/BarcodeStandard/Labels.cs +++ b/BarcodeStandard/Labels.cs @@ -26,53 +26,51 @@ public static SKImage Label_ITF14(Barcode barcode, SKBitmap img) var labelPadding = textBounds.Height / 2f; var backY = img.Height - textBounds.Height - labelPadding * 2f; - using (var canvas = new SKCanvas(img)) + using var canvas = new SKCanvas(img); + //draw bounding box side overdrawn by label + using (var pen = new SKPaint()) { - //draw bounding box side overdrawn by label - using (var pen = new SKPaint()) - { - pen.FilterQuality = SKFilterQuality.High; - pen.IsAntialias = true; - pen.ColorF = barcode.ForeColor; - pen.StrokeWidth = (float)img.Height / 16; - - canvas.DrawLine(new SKPoint(0, backY - pen.StrokeWidth / 2f), - new SKPoint(img.Width, backY - pen.StrokeWidth / 2f), pen); //bottom - } - - //color a box at the bottom of the barcode to hold the string of data - using (var paint = new SKPaint(font)) - { - paint.FilterQuality = SKFilterQuality.High; - paint.IsAntialias = true; - paint.ColorF = barcode.BackColor; - paint.Style = SKPaintStyle.Fill; - - var rect = SKRect.Create(0, backY, img.Width, textBounds.Height + labelPadding * 2f); - canvas.DrawRect(rect, paint); - } - - //draw datastring under the barcode image - foreBrush.FilterQuality = SKFilterQuality.High; - foreBrush.IsAntialias = true; - foreBrush.ColorF = barcode.ForeColor; - foreBrush.TextAlign = SKTextAlign.Center; - - var labelX = img.Width / 2f; - var labelY = img.Height - textBounds.Height + labelPadding; - - canvas.DrawText(str, labelX, labelY, foreBrush); - - canvas.Save(); - } //using + pen.FilterQuality = SKFilterQuality.High; + pen.IsAntialias = true; + pen.ColorF = barcode.ForeColor; + pen.StrokeWidth = (float)img.Height / 16; + + canvas.DrawLine(new SKPoint(0, backY - pen.StrokeWidth / 2f), + new SKPoint(img.Width, backY - pen.StrokeWidth / 2f), pen); //bottom + } + + //color a box at the bottom of the barcode to hold the string of data + using (var paint = new SKPaint(font)) + { + paint.FilterQuality = SKFilterQuality.High; + paint.IsAntialias = true; + paint.ColorF = barcode.BackColor; + paint.Style = SKPaintStyle.Fill; + + var rect = SKRect.Create(0, backY, img.Width, textBounds.Height + labelPadding * 2f); + canvas.DrawRect(rect, paint); + } + + //draw datastring under the barcode image + foreBrush.FilterQuality = SKFilterQuality.High; + foreBrush.IsAntialias = true; + foreBrush.ColorF = barcode.ForeColor; + foreBrush.TextAlign = SKTextAlign.Center; + + var labelX = img.Width / 2f; + var labelY = img.Height - textBounds.Height + labelPadding; + + canvas.DrawText(str, labelX, labelY, foreBrush); + + canvas.Save(); } return SKImage.FromBitmap(img); - }//try + } catch (Exception ex) { throw new Exception("ELABEL_ITF14-1: " + ex.Message); - }//catch + } } /// @@ -128,11 +126,11 @@ public static SKImage Label_Generic(Barcode barcode, SKBitmap img) g.Save(); } return SKImage.FromBitmap(img); - }//try + } catch (Exception ex) { throw new Exception("ELABEL_GENERIC-1: " + ex.Message); - }//catch + } finally { foreBrush.Dispose(); @@ -220,11 +218,11 @@ public static SKImage Label_EAN13(Barcode barcode, SKBitmap img) } return SKImage.FromBitmap(img); - }//try + } catch (Exception ex) { throw new Exception("ELABEL_EAN13-1: " + ex.Message); - }//catch + } finally { foreBrush.Dispose(); @@ -316,11 +314,11 @@ public static SKImage Label_UPCA(Barcode barcode, SKBitmap img) } return SKImage.FromBitmap(img); - }//try + } catch (Exception ex) { throw new Exception("ELABEL_UPCA-1: " + ex.Message); - }//catch + } finally { foreBrush.Dispose(); @@ -328,7 +326,7 @@ public static SKImage Label_UPCA(Barcode barcode, SKBitmap img) } }//Label_UPCA - private static int GetFontSize(Barcode barcode, int wid, int hgt, string lbl) + /*private static int GetFontSize(int wid, int hgt, string lbl) { //Returns the optimal font size for the specified dimensions var fontSize = 10; @@ -338,23 +336,19 @@ private static int GetFontSize(Barcode barcode, int wid, int hgt, string lbl) var bounds = SKRect.Empty; for (var i = 1; i <= 100; i++) { - using (var testFont = new SKFont(SKTypeface.FromFamilyName("Arial", SKFontStyle.Normal), i)) - { - // Make a Graphics object to measure the text. - using (var gr = new SKPaint(testFont)) - { - gr.MeasureText(lbl, ref bounds); - - if (!(bounds.Width > wid) && !(bounds.Height > hgt)) continue; - fontSize = i - 1; - break; - } - } + using var testFont = new SKFont(SKTypeface.FromFamilyName("Arial", SKFontStyle.Normal), i); + // Make a Graphics object to measure the text. + using var gr = new SKPaint(testFont); + gr.MeasureText(lbl, ref bounds); + + if (!(bounds.Width > wid) && !(bounds.Height > hgt)) continue; + fontSize = i - 1; + break; } }; return fontSize; - } + }*/ } } diff --git a/BarcodeStandard/Symbologies/Codabar.cs b/BarcodeStandard/Symbologies/Codabar.cs index 440ab52..bc163c9 100644 --- a/BarcodeStandard/Symbologies/Codabar.cs +++ b/BarcodeStandard/Symbologies/Codabar.cs @@ -6,12 +6,12 @@ namespace BarcodeStandard.Symbologies /// internal class Codabar : BarcodeCommon, IBarcode { - private readonly System.Collections.Hashtable Codabar_Code = new System.Collections.Hashtable(); //is initialized by init_Codabar() + private readonly System.Collections.Hashtable Codabar_Code = []; //is initialized by init_Codabar() internal Codabar(string input) { RawData = input; - }//Codabar + } /// /// Encode the raw data using the Codabar algorithm. @@ -29,7 +29,7 @@ private string Encode_Codabar() case "D": break; default: Error("ECODABAR-2: Data format invalid. (Invalid START character)"); break; - }//switch + } //check the ending char to make sure its a start/stop char switch (RawData[RawData.Trim().Length - 1].ToString().ToUpper().Trim()) @@ -40,10 +40,10 @@ private string Encode_Codabar() case "D": break; default: Error("ECODABAR-3: Data format invalid. (Invalid STOP character)"); break; - }//switch + } //populate the hashtable to begin the process - init_Codabar(); + Init_Codabar(); //replace non-numeric VALID chars with empty strings before checking for all numerics var temp = RawData; @@ -53,8 +53,8 @@ private string Encode_Codabar() if (!IsNumericOnly(c.ToString())) { temp = temp.Replace(c, '1'); - }//if - }//if + } + } //now that all the valid non-numeric chars have been replaced with a number check if all numeric exist if (!IsNumericOnly(temp)) @@ -66,7 +66,7 @@ private string Encode_Codabar() { result += Codabar_Code[c].ToString(); result += "0"; //inter-character space - }//foreach + } //remove the extra 0 at the end of the result result = result.Remove(result.Length - 1); @@ -78,8 +78,8 @@ private string Encode_Codabar() RawData = RawData.Trim().Substring(1, RawData.Trim().Length - 2); return result; - }//Encode_Codabar - private void init_Codabar() + } + private void Init_Codabar() { Codabar_Code.Clear(); Codabar_Code.Add('0', "101010011"); @@ -106,7 +106,7 @@ private void init_Codabar() Codabar_Code.Add('b', "1010010011"); Codabar_Code.Add('c', "1001001011"); Codabar_Code.Add('d', "1010011001"); - }//init_Codeabar + } #region IBarcode Members @@ -114,5 +114,5 @@ private void init_Codabar() #endregion - }//class -}//namespace + } +} diff --git a/BarcodeStandard/Symbologies/Code11.cs b/BarcodeStandard/Symbologies/Code11.cs index 4e42c2a..56d3a83 100644 --- a/BarcodeStandard/Symbologies/Code11.cs +++ b/BarcodeStandard/Symbologies/Code11.cs @@ -8,12 +8,12 @@ namespace BarcodeStandard.Symbologies /// internal class Code11 : BarcodeCommon, IBarcode { - private readonly string[] C11_Code = { "101011", "1101011", "1001011", "1100101", "1011011", "1101101", "1001101", "1010011", "1101001", "110101", "101101", "1011001" }; + private readonly string[] C11_Code = ["101011", "1101011", "1001011", "1100101", "1011011", "1101101", "1001101", "1010011", "1101001", "110101", "101101", "1011001"]; internal Code11(string input) { RawData = input; - }//Code11 + } /// /// Encode the raw data using the Code 11 algorithm. /// @@ -37,7 +37,7 @@ private string Encode_Code11() cTotal += Int32.Parse(RawData[i].ToString()) * weight++; else cTotal += 10 * weight++; - }//for + } var checksumC = cTotal % 11; dataToEncodeWithChecksums += checksumC.ToString(); @@ -58,10 +58,10 @@ private string Encode_Code11() kTotal += Int32.Parse(dataToEncodeWithChecksums[i].ToString()) * weight++; else kTotal += 10 * weight++; - }//for + } var checksumK = kTotal % 11; dataToEncodeWithChecksums += checksumK.ToString(); - }//if + } //encode data var space = "0"; @@ -74,18 +74,18 @@ private string Encode_Code11() //inter-character space result += space; - }//foreach + } //stop bars result += C11_Code[11]; return result; - }//Encode_Code11 + } #region IBarcode Members public string Encoded_Value => Encode_Code11(); #endregion - }//class -}//namespace + } +} diff --git a/BarcodeStandard/Symbologies/Code128.cs b/BarcodeStandard/Symbologies/Code128.cs index 0aece83..9fd384c 100644 --- a/BarcodeStandard/Symbologies/Code128.cs +++ b/BarcodeStandard/Symbologies/Code128.cs @@ -15,14 +15,14 @@ internal class Code128 : BarcodeCommon, IBarcode public static readonly char FNC4 = Convert.ToChar(203); public enum TYPES : int { DYNAMIC, A, B, C }; - private readonly List C128_Code = new List(); - private readonly Dictionary C128_CodeIndexByA = new Dictionary(); - private readonly Dictionary C128_CodeIndexByB = new Dictionary(); - private readonly Dictionary C128_CodeIndexByC = new Dictionary(); - private List _FormattedData = new List(); - private List _EncodedData = new List(); + private readonly List C128_Code = []; + private readonly Dictionary C128_CodeIndexByA = []; + private readonly Dictionary C128_CodeIndexByB = []; + private readonly Dictionary C128_CodeIndexByC = []; + private readonly List _FormattedData = []; + private readonly List _EncodedData = []; private int? _startCharacterIndex; - private TYPES type = TYPES.DYNAMIC; + private readonly TYPES type = TYPES.DYNAMIC; /// /// Encodes data in Code128 format. @@ -31,7 +31,7 @@ public enum TYPES : int { DYNAMIC, A, B, C }; internal Code128(string input) { RawData = input; - }//Code128 + } /// /// Encodes data in Code128 format. @@ -42,7 +42,7 @@ internal Code128(string input, TYPES type) { this.type = type; RawData = input; - }//Code128 + } string C128_ByA(string a) => C128_Code[C128_CodeIndexByA[a]]; @@ -54,11 +54,11 @@ internal Code128(string input, TYPES type) private string Encode_Code128() { //initialize datastructure to hold encoding information - init_Code128(); + Init_Code128(); return GetEncoding(); - }//Encode_Code128 - private void init_Code128() + } + private void Init_Code128() { //populate data void entry(string a, string b, string c, string encoding) @@ -182,17 +182,17 @@ private List FindStartorCodeCharacter(string s) { var rows = new List(); - //if two chars are numbers (or FNC1) then START_C or CODE_C + // if two chars are numbers (or FNC1) then START_C or CODE_C if (s.Length > 1 && (Char.IsNumber(s[0]) || s[0] == FNC1) && (Char.IsNumber(s[1]) || s[1] == FNC1)) { if (!_startCharacterIndex.HasValue) { _startCharacterIndex = C128_CodeIndexByA["START_C"]; rows.Add(_startCharacterIndex.Value); - }//if + } else rows.Add(C128_CodeIndexByA["CODE_C"]); - }//if + } else { try @@ -204,11 +204,11 @@ private List FindStartorCodeCharacter(string s) { _startCharacterIndex = C128_CodeIndexByA["START_A"]; rows.Add(_startCharacterIndex.Value); - }//if + } else { rows.Add(C128_CodeIndexByB["CODE_A"]);//first column is FNC4 so use B - }//else + } } var BFound = C128_CodeIndexByB.TryGetValue(s, out var bIndex) && (!AFound || bIndex != aIndex); if (BFound) @@ -217,21 +217,21 @@ private List FindStartorCodeCharacter(string s) { _startCharacterIndex = C128_CodeIndexByA["START_B"]; rows.Add(_startCharacterIndex.Value); - }//if + } else { rows.Add(C128_CodeIndexByA["CODE_B"]); - }//else + } } - }//try + } catch (Exception ex) { Error("EC128-1: " + ex.Message); - }//catch + } if (rows.Count <= 0) Error("EC128-2: Could not determine start character."); - }//else + } return rows; } @@ -243,16 +243,16 @@ private string CalculateCheckDigit() { var s = _FormattedData[(int)i]; - //try to find value in the A column + // try to find value in the A column var value = C128_CodeIndexByA.TryGetValue(s, out var index) - //try to find value in the B column + // try to find value in the B column || C128_CodeIndexByB.TryGetValue(s, out index) - //try to find value in the C column + // try to find value in the C column || C128_CodeIndexByC.TryGetValue(s, out index) ? (uint)index : throw new InvalidOperationException($"Unable to find character “{s}”"); var addition = value * ((i == 0) ? 1 : i); checkSum += addition; - }//for + } var remainder = checkSum % 103; return C128_Code[(int)remainder]; @@ -308,30 +308,30 @@ private void BreakUpDataForEncoding() if (temp == "") { temp += c; - }//if + } else { temp += c; _FormattedData.Add(temp); temp = ""; - }//else - }//if + } + } else { if (temp != "") { _FormattedData.Add(temp); temp = ""; - }//if + } _FormattedData.Add(c.ToString()); - }//else - }//foreach + } + } - //if something is still in temp go ahead and push it onto the queue + // if something is still in temp go ahead and push it onto the queue if (temp != "") { _FormattedData.Add(temp); - }//if + } } private void InsertStartandCodeCharacters() { @@ -352,7 +352,7 @@ private void InsertStartandCodeCharacters() Error("EC128-4: Unknown start type in fixed type encoding."); break; } - }//if + } else { try @@ -375,11 +375,11 @@ private void InsertStartandCodeCharacters() { sameCodeSet = true; break; - }//if - }//foreach + } + } //only insert a new code char if starting a new codeset - //if (CurrentCodeString == "" || !tempStartChars[0][col].ToString().EndsWith(CurrentCodeString)) /* Removed because of bug */ + // if (CurrentCodeString == "" || !tempStartChars[0][col].ToString().EndsWith(CurrentCodeString)) /* Removed because of bug */ if (col == null || !sameCodeSet) { @@ -414,15 +414,15 @@ private void InsertStartandCodeCharacters() } } } - }//if + } - }//for - }//try + } + } catch (Exception ex) { Error("EC128-3: Could not insert start and code characters.\n Message: " + ex.Message); - }//catch - }//else + } + } } private string GetEncoding() { @@ -457,23 +457,20 @@ private string GetEncoding() { E_Row = C128_TryByB(s); - if (E_Row == null) - { - E_Row = C128_TryByC(s); - }//if - }//if + E_Row ??= C128_TryByC(s); + } break; default: E_Row = null; break; - }//switch + } if (E_Row == null) Error("EC128-5: Could not find encoding of a value( " + s + " ) in C128 type " + type.ToString()); Encoded_Data += E_Row; _EncodedData.Add(E_Row); - }//foreach + } //add the check digit string checkDigit = CalculateCheckDigit(); @@ -497,5 +494,5 @@ private string GetEncoding() public string Encoded_Value => Encode_Code128(); #endregion - }//class -}//namespace + } +} diff --git a/BarcodeStandard/Symbologies/Code39.cs b/BarcodeStandard/Symbologies/Code39.cs index 9ffa517..899b810 100644 --- a/BarcodeStandard/Symbologies/Code39.cs +++ b/BarcodeStandard/Symbologies/Code39.cs @@ -8,8 +8,8 @@ namespace BarcodeStandard.Symbologies /// internal class Code39 : BarcodeCommon, IBarcode { - private readonly System.Collections.Hashtable C39_Code = new System.Collections.Hashtable(); //is initialized by init_Code39() - private readonly System.Collections.Hashtable ExtC39_Translation = new System.Collections.Hashtable(); + private readonly System.Collections.Hashtable C39_Code = []; //is initialized by init_Code39() + private readonly System.Collections.Hashtable ExtC39_Translation = []; private readonly bool _allowExtended; private readonly bool _enableChecksum; @@ -20,7 +20,7 @@ internal class Code39 : BarcodeCommon, IBarcode internal Code39(string input) { RawData = input; - }//Code39 + } /// /// Encodes with Code39. @@ -51,8 +51,8 @@ internal Code39(string input, bool allowExtended, bool enableChecksum) /// private string Encode_Code39() { - init_Code39(); - init_ExtendedCode39(); + Init_Code39(); + Init_ExtendedCode39(); var strNoAstr = RawData.Replace("*", ""); var strFormattedData = "*" + strNoAstr + (_enableChecksum ? GetChecksumChar(strNoAstr).ToString() : String.Empty) + "*"; @@ -61,22 +61,22 @@ private string Encode_Code39() InsertExtendedCharsIfNeeded(ref strFormattedData); var result = ""; - //foreach (char c in this.FormattedData) + // if (char c in this.FormattedData) foreach (var c in strFormattedData) { try { result += C39_Code[c].ToString(); result += "0";//whitespace - }//try + } catch { if (_allowExtended) Error("EC39-1: Invalid data."); else Error("EC39-1: Invalid data. (Try using Extended Code39)"); - }//catch - }//foreach + } + } result = result.Substring(0, result.Length-1); @@ -85,7 +85,7 @@ private string Encode_Code39() return result; }//Encode_Code39 - private void init_Code39() + private void Init_Code39() { C39_Code.Clear(); C39_Code.Add('0', "101001101101"); @@ -132,8 +132,8 @@ private void init_Code39() C39_Code.Add('+', "100101001001"); C39_Code.Add('%', "101001001001"); C39_Code.Add('*', "100101101101"); - }//init_Code39 - private void init_ExtendedCode39() + } + private void Init_ExtendedCode39() { ExtC39_Translation.Clear(); ExtC39_Translation.Add(Convert.ToChar(0).ToString(), "%U"); @@ -235,14 +235,14 @@ private void InsertExtendedCharsIfNeeded(ref string formattedData) { var s = C39_Code[c].ToString(); output += c; - }//try + } catch { //insert extended substitution var oTrans = ExtC39_Translation[c.ToString()]; output += oTrans.ToString(); - }//catch - }//foreach + } + } formattedData = output; } @@ -256,7 +256,7 @@ private char GetChecksumChar(string strNoAstr) //Calculate the checksum foreach (var t in strNoAstr) { - sum = sum + Code39_Charset.IndexOf(t.ToString(), StringComparison.Ordinal); + sum += Code39_Charset.IndexOf(t.ToString(), StringComparison.Ordinal); } //return the checksum char @@ -267,5 +267,5 @@ private char GetChecksumChar(string strNoAstr) public string Encoded_Value => Encode_Code39(); #endregion - }//class -}//namespace + } +} diff --git a/BarcodeStandard/Symbologies/EAN13.cs b/BarcodeStandard/Symbologies/EAN13.cs index f056e92..5df0777 100644 --- a/BarcodeStandard/Symbologies/EAN13.cs +++ b/BarcodeStandard/Symbologies/EAN13.cs @@ -9,11 +9,11 @@ namespace BarcodeStandard.Symbologies /// internal class EAN13 : BarcodeCommon, IBarcode { - private readonly string[] EAN_CodeA = { "0001101", "0011001", "0010011", "0111101", "0100011", "0110001", "0101111", "0111011", "0110111", "0001011" }; - private readonly string[] EAN_CodeB = { "0100111", "0110011", "0011011", "0100001", "0011101", "0111001", "0000101", "0010001", "0001001", "0010111" }; - private readonly string[] EAN_CodeC = { "1110010", "1100110", "1101100", "1000010", "1011100", "1001110", "1010000", "1000100", "1001000", "1110100" }; - private readonly string[] EAN_Pattern = { "aaaaaa", "aababb", "aabbab", "aabbba", "abaabb", "abbaab", "abbbaa", "ababab", "ababba", "abbaba" }; - private readonly Hashtable _countryCodes = new Hashtable(); //is initialized by init_CountryCodes() + private readonly string[] EAN_CodeA = ["0001101", "0011001", "0010011", "0111101", "0100011", "0110001", "0101111", "0111011", "0110111", "0001011"]; + private readonly string[] EAN_CodeB = ["0100111", "0110011", "0011011", "0100001", "0011101", "0111001", "0000101", "0010001", "0001001", "0010111"]; + private readonly string[] EAN_CodeC = ["1110010", "1100110", "1101100", "1000010", "1011100", "1001110", "1010000", "1000100", "1001000", "1110100"]; + private readonly string[] EAN_Pattern = ["aaaaaa", "aababb", "aabbab", "aabbba", "abaabb", "abbaab", "abbbaa", "ababab", "ababba", "abbaba"]; + private readonly Hashtable _countryCodes = []; //is initialized by init_CountryCodes() private string _countryAssigningManufacturerCode = "N/A"; public string CountryAssigningManufacturerCode { get => _countryAssigningManufacturerCode; set => _countryAssigningManufacturerCode = value; } @@ -58,7 +58,7 @@ private string Encode_EAN13() if (patterncode[pos] == 'b') result += EAN_CodeB[Int32.Parse(RawData[pos + 1].ToString())]; pos++; - }//while + } //add divider bars @@ -69,7 +69,7 @@ private string Encode_EAN13() while (pos <= 5) { result += EAN_CodeC[Int32.Parse(RawData[(pos++) + 6].ToString())]; - }//while + } //checksum digit var cs = Int32.Parse(RawData[RawData.Length - 1].ToString()); @@ -86,7 +86,7 @@ private string Encode_EAN13() } return result; - }//Encode_EAN13 + } private void ParseCountryCode() { @@ -122,8 +122,8 @@ private void Create_CountryCodeRange(int startingNumber, int endingNumber, strin for (var i = startingNumber; i <= endingNumber; i++) { _countryCodes.Add(i.ToString("00"), countryDescription); - } // for - } // create_CountryCodeRange + } + } private void Init_CountryCodes() { @@ -260,7 +260,7 @@ private void Init_CountryCodes() Create_CountryCodeRange(980, 980, "REFUND RECEIPTS"); Create_CountryCodeRange(981, 984, "GS1 COUPON IDENTIFICATION FOR COMMON CURRENCY AREAS"); Create_CountryCodeRange(990, 999, "GS1 COUPON IDENTIFICATION"); - }//init_CountryCodes + } private void CheckDigit() { try @@ -276,7 +276,7 @@ private void CheckDigit() odd += Int32.Parse(rawDataHolder.Substring(i, 1)); else even += Int32.Parse(rawDataHolder.Substring(i, 1)) * 3; - }//for + } var total = even + odd; var cs = total % 10; @@ -285,11 +285,11 @@ private void CheckDigit() cs = 0; RawData = rawDataHolder + cs.ToString()[0]; - }//try + } catch { Error("EEAN13-4: Error calculating check digit."); - }//catch + } } #region IBarcode Members diff --git a/BarcodeStandard/Symbologies/EAN8.cs b/BarcodeStandard/Symbologies/EAN8.cs index b634af8..3c68bfa 100644 --- a/BarcodeStandard/Symbologies/EAN8.cs +++ b/BarcodeStandard/Symbologies/EAN8.cs @@ -8,8 +8,8 @@ namespace BarcodeStandard.Symbologies /// internal class EAN8 : BarcodeCommon, IBarcode { - private readonly string[] EAN_CodeA = { "0001101", "0011001", "0010011", "0111101", "0100011", "0110001", "0101111", "0111011", "0110111", "0001011" }; - private readonly string[] EAN_CodeC = { "1110010", "1100110", "1101100", "1000010", "1011100", "1001110", "1010000", "1000100", "1001000", "1110100" }; + private readonly string[] EAN_CodeA = ["0001101", "0011001", "0010011", "0111101", "0100011", "0110001", "0101111", "0111011", "0110111", "0001011"]; + private readonly string[] EAN_CodeC = ["1110010", "1100110", "1101100", "1000010", "1011100", "1001110", "1010000", "1000100", "1001000", "1110100"]; internal EAN8(string input) { @@ -36,7 +36,7 @@ private string Encode_EAN8() for (int i = 0; i < RawData.Length / 2; i++) { result += EAN_CodeA[Int32.Parse(RawData[i].ToString())]; - }//for + } //center guard bars result += "01010"; @@ -45,12 +45,12 @@ private string Encode_EAN8() for (int i = RawData.Length / 2; i < RawData.Length; i++) { result += EAN_CodeC[Int32.Parse(RawData[i].ToString())]; - }//for + } result += "101"; return result; - }//Encode_EAN8 + } private void CheckDigit() { @@ -65,13 +65,13 @@ private void CheckDigit() for (int i = 0; i <= 6; i += 2) { odd += Int32.Parse(RawData.Substring(i, 1)) * 3; - }//for + } //even for (int i = 1; i <= 5; i += 2) { even += Int32.Parse(RawData.Substring(i, 1)); - }//for + } int total = even + odd; int checksum = total % 10; @@ -81,7 +81,7 @@ private void CheckDigit() //add the checksum to the end of the RawData += checksum.ToString(); - }//if + } } #region IBarcode Members diff --git a/BarcodeStandard/Symbologies/FIM.cs b/BarcodeStandard/Symbologies/FIM.cs index 9f239e8..0bdc292 100644 --- a/BarcodeStandard/Symbologies/FIM.cs +++ b/BarcodeStandard/Symbologies/FIM.cs @@ -6,7 +6,7 @@ /// internal class FIM : BarcodeCommon, IBarcode { - private readonly string[] FIM_Codes = { "110010011", "101101101", "110101011", "111010111", "101000101" }; + private readonly string[] FIM_Codes = ["110010011", "101101101", "110101011", "111010111", "101000101"]; public enum FIMTypes {FIM_A = 0, FIM_B, FIM_C, FIM_D, FIM_E}; internal FIM(string input) @@ -32,7 +32,7 @@ internal FIM(string input) break; default: Error("EFIM-1: Could not determine encoding type. (Only pass in A, B, C, D, or E)"); break; - }//switch + } } internal string Encode_FIM() @@ -41,7 +41,7 @@ internal string Encode_FIM() foreach (char c in RawData) { encoded += c + "0"; - }//foreach + } encoded = encoded.Substring(0, encoded.Length - 1); diff --git a/BarcodeStandard/Symbologies/IATA2of5.cs b/BarcodeStandard/Symbologies/IATA2of5.cs index 429ee28..c3bf67b 100644 --- a/BarcodeStandard/Symbologies/IATA2of5.cs +++ b/BarcodeStandard/Symbologies/IATA2of5.cs @@ -2,12 +2,12 @@ { internal class IATA2of5 : BarcodeCommon, IBarcode { - private readonly string[] IATA2of5_Code = { "10101110111010", "11101010101110", "10111010101110", "11101110101010", "10101110101110", "11101011101010", "10111011101010", "10101011101110", "11101010111010", "10111010111010" }; + private readonly string[] IATA2of5_Code = ["10101110111010", "11101010101110", "10111010101110", "11101110101010", "10101110101110", "11101011101010", "10111011101010", "10101011101110", "11101010111010", "10111010111010"]; internal IATA2of5(string input) { RawData = input; - }//Standard2of5 + } /// /// Encode the raw data using the IATA 2 of 5 algorithm. diff --git a/BarcodeStandard/Symbologies/ISBN.cs b/BarcodeStandard/Symbologies/ISBN.cs index 73357a2..5cbaf7b 100644 --- a/BarcodeStandard/Symbologies/ISBN.cs +++ b/BarcodeStandard/Symbologies/ISBN.cs @@ -26,15 +26,15 @@ private string Encode_ISBN_Bookland() { if (RawData.Length == 10) RawData = RawData.Remove(9, 1); RawData = "978" + RawData; - type = "ISBN"; //if + type = "ISBN"; break; } case 12 when RawData.StartsWith("978"): - type = "BOOKLAND-NOCHECKDIGIT"; //else if + type = "BOOKLAND-NOCHECKDIGIT"; break; case 13 when RawData.StartsWith("978"): type = "BOOKLAND-CHECKDIGIT"; - RawData = RawData.Remove(12, 1); //else if + RawData = RawData.Remove(12, 1); break; } @@ -43,7 +43,7 @@ private string Encode_ISBN_Bookland() var ean13 = new EAN13(RawData); return ean13.Encoded_Value; - }//Encode_ISBN_Bookland + } #region IBarcode Members diff --git a/BarcodeStandard/Symbologies/ITF14.cs b/BarcodeStandard/Symbologies/ITF14.cs index d021f44..7a3dfba 100644 --- a/BarcodeStandard/Symbologies/ITF14.cs +++ b/BarcodeStandard/Symbologies/ITF14.cs @@ -8,7 +8,7 @@ namespace BarcodeStandard.Symbologies /// internal class ITF14 : BarcodeCommon, IBarcode { - private readonly string[] ITF14_Code = { "NNWWN", "WNNNW", "NWNNW", "WWNNN", "NNWNW", "WNWNN", "NWWNN", "NNNWW", "WNNWN", "NWNWN" }; + private readonly string[] ITF14_Code = ["NNWWN", "WNNNW", "NWNNW", "WWNNN", "NNWNW", "WNWNN", "NWWNN", "NNNWW", "WNNWN", "NWNWN"]; internal ITF14(string input) { @@ -43,7 +43,7 @@ private string Encode_ITF14() patternmixed += patternbars[0].ToString() + patternspaces[0].ToString(); patternbars = patternbars.Substring(1); patternspaces = patternspaces.Substring(1); - }//while + } foreach (var c1 in patternmixed) { @@ -53,22 +53,22 @@ private string Encode_ITF14() result += "1"; else result += "11"; - }//if + } else { if (c1 == 'N') result += "0"; else result += "00"; - }//else + } bars = !bars; - }//foreach - }//foreach + } + } //add ending bars result += "1101"; return result; - }//Encode_ITF14 + } private void CheckDigit() { //calculate and include checksum if it is necessary @@ -80,7 +80,7 @@ private void CheckDigit() { var temp = Int32.Parse(RawData.Substring(i, 1)); total += temp * ((i == 0 || i % 2 == 0) ? 3 : 1); - }//for + } var cs = total % 10; cs = 10 - cs; @@ -88,7 +88,7 @@ private void CheckDigit() cs = 0; this.RawData += cs.ToString(); - }//if + } } #region IBarcode Members diff --git a/BarcodeStandard/Symbologies/Interleaved2of5.cs b/BarcodeStandard/Symbologies/Interleaved2of5.cs index 35c288e..7f44e80 100644 --- a/BarcodeStandard/Symbologies/Interleaved2of5.cs +++ b/BarcodeStandard/Symbologies/Interleaved2of5.cs @@ -8,7 +8,7 @@ namespace BarcodeStandard.Symbologies /// internal class Interleaved2of5 : BarcodeCommon, IBarcode { - private readonly string[] _i25Code = { "NNWWN", "WNNNW", "NWNNW", "WWNNN", "NNWNW", "WNWNN", "NWWNN", "NNNWW", "WNNWN", "NWNWN" }; + private readonly string[] _i25Code = ["NNWWN", "WNNNW", "NWNNW", "WWNNN", "NNWNW", "WNWNN", "NWWNN", "NNNWW", "WNNWN", "NWNWN"]; private readonly Type _encodedType; internal Interleaved2of5(string input, Type encodedType) @@ -44,7 +44,7 @@ private string Encode_Interleaved2of5() patternmixed += patternbars[0].ToString() + patternspaces[0].ToString(); patternbars = patternbars.Substring(1); patternspaces = patternspaces.Substring(1); - }//while + } foreach (char c1 in patternmixed) { @@ -54,22 +54,22 @@ private string Encode_Interleaved2of5() result += "1"; else result += "11"; - }//if + } else { if (c1 == 'N') result += "0"; else result += "00"; - }//else + } bars = !bars; - }//foreach - }//foreach + } + } //add ending bars result += "1101"; return result; - }//Encode_Interleaved2of5 + } private int CalculateMod10CheckDigit() { diff --git a/BarcodeStandard/Symbologies/JAN13.cs b/BarcodeStandard/Symbologies/JAN13.cs index 0b95f84..e3d3795 100644 --- a/BarcodeStandard/Symbologies/JAN13.cs +++ b/BarcodeStandard/Symbologies/JAN13.cs @@ -19,9 +19,9 @@ private string Encode_JAN13() if (!IsNumericOnly(RawData)) Error("EJAN13-2: Numeric Data Only"); - EAN13 ean13 = new EAN13(RawData); + EAN13 ean13 = new(RawData); return ean13.Encoded_Value; - }//Encode_JAN13 + } #region IBarcode Members diff --git a/BarcodeStandard/Symbologies/MSI.cs b/BarcodeStandard/Symbologies/MSI.cs index 2c7a3bd..1bf7e68 100644 --- a/BarcodeStandard/Symbologies/MSI.cs +++ b/BarcodeStandard/Symbologies/MSI.cs @@ -8,14 +8,14 @@ internal class MSI : BarcodeCommon, IBarcode /// MSI encoding /// Written by: Brad Barnhill /// - private readonly string[] MSI_Code = { "100100100100", "100100100110", "100100110100", "100100110110", "100110100100", "100110100110", "100110110100", "100110110110", "110100100100", "110100100110" }; - private Type Encoded_Type = Type.Unspecified; + private readonly string[] MSI_Code = ["100100100100", "100100100110", "100100110100", "100100110110", "100110100100", "100110100110", "100110110100", "100110110110", "110100100100", "110100100110"]; + private readonly Type Encoded_Type = Type.Unspecified; internal MSI(string input, Type encodedType) { Encoded_Type = encodedType; RawData = input; - }//MSI + } /// /// Encode the raw data using the MSI algorithm. @@ -43,13 +43,13 @@ private string Encode_MSI() foreach (var c in withChecksum) { result += MSI_Code[Int32.Parse(c.ToString())]; - }//foreach + } //add stop character result += "1001"; return result; - }//Encode_MSI + } private string Mod10(string code) { @@ -60,7 +60,7 @@ private string Mod10(string code) odds = code[i] + odds; if (i - 1 >= 0) evens = code[i - 1] + evens; - }//for + } //multiply odds by 2 odds = Convert.ToString((Int32.Parse(odds) * 2)); @@ -84,7 +84,7 @@ private string Mod11(string code) { if (weight > 7) weight = 2; sum += Int32.Parse(code[i].ToString()) * weight++; - }//foreach + } var mod = sum % 11; var checksum = mod == 0 ? 0 : 11 - mod; @@ -96,5 +96,5 @@ private string Mod11(string code) public string Encoded_Value => Encode_MSI(); #endregion - }//class -}//namepsace + } +} diff --git a/BarcodeStandard/Symbologies/Pharmacode.cs b/BarcodeStandard/Symbologies/Pharmacode.cs index a0fa891..11014ed 100644 --- a/BarcodeStandard/Symbologies/Pharmacode.cs +++ b/BarcodeStandard/Symbologies/Pharmacode.cs @@ -8,9 +8,9 @@ namespace BarcodeStandard.Symbologies /// internal class Pharmacode : BarcodeCommon, IBarcode { - string _thinBar = "1"; - string _gap = "00"; - string _thickBar = "111"; + readonly string _thinBar = "1"; + readonly string _gap = "00"; + readonly string _thickBar = "111"; /// /// Encodes with Pharmacode. @@ -23,11 +23,11 @@ internal Pharmacode(string input) if (!IsNumericOnly(RawData)) { Error("EPHARM-1: Data contains invalid characters (non-numeric)."); - }//if + } else if (RawData.Length > 6) { Error("EPHARM-2: Data too long (invalid data input length)."); - }//if + } } /// @@ -35,16 +35,14 @@ internal Pharmacode(string input) /// private string Encode_Pharmacode() { - int num; - - if (!Int32.TryParse(RawData, out num)) + if (!Int32.TryParse(RawData, out int num)) { Error("EPHARM-3: Input is unparseable."); } else if (num < 3 || num > 131070) { Error("EPHARM-4: Data contains invalid characters (invalid numeric range)."); - }//if + } var result = String.Empty; do diff --git a/BarcodeStandard/Symbologies/Postnet.cs b/BarcodeStandard/Symbologies/Postnet.cs index 9b1c7a2..b0d0bcc 100644 --- a/BarcodeStandard/Symbologies/Postnet.cs +++ b/BarcodeStandard/Symbologies/Postnet.cs @@ -8,12 +8,12 @@ namespace BarcodeStandard.Symbologies /// internal class Postnet : BarcodeCommon, IBarcode { - private readonly string[] POSTNET_Code = { "11000", "00011", "00101", "00110", "01001", "01010", "01100", "10001", "10010", "10100" }; + private readonly string[] POSTNET_Code = ["11000", "00011", "00101", "00110", "01001", "01010", "01100", "10001", "10010", "10100"]; internal Postnet(string input) { RawData = input; - }//Postnet + } /// /// Encode the raw data using the PostNet algorithm. @@ -31,7 +31,7 @@ private string Encode_Postnet() case 11: break; default: Error("EPOSTNET-2: Invalid data length. (5, 6, 9, or 11 digits only)"); break; - }//switch + } //Note: 0 = half bar and 1 = full bar //initialize the result with the starting bar @@ -45,12 +45,12 @@ private string Encode_Postnet() var index = Convert.ToInt32(c.ToString()); result += POSTNET_Code[index]; checkdigitsum += index; - }//try + } catch (Exception ex) { Error("EPOSTNET-2: Invalid data. (Numeric only) --> " + ex.Message); - }//catch - }//foreach + } + } //calculate and add check digit var temp = checkdigitsum % 10; @@ -62,12 +62,12 @@ private string Encode_Postnet() result += "1"; return result; - }//Encode_PostNet + } #region IBarcode Members public string Encoded_Value => Encode_Postnet(); #endregion - }//class -}//namespace + } +} diff --git a/BarcodeStandard/Symbologies/Standard2of5.cs b/BarcodeStandard/Symbologies/Standard2of5.cs index c524feb..52f2533 100644 --- a/BarcodeStandard/Symbologies/Standard2of5.cs +++ b/BarcodeStandard/Symbologies/Standard2of5.cs @@ -6,14 +6,14 @@ namespace BarcodeStandard.Symbologies /// internal class Standard2of5 : BarcodeCommon, IBarcode { - private readonly string[] S25_Code = { "10101110111010", "11101010101110", "10111010101110", "11101110101010", "10101110101110", "11101011101010", "10111011101010", "10101011101110", "11101010111010", "10111010111010" }; + private readonly string[] S25_Code = ["10101110111010", "11101010101110", "10111010101110", "11101110101010", "10101110101110", "11101011101010", "10111011101010", "10101011101110", "11101010111010", "10111010111010"]; private readonly Type _encodedType = Type.Unspecified; internal Standard2of5(string input, Type encodedType) { RawData = input; _encodedType = encodedType; - }//Standard2of5 + } /// /// Encode the raw data using the Standard 2 of 5 algorithm. @@ -35,7 +35,7 @@ private string Encode_Standard2of5() //add ending bars result += "1101011"; return result; - }//Encode_Standard2of5 + } private int CalculateMod10CheckDigit() { diff --git a/BarcodeStandard/Symbologies/Telepen.cs b/BarcodeStandard/Symbologies/Telepen.cs index 798f3e6..c08dee1 100644 --- a/BarcodeStandard/Symbologies/Telepen.cs +++ b/BarcodeStandard/Symbologies/Telepen.cs @@ -9,7 +9,7 @@ namespace BarcodeStandard.Symbologies /// internal class Telepen : BarcodeCommon, IBarcode { - private static readonly Hashtable Telepen_Code = new Hashtable(); + private static readonly Hashtable Telepen_Code = []; private enum StartStopCode : int { START1, STOP1, START2, STOP2, START3, STOP3 }; private StartStopCode _startCode = StartStopCode.START1; private StartStopCode _stopCode = StartStopCode.STOP1; @@ -35,13 +35,10 @@ private string Encode_Telepen() Init_Telepen(); _iCheckSum = 0; - var result = ""; - SetEncodingSequence(); //include the Start sequence pattern - result = Telepen_Code[_startCode].ToString(); - + string result = Telepen_Code[_startCode].ToString(); switch (_startCode) { //numeric --> ascii @@ -52,7 +49,7 @@ private string Encode_Telepen() { EncodeSwitchMode(ref result); EncodeASCII(RawData.Substring(_switchModeIndex), ref result); - }//if + } break; //ascii --> numeric case StartStopCode.START3: @@ -64,7 +61,7 @@ private string Encode_Telepen() default: EncodeASCII(RawData, ref result); break; - }//switch + } //checksum result += Telepen_Code[Calculate_Checksum(_iCheckSum)]; @@ -73,7 +70,7 @@ private string Encode_Telepen() result += Telepen_Code[_stopCode]; return result; - }//Encode_Telepen + } private void EncodeASCII(string input, ref string output) { @@ -83,12 +80,12 @@ private void EncodeASCII(string input, ref string output) { output += Telepen_Code[c]; _iCheckSum += Convert.ToInt32(c); - }//foreach - }//try + } + } catch { Error("ETELEPEN-1: Invalid data when encoding ASCII"); - }//catch + } } private void EncodeNumeric(string input, ref string output) { @@ -101,12 +98,12 @@ private void EncodeNumeric(string input, ref string output) { output += Telepen_Code[Convert.ToChar(Int32.Parse(input.Substring(i, 2)) + 27)]; _iCheckSum += Int32.Parse(input.Substring(i, 2)) + 27; - }//for - }//try + } + } catch { Error("ETELEPEN-2: Numeric encoding failed"); - }//catch + } } private void EncodeSwitchMode(ref string output) { @@ -118,7 +115,7 @@ private void EncodeSwitchMode(ref string output) private char Calculate_Checksum(int iCheckSum) { return Convert.ToChar(127 - (iCheckSum % 127)); - }//Calculate_Checksum(string) + } private void SetEncodingSequence() { @@ -135,7 +132,7 @@ private void SetEncodingSequence() StartNumerics++; else break; - }//foreach + } if (StartNumerics == RawData.Length) { @@ -145,7 +142,7 @@ private void SetEncodingSequence() if ((RawData.Length % 2) > 0) _switchModeIndex = RawData.Length - 1; - }//if + } else { //ending number of numbers @@ -156,7 +153,7 @@ private void SetEncodingSequence() EndNumerics++; else break; - }//for + } if (StartNumerics >= 4 || EndNumerics >= 4) { @@ -167,17 +164,17 @@ private void SetEncodingSequence() _startCode = StartStopCode.START2; _stopCode = StartStopCode.STOP2; _switchModeIndex = (StartNumerics % 2) == 1 ? StartNumerics - 1 : StartNumerics; - }//if + } else { //start in ascii switching to numeric _startCode = StartStopCode.START3; _stopCode = StartStopCode.STOP3; _switchModeIndex = (EndNumerics % 2) == 1 ? RawData.Length - EndNumerics + 1 : RawData.Length - EndNumerics; - }//else - }//if - }//else - }//SetEncodingSequence + } + } + } + } private void Init_Telepen() { diff --git a/BarcodeStandard/Symbologies/UPCA.cs b/BarcodeStandard/Symbologies/UPCA.cs index 37d12c4..dabadfb 100644 --- a/BarcodeStandard/Symbologies/UPCA.cs +++ b/BarcodeStandard/Symbologies/UPCA.cs @@ -9,10 +9,8 @@ namespace BarcodeStandard.Symbologies /// internal class UPCA : BarcodeCommon, IBarcode { - private readonly string[] UPC_Code_A = { "0001101", "0011001", "0010011", "0111101", "0100011", "0110001", "0101111", "0111011", "0110111", "0001011" }; - private readonly string[] UPC_Code_B = { "1110010", "1100110", "1101100", "1000010", "1011100", "1001110", "1010000", "1000100", "1001000", "1110100" }; - private string _countryAssigningManufacturerCode = "N/A"; - private readonly Hashtable _countryCodes = new Hashtable(); //is initialized by init_CountryCodes() + private readonly string[] UPC_Code_A = ["0001101", "0011001", "0010011", "0111101", "0100011", "0110001", "0101111", "0111011", "0110111", "0001011"]; + private readonly string[] UPC_Code_B = ["1110010", "1100110", "1101100", "1000010", "1011100", "1001110", "1010000", "1000100", "1001000", "1110100"]; internal UPCA(string input) { @@ -43,7 +41,7 @@ private string Encode_UPCA() { result += UPC_Code_A[Int32.Parse(RawData[pos + 1].ToString())]; pos++; - }//while + } //add divider bars result += "01010"; @@ -53,7 +51,7 @@ private string Encode_UPCA() while (pos < 5) { result += UPC_Code_B[Int32.Parse(RawData[(pos++) + 6].ToString())]; - }//while + } //forth result += UPC_Code_B[Int32.Parse(RawData[RawData.Length - 1].ToString())]; @@ -61,170 +59,9 @@ private string Encode_UPCA() //add ending guard bars result += "101"; - //get the manufacturer assigning country - init_CountryCodes(); - var twodigitCode = "0" + RawData.Substring(0, 1); - try - { - _countryAssigningManufacturerCode = _countryCodes[twodigitCode].ToString(); - }//try - catch - { - Error("EUPCA-3: Country assigning manufacturer code not found."); - }//catch - finally { _countryCodes.Clear(); } - return result; - }//Encode_UPCA - private void init_CountryCodes() - { - _countryCodes.Clear(); - _countryCodes.Add("00", "US / CANADA"); - _countryCodes.Add("01", "US / CANADA"); - _countryCodes.Add("02", "US / CANADA"); - _countryCodes.Add("03", "US / CANADA"); - _countryCodes.Add("04", "US / CANADA"); - _countryCodes.Add("05", "US / CANADA"); - _countryCodes.Add("06", "US / CANADA"); - _countryCodes.Add("07", "US / CANADA"); - _countryCodes.Add("08", "US / CANADA"); - _countryCodes.Add("09", "US / CANADA"); - _countryCodes.Add("10", "US / CANADA"); - _countryCodes.Add("11", "US / CANADA"); - _countryCodes.Add("12", "US / CANADA"); - _countryCodes.Add("13", "US / CANADA"); - - _countryCodes.Add("20", "IN STORE"); - _countryCodes.Add("21", "IN STORE"); - _countryCodes.Add("22", "IN STORE"); - _countryCodes.Add("23", "IN STORE"); - _countryCodes.Add("24", "IN STORE"); - _countryCodes.Add("25", "IN STORE"); - _countryCodes.Add("26", "IN STORE"); - _countryCodes.Add("27", "IN STORE"); - _countryCodes.Add("28", "IN STORE"); - _countryCodes.Add("29", "IN STORE"); - - _countryCodes.Add("30", "FRANCE"); - _countryCodes.Add("31", "FRANCE"); - _countryCodes.Add("32", "FRANCE"); - _countryCodes.Add("33", "FRANCE"); - _countryCodes.Add("34", "FRANCE"); - _countryCodes.Add("35", "FRANCE"); - _countryCodes.Add("36", "FRANCE"); - _countryCodes.Add("37", "FRANCE"); - - _countryCodes.Add("40", "GERMANY"); - _countryCodes.Add("41", "GERMANY"); - _countryCodes.Add("42", "GERMANY"); - _countryCodes.Add("43", "GERMANY"); - _countryCodes.Add("44", "GERMANY"); - - _countryCodes.Add("45", "JAPAN"); - _countryCodes.Add("46", "RUSSIAN FEDERATION"); - _countryCodes.Add("49", "JAPAN (JAN-13)"); - - _countryCodes.Add("50", "UNITED KINGDOM"); - _countryCodes.Add("54", "BELGIUM / LUXEMBOURG"); - _countryCodes.Add("57", "DENMARK"); - - _countryCodes.Add("64", "FINLAND"); - - _countryCodes.Add("70", "NORWAY"); - _countryCodes.Add("73", "SWEDEN"); - _countryCodes.Add("76", "SWITZERLAND"); - - _countryCodes.Add("80", "ITALY"); - _countryCodes.Add("81", "ITALY"); - _countryCodes.Add("82", "ITALY"); - _countryCodes.Add("83", "ITALY"); - _countryCodes.Add("84", "SPAIN"); - _countryCodes.Add("87", "NETHERLANDS"); - - _countryCodes.Add("90", "AUSTRIA"); - _countryCodes.Add("91", "AUSTRIA"); - _countryCodes.Add("93", "AUSTRALIA"); - _countryCodes.Add("94", "NEW ZEALAND"); - _countryCodes.Add("99", "COUPONS"); - - _countryCodes.Add("471", "TAIWAN"); - _countryCodes.Add("474", "ESTONIA"); - _countryCodes.Add("475", "LATVIA"); - _countryCodes.Add("477", "LITHUANIA"); - _countryCodes.Add("479", "SRI LANKA"); - _countryCodes.Add("480", "PHILIPPINES"); - _countryCodes.Add("482", "UKRAINE"); - _countryCodes.Add("484", "MOLDOVA"); - _countryCodes.Add("485", "ARMENIA"); - _countryCodes.Add("486", "GEORGIA"); - _countryCodes.Add("487", "KAZAKHSTAN"); - _countryCodes.Add("489", "HONG KONG"); - - _countryCodes.Add("520", "GREECE"); - _countryCodes.Add("528", "LEBANON"); - _countryCodes.Add("529", "CYPRUS"); - _countryCodes.Add("531", "MACEDONIA"); - _countryCodes.Add("535", "MALTA"); - _countryCodes.Add("539", "IRELAND"); - _countryCodes.Add("560", "PORTUGAL"); - _countryCodes.Add("569", "ICELAND"); - _countryCodes.Add("590", "POLAND"); - _countryCodes.Add("594", "ROMANIA"); - _countryCodes.Add("599", "HUNGARY"); - - _countryCodes.Add("600", "SOUTH AFRICA"); - _countryCodes.Add("601", "SOUTH AFRICA"); - _countryCodes.Add("609", "MAURITIUS"); - _countryCodes.Add("611", "MOROCCO"); - _countryCodes.Add("613", "ALGERIA"); - _countryCodes.Add("619", "TUNISIA"); - _countryCodes.Add("622", "EGYPT"); - _countryCodes.Add("625", "JORDAN"); - _countryCodes.Add("626", "IRAN"); - _countryCodes.Add("690", "CHINA"); - _countryCodes.Add("691", "CHINA"); - _countryCodes.Add("692", "CHINA"); - - _countryCodes.Add("729", "ISRAEL"); - _countryCodes.Add("740", "GUATEMALA"); - _countryCodes.Add("741", "EL SALVADOR"); - _countryCodes.Add("742", "HONDURAS"); - _countryCodes.Add("743", "NICARAGUA"); - _countryCodes.Add("744", "COSTA RICA"); - _countryCodes.Add("746", "DOMINICAN REPUBLIC"); - _countryCodes.Add("750", "MEXICO"); - _countryCodes.Add("759", "VENEZUELA"); - _countryCodes.Add("770", "COLOMBIA"); - _countryCodes.Add("773", "URUGUAY"); - _countryCodes.Add("775", "PERU"); - _countryCodes.Add("777", "BOLIVIA"); - _countryCodes.Add("779", "ARGENTINA"); - _countryCodes.Add("780", "CHILE"); - _countryCodes.Add("784", "PARAGUAY"); - _countryCodes.Add("785", "PERU"); - _countryCodes.Add("786", "ECUADOR"); - _countryCodes.Add("789", "BRAZIL"); - - _countryCodes.Add("850", "CUBA"); - _countryCodes.Add("858", "SLOVAKIA"); - _countryCodes.Add("859", "CZECH REPUBLIC"); - _countryCodes.Add("860", "YUGLOSLAVIA"); - _countryCodes.Add("869", "TURKEY"); - _countryCodes.Add("880", "SOUTH KOREA"); - _countryCodes.Add("885", "THAILAND"); - _countryCodes.Add("888", "SINGAPORE"); - _countryCodes.Add("890", "INDIA"); - _countryCodes.Add("893", "VIETNAM"); - _countryCodes.Add("899", "INDONESIA"); - - _countryCodes.Add("955", "MALAYSIA"); - _countryCodes.Add("977", "INTERNATIONAL STANDARD SERIAL NUMBER FOR PERIODICALS (ISSN)"); - _countryCodes.Add("978", "INTERNATIONAL STANDARD BOOK NUMBERING (ISBN)"); - _countryCodes.Add("979", "INTERNATIONAL STANDARD MUSIC NUMBER (ISMN)"); - _countryCodes.Add("980", "REFUND RECEIPTS"); - _countryCodes.Add("981", "COMMON CURRENCY COUPONS"); - _countryCodes.Add("982", "COMMON CURRENCY COUPONS"); - }//init_CountryCodes + } + private void CheckDigit() { try @@ -240,18 +77,18 @@ private void CheckDigit() sum += Int32.Parse(rawDataHolder.Substring(i, 1)) * 3; else sum += Int32.Parse(rawDataHolder.Substring(i, 1)); - }//for + } int cs = (10 - sum % 10) % 10; //replace checksum if provided by the user and replace with the calculated checksum RawData = rawDataHolder + cs; - }//try + } catch { Error("EUPCA-4: Error calculating check digit."); - }//catch - }//CheckDigit + } + } #region IBarcode Members diff --git a/BarcodeStandard/Symbologies/UPCE.cs b/BarcodeStandard/Symbologies/UPCE.cs index 97b6beb..50aa409 100644 --- a/BarcodeStandard/Symbologies/UPCE.cs +++ b/BarcodeStandard/Symbologies/UPCE.cs @@ -10,10 +10,10 @@ namespace BarcodeStandard.Symbologies /// internal class UPCE : BarcodeCommon, IBarcode { - private readonly string[] EAN_Code_A = { "0001101", "0011001", "0010011", "0111101", "0100011", "0110001", "0101111", "0111011", "0110111", "0001011" }; - private readonly string[] EAN_Code_B = { "0100111", "0110011", "0011011", "0100001", "0011101", "0111001", "0000101", "0010001", "0001001", "0010111" }; - private readonly string[] UPC_E_Code0 = { "bbbaaa", "bbabaa", "bbaaba", "bbaaab", "babbaa", "baabba", "baaabb", "bababa", "babaab", "baabab" }; - private readonly string[] UPC_E_Code1 = { "aaabbb", "aababb", "aabbab", "aabbba", "abaabb", "abbaab", "abbbaa", "ababab", "ababba", "abbaba" }; + private readonly string[] EAN_Code_A = ["0001101", "0011001", "0010011", "0111101", "0100011", "0110001", "0101111", "0111011", "0110111", "0001011"]; + private readonly string[] EAN_Code_B = ["0100111", "0110011", "0011011", "0100001", "0011101", "0111001", "0000101", "0010001", "0001001", "0010111"]; + private readonly string[] UPC_E_Code0 = ["bbbaaa", "bbabaa", "bbaaba", "bbaaab", "babbaa", "baabba", "baaabb", "bababa", "babaab", "baabab"]; + private readonly string[] UPC_E_Code1 = ["aaabbb", "aababb", "aabbab", "aabbba", "abaabb", "abbaab", "abbbaa", "ababab", "ababba", "abbaba"]; /// /// Encodes a UPC-E symbol. @@ -50,16 +50,16 @@ private string Encode_UPCE() if (RawData.Length == 12) { numberSystem = Int16.Parse(RawData[0].ToString()); - RawData = convertUPCAToUPCE(); + RawData = ConvertUPCAToUPCE(); } - int checkDigit = Int16.Parse(calculateCheckDigit(convertUPCEToUPCA(RawData))); + int checkDigit = Int16.Parse(CalculateCheckDigit(ConvertUPCEToUPCA(RawData))); //get encoding pattern - String pattern = getPattern(checkDigit, numberSystem); + String pattern = GetPattern(checkDigit, numberSystem); //encode the data - StringBuilder result = new StringBuilder("101"); + StringBuilder result = new("101"); int pos = 0; foreach (char c in pattern) @@ -79,9 +79,9 @@ private string Encode_UPCE() result.Append("010101"); return result.ToString(); - }//Encode_UPCE + } - private String getPattern(int checkDigit, int numberSystem) + private String GetPattern(int checkDigit, int numberSystem) { if (numberSystem != 0 && numberSystem != 1) { @@ -102,7 +102,7 @@ private String getPattern(int checkDigit, int numberSystem) return pattern; } - private String convertUPCAToUPCE() + private String ConvertUPCAToUPCE() { String UPCECode = ""; @@ -147,7 +147,7 @@ private String convertUPCAToUPCE() return UPCECode; } - private String convertUPCEToUPCA(String UPCECode) + private String ConvertUPCEToUPCA(String UPCECode) { String UPCACode = "0"; if (UPCECode.EndsWith("0") || UPCECode.EndsWith("1") || UPCECode.EndsWith("2")) @@ -178,7 +178,7 @@ private String convertUPCEToUPCA(String UPCECode) return UPCACode; } - private String calculateCheckDigit(String upcA) + private String CalculateCheckDigit(String upcA) { int cs = 0; try @@ -201,7 +201,7 @@ private String calculateCheckDigit(String upcA) cs = (10 - sum % 10) % 10; } - catch (Exception ex) + catch (Exception) { Error("EUPCE-5: Error calculating check digit."); } diff --git a/BarcodeStandard/Symbologies/UPCSupplement2.cs b/BarcodeStandard/Symbologies/UPCSupplement2.cs index cb85c77..0afaf5e 100644 --- a/BarcodeStandard/Symbologies/UPCSupplement2.cs +++ b/BarcodeStandard/Symbologies/UPCSupplement2.cs @@ -8,9 +8,9 @@ namespace BarcodeStandard.Symbologies /// internal class UPCSupplement2 : BarcodeCommon, IBarcode { - private readonly string [] EAN_CodeA = { "0001101", "0011001", "0010011", "0111101", "0100011", "0110001", "0101111", "0111011", "0110111", "0001011" }; - private readonly string [] EAN_CodeB = { "0100111", "0110011", "0011011", "0100001", "0011101", "0111001", "0000101", "0010001", "0001001", "0010111" }; - private readonly string[] UPC_SUPP_2 = { "aa", "ab", "ba", "bb" }; + private readonly string [] EAN_CodeA = ["0001101", "0011001", "0010011", "0111101", "0100011", "0110001", "0101111", "0111011", "0110111", "0001011"]; + private readonly string [] EAN_CodeB = ["0100111", "0110011", "0011011", "0100001", "0011101", "0111001", "0000101", "0010001", "0001001", "0010111"]; + private readonly string[] UPC_SUPP_2 = ["aa", "ab", "ba", "bb"]; internal UPCSupplement2(string input) { @@ -32,7 +32,7 @@ private string Encode_UPCSupplemental_2() try { pattern = this.UPC_SUPP_2[Int32.Parse(RawData.Trim()) % 4]; - }//try + } catch { Error("EUPC-SUP2-3: Invalid Data. (Numeric only)"); } string result = "1011"; @@ -44,22 +44,22 @@ private string Encode_UPCSupplemental_2() { //encode using odd parity result += EAN_CodeA[Int32.Parse(RawData[pos].ToString())]; - }//if + } else if (c == 'b') { //encode using even parity result += EAN_CodeB[Int32.Parse(RawData[pos].ToString())]; - }//else if + } if (pos++ == 0) result += "01"; //Inter-character separator - }//foreach + } return result; - }//Encode_UPSSupplemental_2 + } #region IBarcode Members public string Encoded_Value => Encode_UPCSupplemental_2(); #endregion - }//class -}//namespace + } +} diff --git a/BarcodeStandard/Symbologies/UPCSupplement5.cs b/BarcodeStandard/Symbologies/UPCSupplement5.cs index e49fc85..6743ab9 100644 --- a/BarcodeStandard/Symbologies/UPCSupplement5.cs +++ b/BarcodeStandard/Symbologies/UPCSupplement5.cs @@ -8,9 +8,9 @@ namespace BarcodeStandard.Symbologies /// internal class UPCSupplement5 : BarcodeCommon, IBarcode { - private readonly string[] EAN_CodeA = { "0001101", "0011001", "0010011", "0111101", "0100011", "0110001", "0101111", "0111011", "0110111", "0001011" }; - private readonly string[] EAN_CodeB = { "0100111", "0110011", "0011011", "0100001", "0011101", "0111001", "0000101", "0010001", "0001001", "0010111" }; - private readonly string[] UPC_SUPP_5 = { "bbaaa", "babaa", "baaba", "baaab", "abbaa", "aabba", "aaabb", "ababa", "abaab", "aabab" }; + private readonly string[] EAN_CodeA = ["0001101", "0011001", "0010011", "0111101", "0100011", "0110001", "0101111", "0111011", "0110111", "0001011"]; + private readonly string[] EAN_CodeB = ["0100111", "0110011", "0011011", "0100001", "0011101", "0111001", "0000101", "0010001", "0001001", "0010111"]; + private readonly string[] UPC_SUPP_5 = ["bbaaa", "babaa", "baaba", "baaab", "abbaa", "aabba", "aaabb", "ababa", "abaab", "aabab"]; internal UPCSupplement5(string input) { @@ -35,13 +35,13 @@ private string Encode_UPCSupplemental_5() for (var i = 0; i <= 4; i += 2) { odd += Int32.Parse(RawData.Substring(i, 1)) * 3; - }//for + } //even for (var i = 1; i < 4; i += 2) { even += Int32.Parse(RawData.Substring(i, 1)) * 9; - }//for + } var total = even + odd; var cs = total % 10; @@ -61,22 +61,22 @@ private string Encode_UPCSupplemental_5() { case 'a': //encode using odd parity - result += EAN_CodeA[Int32.Parse(RawData[pos].ToString())]; //if + result += EAN_CodeA[Int32.Parse(RawData[pos].ToString())]; break; case 'b': //encode using even parity - result += EAN_CodeB[Int32.Parse(RawData[pos].ToString())]; //else if + result += EAN_CodeB[Int32.Parse(RawData[pos].ToString())]; break; } pos++; - }//foreach + } return result; - }//Encode_UPCSupplemental_5 + } #region IBarcode Members public string Encoded_Value => Encode_UPCSupplemental_5(); #endregion - }//class -}//namespace + } +} diff --git a/BarcodeStandardExample/BarcodeStandardExample.csproj b/BarcodeStandardExample/BarcodeStandardExample.csproj index fbd9873..9acdba9 100644 --- a/BarcodeStandardExample/BarcodeStandardExample.csproj +++ b/BarcodeStandardExample/BarcodeStandardExample.csproj @@ -104,16 +104,16 @@ - 2.88.6 + 2.88.8 - 2.88.6 + 2.88.8 - 2.88.6 + 2.88.8 - 8.0.0-rc.1.23419.4 + 8.0.0 diff --git a/BarcodeStandardExample/TestApp.cs b/BarcodeStandardExample/TestApp.cs index 5059806..8783fa9 100644 --- a/BarcodeStandardExample/TestApp.cs +++ b/BarcodeStandardExample/TestApp.cs @@ -16,7 +16,7 @@ namespace BarcodeStandardExample /// public partial class TestApp : Form { - Barcode _b = new Barcode(); + readonly Barcode _b = new(); public TestApp() { @@ -48,7 +48,7 @@ private void btnEncode_Click(object sender, EventArgs e) case "left": _b.Alignment = AlignmentPositions.Left; break; case "right": _b.Alignment = AlignmentPositions.Right; break; default: _b.Alignment = AlignmentPositions.Center; break; - }//switch + } var type = GetTypeSelected(); @@ -95,15 +95,15 @@ private void btnEncode_Click(object sender, EventArgs e) txtWidth.Text = _b.Width.ToString(); if (_b.AspectRatio.HasValue) txtHeight.Text = _b.Height.ToString(); - }//if + } //reposition the barcode image to the middle barcode.Location = new Point((barcode.Location.X + barcode.Width / 2) - barcode.Width / 2, (barcode.Location.Y + barcode.Height / 2) - barcode.Height / 2); - }//try + } catch (Exception ex) { MessageBox.Show(ex.Message); - }//catch + } }//btnEncode_Click private void btnSave_Click(object sender, EventArgs e) @@ -119,7 +119,7 @@ private void btnSave_Click(object sender, EventArgs e) case 1: /* JPG */ type = SaveTypes.Jpg; break; case 2: /* PNG */ type = SaveTypes.Png; break; case 3: /* WEBP*/ type = SaveTypes.Webp; break; - }//switch + } _b.SaveImage(sfd.FileName, type); }//btnSave_Click @@ -162,7 +162,7 @@ private Type GetTypeSelected() case "Pharmacode": type = Type.Pharmacode; break; case "IATA2of5": type = Type.IATA2of5; break; default: MessageBox.Show(@"Please specify the encoding type."); break; - }//switch + } return type; } @@ -181,7 +181,7 @@ private void btnForeColor_Click(object sender, EventArgs e) { _b.ForeColor = new SKColor(colorDialog.Color.R, colorDialog.Color.G, colorDialog.Color.B, colorDialog.Color.A); btnForeColor.BackColor = colorDialog.Color; - }//if + } }//using }//btnForeColor_Click @@ -193,7 +193,7 @@ private void btnBackColor_Click(object sender, EventArgs e) if (colorDialog.ShowDialog() != DialogResult.OK) return; _b.BackColor = new SKColor(colorDialog.Color.R, colorDialog.Color.G, colorDialog.Color.B, colorDialog.Color.A); btnBackColor.BackColor = colorDialog.Color; - //if + }//using }//btnBackColor_Click @@ -209,7 +209,7 @@ private void btnSaveJSON_Click(object sender, EventArgs e) { sw.Write(_b.ToJson(chkIncludeImageInSavedData.Checked)); }//using - //if + }//using } @@ -225,7 +225,7 @@ private void btnLoadJSON_Click(object sender, EventArgs e) { LoadFromSaveData(savedData); }//using - }//if + } }//using //populate the local object @@ -262,7 +262,7 @@ private void btnLoadXML_Click(object sender, EventArgs e) { LoadFromSaveData(savedData); }//using - }//if + } }//using //populate the local object @@ -392,7 +392,7 @@ private void LoadFromSaveData(SaveData saveData) break; default: throw new Exception("ELOADXML-1: Unsupported encoding type in XML."); - }//switch + } } private void btnMassGeneration_Click(object sender, EventArgs e) @@ -449,5 +449,5 @@ private static SKBitmap GetBarCode(Barcode b, string codeNumber, Type type, int return null; } } - }//class -}//namespace \ No newline at end of file + } +} \ No newline at end of file diff --git a/BarcodeStandardTests/BarcodeStandardTests.csproj b/BarcodeStandardTests/BarcodeStandardTests.csproj index 5945132..ca64147 100644 --- a/BarcodeStandardTests/BarcodeStandardTests.csproj +++ b/BarcodeStandardTests/BarcodeStandardTests.csproj @@ -7,13 +7,13 @@ - - + + - - - - + + + + all runtime; build; native; contentfiles; analyzers; buildtransitive @@ -23,4 +23,8 @@ + + + +