今日の急速なビジネス環境では、バーコードテクノロジーは操作を簡素化し、データ管理を強化する上で重要な役割を果たしています。 さまざまなバーコーディング基準の間で、GS1-128**は多様性と詳細な製品情報を暗号化できる能力のために業界全体で広く使用されています。 このガイドは、アプリケーションアイデンティティファー(AI)を理解して外観をカスタマイズして画像をエクスポートするために、GS1-128のバーコースを生成します。
完全例
結末の例:
- 共通のAIを使用して有効なGS1ストレッチを構築する: (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) { ... }
}
}
■「Build & 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の出力はあなたのワークディレクトリに書かれます。
ステップ・ステップ・ガイド
ステップ1:GS1-128の基本を理解する
GS1-128 は コード 128 ベースの シンボロジーで、データを暗号化する ** アプリケーション アイデンティファー (AI) を使用します。
共通のアイ:
- (01) GTIN-14 (固定 14 桁; 左にゼロでパッド より短い場合)
- (17) 終了日(YYMMDD)
- (10) バッチ/ロット(変長)
- (21) シリアル(変長)
FNC1処理: パレンテージを含む人間読みやすい線を通過する場合(例えば、 (01)1234...(10)LOT
)図書館は、GS1ルールに従って自動的にFNC1分離器を入力します - 特に **変動長さの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:アプリケーションID(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");
実用的なヒント&ゴッチャー
- Quiet zones matter: スキャナーが衝突する場合は、左/右のマージンとXDサイズを少し増やす。
- テルマプリンター: 年齢/服装は薄いバーを溶かすことができます。
XDimension
(たとえば、3〜4px) メディアを清潔に保つ。 - GTINチェック番号: GTINを構築している場合は、フィールドエラーを防ぐために、Mod10チェック数字を計算/確認します。
- 日付ウィンドウ: 例の YY フロントはシンプルで、あなたのビジネスルール(例えば、20xx だけ)に合致します。
- バージョンコントロール: サイズ/マージンパラメーターを構成して、バーコードが周囲で再生可能になります。
結論
Aspose.BarCode for .NET を使用して、標準に準拠する GS1-128* バーコードを作成することは簡単です. AIs を定義し、フォーマットを有効化、タンプリントパラメーター、およびラベル化の準備ができている純粋な PNG エクスポートします. 上記の完全な例を堅固な出発点として使用すると、サイズ/マージンと認証ルールを調整して印刷機、材料、スキャナーに合致します。