在今天的快速业务环境中,条码技术在简化操作和提高数据管理方面起着至关重要的作用. 各种条形码标准中 GS1-128 因其多样性和详细产品信息编码能力而在各个行业广泛使用。

完整的例子

这个终结的例子:

  • 使用常见 AIs 构建有效的 GS1 序列: (01) GTIN-14, (17) Expiry YYMMDD*、 () Batch/Lot* 和 (21) Serial
  • 确认长度/格式。
  • 提供 GS1-128 图像与印刷友好的尺寸/边缘。
  • 保存 PNG 文件。 您可以通过硬代码值或通过命令线条。
// File: Program.cs
// Compile: dotnet add package Aspose.BarCode && dotnet build
// Run (examples):
//   dotnet run --project . -- 1234567890123 260930 ABC123 SN-00987
//   dotnet run --project . -- 400638133393 260930 LOT-77 SER-42   (GTIN will be padded to 14 digits)

using System;
using Aspose.BarCode.Generation;

namespace GS1_128_BarcodeExample
{
    class Program
    {
        static int Main(string[] args)
        {
            try
            {
                // ---------------------------
                // 1) Read inputs (or defaults)
                // ---------------------------
                // Args: [0]=GTIN (≤14 digits), [1]=EXP YYMMDD, [2]=BATCH (var len), [3]=SERIAL (var len)
                string gtinRaw    = args.Length > 0 ? args[0] : "1234567890123";
                string expYyMmDd  = args.Length > 1 ? args[1] : "260930";       // 2026-09-30
                string batchLot   = args.Length > 2 ? args[2] : "ABC123";
                string serial     = args.Length > 3 ? args[3] : "SN-00987";

                // ---------------------------
                // 2) Normalize & validate
                // ---------------------------
                // Ensure GTIN is 14 digits (pad left with zeros when shorter)
                if (gtinRaw.Length > 14 || !IsAllDigits(gtinRaw))
                    throw new ArgumentException("GTIN must be numeric and ≤ 14 digits.");

                string gtin14 = gtinRaw.PadLeft(14, '0');
                if (gtin14.Length != 14) throw new ArgumentException("GTIN must be exactly 14 digits after padding.");

                // Optional (advanced): you can calculate or verify GTIN check digit here if desired.

                if (!IsValidYyMmDd(expYyMmDd))
                    throw new ArgumentException("(17) Expiration must be YYMMDD and represent a valid calendar date.");

                // Variable-length AIs (10) & (21) can be any non-empty strings; keep them short & scanner-friendly.
                if (string.IsNullOrWhiteSpace(batchLot)) throw new ArgumentException("(10) Batch/Lot cannot be empty.");
                if (string.IsNullOrWhiteSpace(serial))   throw new ArgumentException("(21) Serial cannot be empty.");

                // ---------------------------
                // 3) Compose GS1 code text
                // ---------------------------
                // Parentheses are human-readable; the library handles FNC1 as needed.
                string gs1Text = $"(01){gtin14}(17){expYyMmDd}(10){batchLot}(21){serial}";

                // ---------------------------
                // 4) Configure generator
                // ---------------------------
                using (var generator = new BarCodeGenerator(EncodeTypes.GS1_128, gs1Text))
                {
                    // Minimum module (bar) thickness — increase for thermal printers / rough media
                    generator.Parameters.Barcode.XDimension.Pixels = 3;

                    // Symbol height (for 1D-like linear symbol height inside GS1-128 area)
                    generator.Parameters.Barcode.BarHeight.Millimeters = 22f;

                    // Target output size (entire image). Adjust per label stock / DPI.
                    generator.Parameters.Barcode.ImageWidth.Inches  = 2.8f;
                    generator.Parameters.Barcode.ImageHeight.Inches = 1.2f;

                    // Quiet zones (margins) — critical for scan reliability
                    generator.Parameters.Barcode.LeftMargin.Millimeters   = 4f;
                    generator.Parameters.Barcode.RightMargin.Millimeters  = 4f;
                    generator.Parameters.Barcode.TopMargin.Millimeters    = 2f;
                    generator.Parameters.Barcode.BottomMargin.Millimeters = 2f;

                    // Human-readable text placement and formatting
                    generator.Parameters.Barcode.CodeTextParameters.Location = CodeLocation.Below;
                    generator.Parameters.Barcode.CodeTextParameters.FontSize.Point = 8f;
                    generator.Parameters.Barcode.CodeTextParameters.Space.Millimeters = 1.0f;

                    // ---------------------------
                    // 5) Save image (by extension)
                    // ---------------------------
                    string fileName = $"GS1_128_{gtin14}_{batchLot}_{serial}.png";
                    generator.Save(fileName);
                    Console.WriteLine($"✅ GS1-128 barcode saved: {fileName}");
                }

                return 0;
            }
            catch (Exception ex)
            {
                Console.Error.WriteLine("❌ Error: " + ex.Message);
                Console.Error.WriteLine("Usage: <exe> <gtin≤14digits> <expYYMMDD> <batch> <serial>");
                return 1;
            }
        }

