בסביבת העסק המהירה של היום, טכנולוגיית קוד סרגל משחקת תפקיד קריטי בהקלה על הפעולות ובהתקדמות ניהול הנתונים.בין סטנדרטים שונים של קוד סיסמה, GS1-128 נמצא בשימוש נרחב בכל התעשיות בשל מגוון שלה ויכולת הקוד של מידע מוצר מפורט.המדריך הזה מלמד אותך דרך ליצירת קודים שורות GS1-128, עם Aspose.BarCode עבור .NET – מתוך הבנה של יישומי זיהוי (AIs) כדי להתאים אישית את המראה ולייצוא תמונות.

דוגמה מלאה

דוגמה סופית לסוף:

  • יצירת שורת GS1 בתוקף באמצעות AIs נפוצים: (01) GTIN-14, (17) Expiry YYMMDD**, (10) 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) { ... }
    }
}
  • לבנות ולרוץ *
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 הוא סימבוולוגיה מבוססת ** קוד 128** שמקודדת נתונים באמצעות ** מזהים יישומים (AI)**.

דוגמאות נפוצות:

  • (01) GTIN-14 (קבוע 14 דיגיטליות; חותם עם אפס בצד שמאל אם קצר יותר)
  • (17) תאריך התפוגה (** YYMMDD**)
  • (10) Batch/Lot (** אורך משתנה**)
  • (21) סדרה (** אורך משתנה**)

FNC1 ניהול: כאשר אתה עובר רצועה קריאה אנושית עם דוגמאות (לדוגמה, (01)1234...(10)LOTהספרייה מוסיפה באופן אוטומטי את הפריקטורים FNC1 לפי הכללים של GS1 – במיוחד כאשר AI אורך משתנה עוקב אחר AI אחר.

שלב 2: הגדרת הגדרות סרגל

השתמש XDimension (עומק המודול), BarHeight, ו margins כדי לאזן את צפיפות הדפסה ואת סובלנות הסורק.

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: הגדרת זיהוי יישומים (AIs)

להדביק את ה-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

  • משמעות של אזורי קוויט: אם סורק נלחם, להגדיל את השמאל / הימין מגרשים ו XDממד מעט.
  • הדפסה תרמית: הגיל / הבגדים יכולים להדביק שורות יפות. XDimension (לדוגמה, 3-4 px) ולשמור על התקשורת נקיה.
  • ג’טין צ’ק דיגיטלי: אם אתה בונה GTINs, לחשב / לבדוק את מוד 10 צ ‘ק כדי למנוע שגיאות שדה.
  • חלון תאריך: חלון YY בדוגמה הוא פשוט; תואם את הכללים העסקיים שלך (לדוגמא, רק 20xx).
  • ** בקרת גרסה:** שמור את הפרמטרים של גודל/מגוון ב-config כך שקוד הבר ניתן לשחזר ברחבי הסביבה.

מסקנה

עם Aspose.BarCode עבור .NET, יצירת סטנדרטים תואמים GS1-128 קודים בר הוא פשוט. הגדר את ה- AIs שלך, להעריך פורמטים, פרמטרים טון הדפסה, ולייצוא PNG נקי מוכן לזיהוי.

More in this category