From 5f91f0f212f985f7bab82b4ba0a68b8baef98308 Mon Sep 17 00:00:00 2001 From: Joel Wikberg <17063038+jwikberg@users.noreply.github.com> Date: Fri, 30 Dec 2022 10:23:10 +0100 Subject: [PATCH 1/4] Added support for setting left margin print position --- ESCPOS_NET.ConsoleTest/Program.cs | 5 +++++ ESCPOS_NET.ConsoleTest/TestPrintPosition.cs | 20 +++++++++++++++++++ .../PrintPositionCommands.cs | 9 +++++++++ .../BaseCommandValues/PrintPosition.cs | 7 +++++++ ESCPOS_NET/Emitters/ICommandEmitter.cs | 3 +++ 5 files changed, 44 insertions(+) create mode 100644 ESCPOS_NET.ConsoleTest/TestPrintPosition.cs create mode 100644 ESCPOS_NET/Emitters/BaseCommandEmitter/PrintPositionCommands.cs create mode 100644 ESCPOS_NET/Emitters/BaseCommandValues/PrintPosition.cs diff --git a/ESCPOS_NET.ConsoleTest/Program.cs b/ESCPOS_NET.ConsoleTest/Program.cs index 4e6c60c..841010c 100644 --- a/ESCPOS_NET.ConsoleTest/Program.cs +++ b/ESCPOS_NET.ConsoleTest/Program.cs @@ -130,6 +130,7 @@ static void Main(string[] args) { Option.LargeByteArrays, "Large Byte Arrays" }, { Option.CashDrawerPin2, "Cash Drawer Pin2" }, { Option.CashDrawerPin5, "Cash Drawer Pin5" }, + { Option.PrintPosition, "Print Position" }, { Option.Exit, "Exit" } }; @@ -213,6 +214,9 @@ static void Main(string[] args) case Option.CashDrawerPin5: printer.Write(Tests.CashDrawerOpenPin5(e)); break; + case Option.PrintPosition: + printer.Write(Tests.Position(e)); + break; default: Console.WriteLine("Invalid entry."); break; @@ -242,6 +246,7 @@ public enum Option LargeByteArrays, CashDrawerPin2, CashDrawerPin5, + PrintPosition, Exit = 99 } diff --git a/ESCPOS_NET.ConsoleTest/TestPrintPosition.cs b/ESCPOS_NET.ConsoleTest/TestPrintPosition.cs new file mode 100644 index 0000000..9a3d25e --- /dev/null +++ b/ESCPOS_NET.ConsoleTest/TestPrintPosition.cs @@ -0,0 +1,20 @@ +using ESCPOS_NET.Emitters; + +namespace ESCPOS_NET.ConsoleTest +{ + public static partial class Tests + { + public static byte[][] Position(ICommandEmitter e) => new byte[][] { + e.SetLeftMargin(0), + e.PrintLine("Left Margin: This is 0 left margin."), + e.SetLeftMargin(10), + e.PrintLine("Left Margin: This is 10 left margin."), + e.SetLeftMargin(20), + e.PrintLine("Left Margin: This is 20 left margin."), + e.SetLeftMargin(30), + e.PrintLine("Left Margin: This is 30 left margin."), + e.SetLeftMargin(0), + e.PrintLine("Left Margin: This is 0 left margin."), + }; + } +} diff --git a/ESCPOS_NET/Emitters/BaseCommandEmitter/PrintPositionCommands.cs b/ESCPOS_NET/Emitters/BaseCommandEmitter/PrintPositionCommands.cs new file mode 100644 index 0000000..3deff20 --- /dev/null +++ b/ESCPOS_NET/Emitters/BaseCommandEmitter/PrintPositionCommands.cs @@ -0,0 +1,9 @@ +using ESCPOS_NET.Emitters.BaseCommandValues; + +namespace ESCPOS_NET.Emitters +{ + public abstract partial class BaseCommandEmitter : ICommandEmitter + { + public virtual byte[] SetLeftMargin(int leftMargin) => new byte[] { Cmd.GS, PrintPosition.LeftMargin, (byte)leftMargin, 0x0 }; + } +} diff --git a/ESCPOS_NET/Emitters/BaseCommandValues/PrintPosition.cs b/ESCPOS_NET/Emitters/BaseCommandValues/PrintPosition.cs new file mode 100644 index 0000000..09acf50 --- /dev/null +++ b/ESCPOS_NET/Emitters/BaseCommandValues/PrintPosition.cs @@ -0,0 +1,7 @@ +namespace ESCPOS_NET.Emitters.BaseCommandValues +{ + public static class PrintPosition + { + public static readonly byte LeftMargin = 0x4C; + } +} diff --git a/ESCPOS_NET/Emitters/ICommandEmitter.cs b/ESCPOS_NET/Emitters/ICommandEmitter.cs index 11de893..972eb67 100644 --- a/ESCPOS_NET/Emitters/ICommandEmitter.cs +++ b/ESCPOS_NET/Emitters/ICommandEmitter.cs @@ -102,5 +102,8 @@ public interface ICommandEmitter byte[] SetBarLabelPosition(BarLabelPrintPosition position); byte[] SetBarLabelFontB(bool fontB); + + /* Print Position Commands */ + byte[] SetLeftMargin(int leftMargin); } } From eec310888880782fa02d548df985857337b11dc5 Mon Sep 17 00:00:00 2001 From: Joel Wikberg <17063038+jwikberg@users.noreply.github.com> Date: Fri, 30 Dec 2022 10:24:16 +0100 Subject: [PATCH 2/4] Added support for printing Aztec codes --- ESCPOS_NET.ConsoleTest/Test2DCodes.cs | 13 ++++- .../BaseCommandEmitter/BarcodeCommands.cs | 31 +++++++++++ .../Emitters/BaseCommandValues/Barcodes.cs | 6 +++ ESCPOS_NET/Emitters/Enums/2DCode.cs | 7 +++ ESCPOS_NET/Emitters/ICommandEmitter.cs | 53 +++++++++++++++++-- 5 files changed, 106 insertions(+), 4 deletions(-) diff --git a/ESCPOS_NET.ConsoleTest/Test2DCodes.cs b/ESCPOS_NET.ConsoleTest/Test2DCodes.cs index fbecce0..3e4d305 100644 --- a/ESCPOS_NET.ConsoleTest/Test2DCodes.cs +++ b/ESCPOS_NET.ConsoleTest/Test2DCodes.cs @@ -1,10 +1,13 @@ using ESCPOS_NET.Emitters; +using System; +using System.Text; namespace ESCPOS_NET.ConsoleTest { public static partial class Tests { private const string websiteString = "https://github.com/lukevp/ESC-POS-.NET/"; + public static byte[][] TwoDimensionCodes(ICommandEmitter e) => new byte[][] { e.PrintLine("PDF417:"), e.Print2DCode(TwoDimensionCodeType.PDF417, websiteString), @@ -36,7 +39,15 @@ public static partial class Tests e.PrintLine("QRCODE MODEL 1 (LARGE):"), e.Print2DCode(TwoDimensionCodeType.QRCODE_MODEL1, websiteString, Size2DCode.LARGE), - e.PrintLine() + e.PrintLine(), + + e.PrintLine("AZTEC CODE (FULL_RANGE):"), + e.PrintAztecCode(websiteString, ModeTypeAztecCode.FULL_RANGE ), + e.PrintLine(), + + e.PrintLine("AZTEC CODE (COMPACT):"), + e.PrintAztecCode(websiteString, ModeTypeAztecCode.COMPACT), + e.PrintLine(), }; } } diff --git a/ESCPOS_NET/Emitters/BaseCommandEmitter/BarcodeCommands.cs b/ESCPOS_NET/Emitters/BaseCommandEmitter/BarcodeCommands.cs index 63f27ae..94f0982 100644 --- a/ESCPOS_NET/Emitters/BaseCommandEmitter/BarcodeCommands.cs +++ b/ESCPOS_NET/Emitters/BaseCommandEmitter/BarcodeCommands.cs @@ -11,6 +11,7 @@ namespace ESCPOS_NET.Emitters public abstract partial class BaseCommandEmitter : ICommandEmitter { /* Barcode Commands */ + public virtual byte[] PrintBarcode(BarcodeType type, string barcode, BarcodeCode code = BarcodeCode.CODE_B) { DataValidator.ValidateBarcode(type, code, barcode); @@ -111,5 +112,35 @@ protected virtual byte[] TwoDimensionCodeBytes(TwoDimensionCodeType type, string public virtual byte[] SetBarLabelPosition(BarLabelPrintPosition position) => new byte[] { Cmd.GS, Barcodes.SetBarLabelPosition, (byte)position }; public virtual byte[] SetBarLabelFontB(bool fontB) => new byte[] { Cmd.GS, Barcodes.SetBarLabelFont, (byte)(fontB ? 1 : 0) }; + + /// + public virtual byte[] PrintAztecCode(string data, ModeTypeAztecCode modeType = ModeTypeAztecCode.FULL_RANGE, int size = 3, int correctionLevel = 23, int numberOfDataLayers = 0) + { + var bytes = data.ToCharArray().Select(x => (byte)x).ToArray(); + return PrintAztecCode(bytes, modeType, size, correctionLevel, numberOfDataLayers); + } + + /// + public virtual byte[] PrintAztecCode(byte[] data, ModeTypeAztecCode modeType = ModeTypeAztecCode.FULL_RANGE, int size = 3, int correctionLevel = 23, int numberOfDataLayers = 0) + { + List command = new List(); + byte[] initial = { Cmd.GS, Barcodes.Set2DCode, Barcodes.PrintBarcode }; + + command.AddRange(initial, Barcodes.SetAztecCodeModeTypeAndNumberOfDataLayers, (byte)modeType, (byte)numberOfDataLayers); + command.AddRange(initial, Barcodes.SetAztecCodeSizeOfModule, (byte)size); + command.AddRange(initial, Barcodes.SetAztecCodeErrorCorrectionLevel, (byte)correctionLevel); + + int num = data.Length + 3; + int pH = num / 256; + int pL = num % 256; + + command.AddRange(initial, (byte)pL, (byte)pH, Barcodes.StoreAztecCodeData); + command.AddRange(data); + + // Prints stored Aztec code + command.AddRange(initial, Barcodes.PrintAztecCode); + + return command.ToArray(); + } } } diff --git a/ESCPOS_NET/Emitters/BaseCommandValues/Barcodes.cs b/ESCPOS_NET/Emitters/BaseCommandValues/Barcodes.cs index 3b1fd80..3209ffd 100644 --- a/ESCPOS_NET/Emitters/BaseCommandValues/Barcodes.cs +++ b/ESCPOS_NET/Emitters/BaseCommandValues/Barcodes.cs @@ -22,5 +22,11 @@ public static class Barcodes public static readonly byte[] SetQRCodeCorrectionLevel = { 0x03, 0x00, 0x31, 0x45 }; public static readonly byte[] StoreQRCodeData = { 0x31, 0x50, 0x30 }; public static readonly byte[] PrintQRCode = { 0x03, 0x00, 0x31, 0x51, 0x30 }; + + public static readonly byte[] SetAztecCodeModeTypeAndNumberOfDataLayers = { 0x04, 0x00, 0x35, 0x42 }; + public static readonly byte[] SetAztecCodeSizeOfModule = { 0x03, 0x00, 0x35, 0x43 }; + public static readonly byte[] SetAztecCodeErrorCorrectionLevel = { 0x03, 0x00, 0x35, 0x45 }; + public static readonly byte[] StoreAztecCodeData = { 0x35, 0x50, 0x30 }; + public static readonly byte[] PrintAztecCode = { 0x03, 0x00, 0x35, 0x51, 0x30 }; } } diff --git a/ESCPOS_NET/Emitters/Enums/2DCode.cs b/ESCPOS_NET/Emitters/Enums/2DCode.cs index f1616c8..07ed019 100644 --- a/ESCPOS_NET/Emitters/Enums/2DCode.cs +++ b/ESCPOS_NET/Emitters/Enums/2DCode.cs @@ -27,4 +27,11 @@ public enum CorrectionLevel2DCode PERCENT_25 = 50, PERCENT_30 = 51, } + + [System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.SpacingRules", "SA1025:Code should not contain multiple whitespace in a row", Justification = "Enums are easier to read if they have whitespace alignment.")] + public enum ModeTypeAztecCode + { + FULL_RANGE = 48, + COMPACT = 49, + } } diff --git a/ESCPOS_NET/Emitters/ICommandEmitter.cs b/ESCPOS_NET/Emitters/ICommandEmitter.cs index 972eb67..d7027e5 100644 --- a/ESCPOS_NET/Emitters/ICommandEmitter.cs +++ b/ESCPOS_NET/Emitters/ICommandEmitter.cs @@ -1,5 +1,3 @@ -using ESCPOS_NET.Emitters.BaseCommandValues; - namespace ESCPOS_NET.Emitters { public interface ICommandEmitter @@ -86,7 +84,6 @@ public interface ICommandEmitter byte[] RequestInkStatus(); - /* Barcode Commands */ byte[] PrintBarcode(BarcodeType type, string barcode, BarcodeCode code = BarcodeCode.CODE_B); @@ -103,6 +100,56 @@ public interface ICommandEmitter byte[] SetBarLabelFontB(bool fontB); + /// + /// Print Aztec Code + /// + /// + /// Data to print as Aztec Code. + /// + /// + /// The mode type for Aztec Code. + /// Default is FULL_RANGE. + /// + /// + /// The size of one module of Aztec Code in dot units, valid range is 2-16. + /// Default is 3. + /// + /// + /// The error correction level in percent, valid range is 5-95. + /// Default is 23. + /// + /// + /// The number of data layers for Aztec Code. + /// 0 = automatic processing for the number of layers, valid range is 0-32. + /// Default is 0. + /// + byte[] PrintAztecCode(string data, ModeTypeAztecCode modeType = ModeTypeAztecCode.FULL_RANGE, int size = 3, int correctionLevel = 23, int numberOfDataLayers = 0); + + /// + /// Print Aztec Code + /// + /// + /// Data to print as Aztec Code. + /// + /// + /// The mode type for Aztec Code. + /// Default is FULL_RANGE. + /// + /// + /// The size of one module of Aztec Code in dot units, valid range is 2-16. + /// Default is 3. + /// + /// + /// The error correction level in percent, valid range is 5-95. + /// Default is 23. + /// + /// + /// The number of data layers for Aztec Code. + /// 0 = automatic processing for the number of layers, valid range is 0-32. + /// Default is 0. + /// + byte[] PrintAztecCode(byte[] data, ModeTypeAztecCode modeType = ModeTypeAztecCode.FULL_RANGE, int size = 3, int correctionLevel = 23, int numberOfDataLayers = 0); + /* Print Position Commands */ byte[] SetLeftMargin(int leftMargin); } From 47eb1d507bef2539875c811e7518ec00519347e5 Mon Sep 17 00:00:00 2001 From: Joel Wikberg <17063038+jwikberg@users.noreply.github.com> Date: Sun, 19 Nov 2023 14:24:22 +0100 Subject: [PATCH 3/4] Added Aztec code validation --- .../DataValidation/AztecDataConstraint.cs | 21 +++++++ .../DataValidation/AztecDataValidator.cs | 63 +++++++++++++++++++ .../BaseCommandEmitter/BarcodeCommands.cs | 10 +-- .../PrintPositionCommands.cs | 15 ++++- ESCPOS_NET/Emitters/ICommandEmitter.cs | 8 +-- 5 files changed, 108 insertions(+), 9 deletions(-) create mode 100644 ESCPOS_NET/DataValidation/AztecDataConstraint.cs create mode 100644 ESCPOS_NET/DataValidation/AztecDataValidator.cs diff --git a/ESCPOS_NET/DataValidation/AztecDataConstraint.cs b/ESCPOS_NET/DataValidation/AztecDataConstraint.cs new file mode 100644 index 0000000..9cd6a31 --- /dev/null +++ b/ESCPOS_NET/DataValidation/AztecDataConstraint.cs @@ -0,0 +1,21 @@ +namespace ESCPOS_NET.DataValidation +{ + public class AztecDataConstraint + { + public int MinLayers { get; set; } + + public int MaxLayers { get; set; } + + public int MinModuleSize { get; set; } + + public int MaxModuleSize { get; set; } + + public int MinCorrectionLevel { get; set; } + + public int MaxCorrectionLevel { get; set; } + + public int MinDataSize { get; set; } + + public int MaxDataSize { get; set; } + } +} diff --git a/ESCPOS_NET/DataValidation/AztecDataValidator.cs b/ESCPOS_NET/DataValidation/AztecDataValidator.cs new file mode 100644 index 0000000..bc8c23e --- /dev/null +++ b/ESCPOS_NET/DataValidation/AztecDataValidator.cs @@ -0,0 +1,63 @@ +using ESCPOS_NET.Emitters; +using System; +using System.Collections.Generic; + +namespace ESCPOS_NET.DataValidation +{ + public static class AztecDataValidator + { + private static Dictionary _constraints = new Dictionary + { + { ModeTypeAztecCode.FULL_RANGE, new AztecDataConstraint { MinLayers = 4, MaxLayers = 32, MinModuleSize = 2, MaxModuleSize = 16, MinCorrectionLevel = 5, MaxCorrectionLevel = 95 } }, + { ModeTypeAztecCode.COMPACT, new AztecDataConstraint { MinLayers = 1, MaxLayers = 4, MinModuleSize = 2, MaxModuleSize = 16, MinCorrectionLevel = 5, MaxCorrectionLevel = 95 } } + }; + + public static void ValidateAztecCode(ModeTypeAztecCode type, byte[] data, int moduleSize, int correctionLevel, int numberOfDataLayers) + { + if (data is null) + { + throw new ArgumentNullException(nameof(data)); + } + + // Validate constraints on aztec code. + _constraints.TryGetValue(type, out var constraints); + if (constraints is null) + { + return; + } + + // Check data layers. 0 = automatic processing. + if (numberOfDataLayers != 0) + { + if (numberOfDataLayers < constraints.MinLayers) + { + throw new ArgumentException($"Number of data layers '{numberOfDataLayers}' is lower than the minimum {constraints.MinLayers} for {type}."); + } + else if (constraints.MaxLayers < numberOfDataLayers) + { + throw new ArgumentException($"Number of data layers '{numberOfDataLayers}' is higher than the maximum {constraints.MaxLayers} for {type}."); + } + } + + // Check module size + if (moduleSize < constraints.MinModuleSize) + { + throw new ArgumentException($"Module size '{moduleSize}' is lower than the minimum {constraints.MinModuleSize} for {type}."); + } + else if (constraints.MaxModuleSize < moduleSize) + { + throw new ArgumentException($"Module size '{moduleSize}' is higher than the minimum {constraints.MaxModuleSize} for {type}."); + } + + // Check correction level + if (correctionLevel < constraints.MinCorrectionLevel) + { + throw new ArgumentException($"Correction level '{correctionLevel}' is lower than the minimum {constraints.MinCorrectionLevel} for {type}."); + } + else if (constraints.MaxCorrectionLevel < correctionLevel) + { + throw new ArgumentException($"Correction level '{correctionLevel}' is higher than the minimum {constraints.MaxCorrectionLevel} for {type}."); + } + } + } +} diff --git a/ESCPOS_NET/Emitters/BaseCommandEmitter/BarcodeCommands.cs b/ESCPOS_NET/Emitters/BaseCommandEmitter/BarcodeCommands.cs index 94f0982..791ba66 100644 --- a/ESCPOS_NET/Emitters/BaseCommandEmitter/BarcodeCommands.cs +++ b/ESCPOS_NET/Emitters/BaseCommandEmitter/BarcodeCommands.cs @@ -114,20 +114,22 @@ protected virtual byte[] TwoDimensionCodeBytes(TwoDimensionCodeType type, string public virtual byte[] SetBarLabelFontB(bool fontB) => new byte[] { Cmd.GS, Barcodes.SetBarLabelFont, (byte)(fontB ? 1 : 0) }; /// - public virtual byte[] PrintAztecCode(string data, ModeTypeAztecCode modeType = ModeTypeAztecCode.FULL_RANGE, int size = 3, int correctionLevel = 23, int numberOfDataLayers = 0) + public virtual byte[] PrintAztecCode(string data, ModeTypeAztecCode modeType = ModeTypeAztecCode.FULL_RANGE, int moduleSize = 3, int correctionLevel = 23, int numberOfDataLayers = 0) { var bytes = data.ToCharArray().Select(x => (byte)x).ToArray(); - return PrintAztecCode(bytes, modeType, size, correctionLevel, numberOfDataLayers); + return PrintAztecCode(bytes, modeType, moduleSize, correctionLevel, numberOfDataLayers); } /// - public virtual byte[] PrintAztecCode(byte[] data, ModeTypeAztecCode modeType = ModeTypeAztecCode.FULL_RANGE, int size = 3, int correctionLevel = 23, int numberOfDataLayers = 0) + public virtual byte[] PrintAztecCode(byte[] data, ModeTypeAztecCode modeType = ModeTypeAztecCode.FULL_RANGE, int moduleSize = 3, int correctionLevel = 23, int numberOfDataLayers = 0) { + AztecDataValidator.ValidateAztecCode(modeType, data, moduleSize, correctionLevel, numberOfDataLayers); + List command = new List(); byte[] initial = { Cmd.GS, Barcodes.Set2DCode, Barcodes.PrintBarcode }; command.AddRange(initial, Barcodes.SetAztecCodeModeTypeAndNumberOfDataLayers, (byte)modeType, (byte)numberOfDataLayers); - command.AddRange(initial, Barcodes.SetAztecCodeSizeOfModule, (byte)size); + command.AddRange(initial, Barcodes.SetAztecCodeSizeOfModule, (byte)moduleSize); command.AddRange(initial, Barcodes.SetAztecCodeErrorCorrectionLevel, (byte)correctionLevel); int num = data.Length + 3; diff --git a/ESCPOS_NET/Emitters/BaseCommandEmitter/PrintPositionCommands.cs b/ESCPOS_NET/Emitters/BaseCommandEmitter/PrintPositionCommands.cs index 3deff20..34f637f 100644 --- a/ESCPOS_NET/Emitters/BaseCommandEmitter/PrintPositionCommands.cs +++ b/ESCPOS_NET/Emitters/BaseCommandEmitter/PrintPositionCommands.cs @@ -1,9 +1,22 @@ using ESCPOS_NET.Emitters.BaseCommandValues; +using System; namespace ESCPOS_NET.Emitters { public abstract partial class BaseCommandEmitter : ICommandEmitter { - public virtual byte[] SetLeftMargin(int leftMargin) => new byte[] { Cmd.GS, PrintPosition.LeftMargin, (byte)leftMargin, 0x0 }; + public virtual byte[] SetLeftMargin(int leftMargin) + { + if (leftMargin < 0) + { + throw new ArgumentException($"Left margin '{leftMargin}' is lower than the minimum 0."); + } + else if (65535 < leftMargin) + { + throw new ArgumentException($"Left margin '{leftMargin}' is higher than the maximum 65535."); + } + + return new byte[] { Cmd.GS, PrintPosition.LeftMargin, (byte)leftMargin, 0x0 }; + } } } diff --git a/ESCPOS_NET/Emitters/ICommandEmitter.cs b/ESCPOS_NET/Emitters/ICommandEmitter.cs index d7027e5..9a3f6fa 100644 --- a/ESCPOS_NET/Emitters/ICommandEmitter.cs +++ b/ESCPOS_NET/Emitters/ICommandEmitter.cs @@ -110,7 +110,7 @@ public interface ICommandEmitter /// The mode type for Aztec Code. /// Default is FULL_RANGE. /// - /// + /// /// The size of one module of Aztec Code in dot units, valid range is 2-16. /// Default is 3. /// @@ -123,7 +123,7 @@ public interface ICommandEmitter /// 0 = automatic processing for the number of layers, valid range is 0-32. /// Default is 0. /// - byte[] PrintAztecCode(string data, ModeTypeAztecCode modeType = ModeTypeAztecCode.FULL_RANGE, int size = 3, int correctionLevel = 23, int numberOfDataLayers = 0); + byte[] PrintAztecCode(string data, ModeTypeAztecCode modeType = ModeTypeAztecCode.FULL_RANGE, int moduleSize = 3, int correctionLevel = 23, int numberOfDataLayers = 0); /// /// Print Aztec Code @@ -135,7 +135,7 @@ public interface ICommandEmitter /// The mode type for Aztec Code. /// Default is FULL_RANGE. /// - /// + /// /// The size of one module of Aztec Code in dot units, valid range is 2-16. /// Default is 3. /// @@ -148,7 +148,7 @@ public interface ICommandEmitter /// 0 = automatic processing for the number of layers, valid range is 0-32. /// Default is 0. /// - byte[] PrintAztecCode(byte[] data, ModeTypeAztecCode modeType = ModeTypeAztecCode.FULL_RANGE, int size = 3, int correctionLevel = 23, int numberOfDataLayers = 0); + byte[] PrintAztecCode(byte[] data, ModeTypeAztecCode modeType = ModeTypeAztecCode.FULL_RANGE, int moduleSize = 3, int correctionLevel = 23, int numberOfDataLayers = 0); /* Print Position Commands */ byte[] SetLeftMargin(int leftMargin); From 55a14825d111fc0ec84aa7b84f5f541e030305d4 Mon Sep 17 00:00:00 2001 From: Joel Wikberg <17063038+jwikberg@users.noreply.github.com> Date: Wed, 3 Jan 2024 14:01:54 +0100 Subject: [PATCH 4/4] Validate Aztec data using ZXing.Net --- ESCPOS_NET/DataValidation/AztecDataConstraint.cs | 6 +----- ESCPOS_NET/DataValidation/AztecDataValidator.cs | 12 +++++++++++- ESCPOS_NET/ESCPOS_NET.csproj | 1 + 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/ESCPOS_NET/DataValidation/AztecDataConstraint.cs b/ESCPOS_NET/DataValidation/AztecDataConstraint.cs index 9cd6a31..cac25f2 100644 --- a/ESCPOS_NET/DataValidation/AztecDataConstraint.cs +++ b/ESCPOS_NET/DataValidation/AztecDataConstraint.cs @@ -13,9 +13,5 @@ public class AztecDataConstraint public int MinCorrectionLevel { get; set; } public int MaxCorrectionLevel { get; set; } - - public int MinDataSize { get; set; } - - public int MaxDataSize { get; set; } } -} +} \ No newline at end of file diff --git a/ESCPOS_NET/DataValidation/AztecDataValidator.cs b/ESCPOS_NET/DataValidation/AztecDataValidator.cs index bc8c23e..be005db 100644 --- a/ESCPOS_NET/DataValidation/AztecDataValidator.cs +++ b/ESCPOS_NET/DataValidation/AztecDataValidator.cs @@ -58,6 +58,16 @@ public static void ValidateAztecCode(ModeTypeAztecCode type, byte[] data, int mo { throw new ArgumentException($"Correction level '{correctionLevel}' is higher than the minimum {constraints.MaxCorrectionLevel} for {type}."); } + + // Try encoding Aztec using ZXing.Net to validate data size + try + { + ZXing.Aztec.Internal.Encoder.encode(data, correctionLevel, numberOfDataLayers); + } + catch (ArgumentException) + { + throw; + } } } -} +} \ No newline at end of file diff --git a/ESCPOS_NET/ESCPOS_NET.csproj b/ESCPOS_NET/ESCPOS_NET.csproj index 1ed71d2..9aa2207 100644 --- a/ESCPOS_NET/ESCPOS_NET.csproj +++ b/ESCPOS_NET/ESCPOS_NET.csproj @@ -36,6 +36,7 @@ +