        // ---- Helpers ---------------------------------------------------------

        // Minimal YYMMDD validation (1900–2099 windowing for simplicity)
        static bool IsValidYyMmDd(string yymmdd)
        {
            if (string.IsNullOrWhiteSpace(yymmdd) || yymmdd.Length != 6) return false;
            if (!IsAllDigits(yymmdd)) return false;

            int yy = int.Parse(yymmdd.Substring(0, 2));
            int mm = int.Parse(yymmdd.Substring(2, 2));
            int dd = int.Parse(yymmdd.Substring(4, 2));

            int year = (yy >= 0 && yy <= 79) ? 2000 + yy : 1900 + yy; // simple window
            try
            {
                var _ = new DateTime(year, mm, dd);
                return true;
            }
            catch
            {
                return false;
            }
        }

        static bool IsAllDigits(string s)
        {
            foreach (char c in s)
                if (c < '0' || c > '9') return false;
            return true;
        }

        // Optional: GTIN-14 check digit calculator (Mod10). Use if you build GTIN from the first 13 digits.
        // static char CalcGtin14CheckDigit(string first13Digits) { ... }
    }
}

此分類上一篇: Building & Run

dotnet new console -n GS1_128_BarcodeExample -f net8.0
cd GS1_128_BarcodeExample
dotnet add package Aspose.BarCode
# Replace Program.cs with the code above, then:
dotnet run -- 1234567890123 260930 ABC123 SN-00987

PNG 输出将写入您的工作目录。

步骤指南

第一步:了解GS1-128基础

GS1-128 是基于 ** 代码 128** 的符号,使用 ** Application Identifiers (AI)** 编码数据,每个 AI 都定义了下列(数据类型和长度)。

常见的:

  • (01) GTIN-14(固定14个字符;如果更短的话,左侧有零点)
  • (17) 终止日期(YYMMDD)
  • (10) Batch/Lot(可变长度)
  • (21) 系列(可变长度)

**FNC1处理:当您通过人可读的序列与偏见(例如, (01)1234...(10)LOT图书馆按 GS1 规则自动输入 FNC1 分离器 - 特别需要一个 ** 变长 AI 由另一个 AI 跟随时。

步骤2:设置条形码设置

使用 XDimension(模块厚度), BarHeightmargins来平衡印刷密度和扫描仪宽容。

generator.Parameters.Barcode.XDimension.Pixels = 3;
generator.Parameters.Barcode.BarHeight.Millimeters = 22f;
generator.Parameters.Barcode.LeftMargin.Millimeters = 4f;
generator.Parameters.Barcode.RightMargin.Millimeters = 4f;
generator.Parameters.Barcode.TopMargin.Millimeters = 2f;
generator.Parameters.Barcode.BottomMargin.Millimeters = 2f;

步骤3:定义应用识别器(AI)

将 GTIN 插入 14 个数字,格式日期为 YYMMDD ,并保持变量长度的 AIs 紧密(避免空间/控制电缆)。

string gtin14 = gtinRaw.PadLeft(14, '0');
string gs1Text = $"(01){gtin14}(17){expYyMmDd}(10){batchLot}(21){serial}";

步骤4:设置条形码文本

您可以在构建器中设置 GS1 文本或以后通过 generator.CodeText例子将其放在建筑师中,并显示如何在需要时重新分配。

步骤5:自定义外观

决定是否在字符串下显示 人可阅读的代码文本(CodeLocation.Below)或删除它,如果您的标签配置在其他地方打印文本。

generator.Parameters.Barcode.CodeTextParameters.Location = CodeLocation.Below;
generator.Parameters.Barcode.CodeTextParameters.FontSize.Point = 8f;
generator.Parameters.Barcode.CodeTextParameters.Space.Millimeters = 1.0f;

步骤6:创建和保存

以延伸(.png, .jpg, .bmp对于标签工作流, PNG 通常是最好的(无损)。

generator.Save("GS1_128_ProductLabel.png");

实用技巧 & Gotchas

  • Quiet zones matter: 如果一个扫描仪冲突,增加左/右边边界和XD尺寸稍微。
  • ** 热打印机:** 年龄/服装可以粉碎精致的线条。 XDimension (例如,3 - 4 px) 保持媒體清潔。
  • GTIN 检查数字: 如果您正在构建 GTIN,请计算/验证 Mod10 查看数字以防止现场错误。
  • 日期窗口: 示例中的YY窗格是简单的;与您的业务规则相匹配(例如,只有20xx)。
  • ** 版本控制:** 在配置中存储尺寸/边缘参数,以便在环境中可重复条形码。

结论

使用 Aspose.BarCode for .NET,创建 符合标准的GS1-128条码是简单的. 定义您的AI,验证格式,图印参数,并出口一个清洁的PNG准备标签。

More in this category