ในสภาพแวดล้อมธุรกิจที่รวดเร็วในวันนี้เทคโนโลยีบาร์โค้ดมีบทบาทสําคัญในการปรับปรุงการดําเนินงานและเพิ่มประสิทธิภาพการจัดการข้อมูล ระหว่างมาตรฐานต่างๆของรหัสบอร์ด GS1-128 ใช้กันอย่างแพร่หลายในอุตสาหกรรมเพื่อความหลากหลายและความสามารถในการเข้ารหัสข้อมูลผลิตภัณฑ์รายละเอียด แนะนํานี้ช่วยให้คุณผ่านการสร้างร่องรัด GS1-128 ด้วย Aspose.BarCode สําหรับ .NET - จากการเข้าใจ Application Identifiers (AIs) เพื่อปรับแต่งรูปร่างและส่งออกภาพ

ตัวอย่างที่สมบูรณ

ตัวอย่างที่สิ้นสุดนี้:

  • สร้างวงจร GS1 ที่ถูกต้องโดยใช้ AIs ที่ทั่วไป: (01) GTIN-14, (17) Expiry YYMMDD***, () Batch/Lot และ (21) Serial
  • รับรองความยาว / รูปแบบ
  • Renders ภาพ 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) { ... }
    }
}

สร้างและทํางาน

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 จะถูกเขียนไว้ในไดเรกทอรีทํางานของคุณ

คู่มือขั้นตอน

ขั้นตอน 1: ทําความเข้าใจ GS1-128 หลักฐาน

GS1-128 เป็นสัญลักษณ์ของ Code 128-based ซึ่งรหัสข้อมูลโดยใช้ Application Identifiers (AI) แต่ละ AI จะกําหนดสิ่งต่อไปนี้ (ประเภทข้อมูลและยาว)

สัญญาณทั่วไป:

  • (01) GTIN-14 ( 14 หมายเลขที่กําหนดเอง; บัตรที่มีเซลล์ด้านซ้ายถ้าสั้นขึ้น)
  • (17) วันที่สิ้นสุด (YYMMDD)
  • (10) Batch/Lot (** ความยาวที่แตกต่างกัน**)
  • (21) ชุด (** ความยาวที่แตกต่างกัน**)

FNC1 การจัดการ: เมื่อคุณผ่านลวดที่สามารถอ่านได้โดยมนุษย์พร้อมพารามิเนส (เช่น (01)1234...(10)LOT), ห้องสมุดจะใส่แยก FNC1 โดยอัตโนมัติตามกฎ GS1 - โดยเฉพาะอย่างยิ่งจําเป็นเมื่อ AI ** ความยาวที่แตกต่างกัน** จะถูกติดตามโดย AI อื่น ๆ

ขั้นตอนที่ 2: การตั้งค่าบาร์โค้ด

ใช้ XDimension (ความหนาของโมดูล) BarHeight และ margins เพื่อให้สมดุลความถี่การพิมพ์และความต้านทานการสแกนเนอร์ สําหรับฉลากความร้อนขนาด XD ขนาดสูงเล็กน้อยและพื้นที่เงียบสงบที่ดีขึ้นอัตราการอ่านครั้งแรก

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)

Pad GTIN to 14 หมายเลข, format dates as YYMMDD and keep variable-length AIs concise (หลีกเลี่ยงพื้นที่ / แถบควบคุม)

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

ขั้นตอนที่ 4: Set บาร์โค้ดข้อความ

คุณสามารถตั้งค่าข้อความ GS1 ในเครื่องสร้างหรือภายหลังผ่านทาง generator.CodeTextตัวอย่างจะวางไว้ในผู้ก่อสร้างและแสดงให้เห็นวิธีการจัดตั้งใหม่หากจําเป็น

ขั้นตอน 5: Customize การปรากฏตัว

ให้คํานวณว่าจะแสดงข้อความโค้ดที่สามารถอ่านได้โดยมนุษย์** ภายใต้แถบ (CodeLocation.Below) หรือลบมันถ้า layout labels ของคุณพิมพ์ข้อความที่อื่น ๆ

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:** หากสแกนเนอร์ต่อสู้เพิ่มขอบด้านซ้าย/ขวาและ XDimension น้อย ๆ
  • **เครื่องพิมพ์อุณหภูมิ:**อายุ/เสื้อผ้าสามารถผสมแถบดี XDimension (เช่น 3 - 4 ชิ้น) และให้สื่อสะอาด
  • GTIN หมายเลขตรวจสอบ: หากคุณสร้าง GTIN คอมพิวเตอร์ / ตรวจสอบ Mod10 เพื่อหลีกเลี่ยงข้อผิดพลาดในสนาม
  • หน้าต่างวันที่: หน้าต่าง YY ในตัวอย่างนั้นเรียบง่ายและสอดคล้องกับกฎธุรกิจของคุณ (เช่นเพียง 20xx)
  • **การควบคุมรุ่น: **จัดเก็บพารามิเตอร์ขนาด / ปริมาณใน config เพื่อให้รหัสบาร์สามารถเล่นได้ทั่วสภาพแวดล้อม

ข้อสรุป

ด้วย Aspose.BarCode สําหรับ .NET การสร้างรหัสบาร์ที่ปฏิบัติตามมาตรฐาน GS1-128** เป็นเรื่องง่าย การตั้งค่า AIs ของคุณ การรับรองรูปแบบการพิมพ์สีและส่งออก PNG ที่สะอาดพร้อมสําหรับการจดหมาย ใช้ตัวอย่างที่สมบูรณ์ข้างต้นเป็นจุดเริ่มต้นที่แข็งแกร่ง จากนั้นปรับขนาด / ปริมาณและกฎการรับประกันเพื่อตอบสนองเครื่องพิมพ์วัสดุและสแกนของคุณ

More in this category