如何建立一維和直線條碼

This article was translated from English: Does it need improvement?
Translated
View the article in English

IronBarcode 可讓您使用簡單的 C# 程式碼產生所有主要的一維條碼格式,包括 Code 128、GS1-128、Code 39、Code 93、EAN-13、EAN-8、UPC-A、UPC-E、Intelligent Mail、MSI、Codabar 和 DataBar,並具備自動驗證和符合性檢查功能。 您可以探索 支援的 BarCode 格式的完整清單,找到適合您需求的格式。

線性 BarCode 仍然是傳遞資訊的業界標準。 這些工具快速、可靠,幾乎可搭配任何掃描器使用。 由於這些 BarCode 不需要進行複雜的影像處理,因此在零售收銀線和輸送帶等高速環境中表現優異。

主要的挑戰是針對您的特定需求選擇正確的格式。 您可能需要 Code128 的高資料密度用於貨運標籤,EAN8 的精巧尺寸用於小型產品,或 IntelligentMail BarCode 的嚴格格式用於郵政服務。 IronBarcode 可幫助您高效可靠地生成所有這些格式。 該資料庫也支援各種 輸出資料格式,以便與您現有的系統無縫整合。

本指南涵蓋如何使用 IronBarcode 生成最重要的一維格式及其常見用例。

<! -- 介紹的視覺演示 --> <!--說明:螢幕截圖或圖表 -->


快速入門:創建您的第一個一維條碼

使用 IronBarcode 的簡單 API,從字串建立 Code128 條碼並將其儲存為 JPEG 影像。 立即開始 - 提供資料、選擇編碼和大小,並撰寫您的影像檔案。

Nuget Icon立即開始使用 NuGet 建立 PDF 檔案:

  1. 使用 NuGet 套件管理器安裝 IronBarcode

    PM > Install-Package BarCode

  2. 複製並運行這段程式碼。

    IronBarCode.BarcodeWriter.CreateBarcode("HELLO-WORLD-123", BarcodeEncoding.Code128, 250, 100).SaveAsJpeg("MyFirstBarcode.jpg");
  3. 部署到您的生產環境進行測試

    立即開始在您的專案中使用 IronBarcode,免費試用!
    arrow pointer

<! -- 輸出顯示在 IronPdf 中建立 1d 和線性條碼的結果 --> --> <!--說明:顯示程式碼執行輸出或結果的截圖 -->

如何建立一維和直線條碼?

線性一維 BarCode 使用可變寬度的線和空格來水平儲存資料。 二維條碼(如 QR code)使用方格網格來保存二維資訊。 二維編碼可容納更多的資料,例如 URL 或聯絡卡片,而一維 BarCode 則只能使用簡短的字母數字字串。

這種簡易性可讓一維 BarCode 更快速地掃描。 它使這些工具與標準雷射掃描器普遍相容,是高速零售和物流環境的理想選擇。 在處理大量掃描應用程式時,請探索我們的 讀取速度選項,以最佳化效能。

讓我們來探討最受支援的格式,以及如何使用 IronBarcode 建立這些格式。

如何建立 Code-128 BarCode?

Code 128 BarCode 常用於內部物流和運送標籤。 它支援完整的 128 個字元 ASCII 範圍,因此能高效率地將標準數字和英文文字壓縮到小空間內。 這使得 Code 128 成為倉庫管理系統和庫存追蹤的理想選擇,在這些系統中,空間有限,但資料密度卻非常重要。

然而,在沒有額外擴充的情況下,它無法原生支援 Unicode 或非拉丁字符。 它無法處理中文或阿拉伯文等語言的複雜字元,並會誤解資料,導致不正確的輸出。 對於需要 Unicode 支援的應用程式,請參閱我們的 撰寫 Unicode BarCode 指南。

我需要哪些程式碼?

IronBarcode 可以直接生成 Code-128:在呼叫 Create 方法時,將 BarcodeEncoding.Code128 作為第二個參數傳入,然後匯出為影像。 進一步了解 使用各種格式和選項建立條碼影像

:path=/static-assets/barcode/content-code-examples/how-to/create-code-128-barcode.cs
using IronBarCode;

// Specify Code 128 Format
GeneratedBarcode barcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode/", BarcodeEncoding.Code128);

// Add barcode value text below the barcode
barcode.AddBarcodeValueTextBelowBarcode();

// Save it as JPG
barcode.SaveAsJpeg("code128-sample.jpg");
Imports IronBarCode

' Specify Code 128 Format
Dim barcode As GeneratedBarcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com/csharp/barcode/", BarcodeEncoding.Code128)

' Add barcode value text below the barcode
barcode.AddBarcodeValueTextBelowBarcode()

' Save it as JPG
barcode.SaveAsJpeg("code128-sample.jpg")
$vbLabelText   $csharpLabel

輸出是什麼樣子?

Code-128範例輸出

如何建立 GSI-128 BarCode?

GS1-128 是應用在 Code 128 之上的附加資料格式。GS1-128 的主要優勢在於全球一致的資料詮釋,消除誤解和歧義。 這種標準化對於產品在不同公司和國家之間移動的供應鏈管理至關重要。

然而,由於格式要求嚴格,若遺漏隱藏控制字元或欄位長度不正確,都會使 BarCode 不符合規範。 如果您遇到 GS1-128 格式化的問題,我們的 GS1-128 疑難排解指南 可協助您解決常見問題。

GSI-128 要求什麼格式?

BarCode 掃描器使用應用程式識別碼 (Application Identifiers, AI) - 識別產品代碼中資訊的數字前綴。 瞭解這些識別碼對於建立可在全球供應鏈中運作的合規 BarCode 至關重要。

GS1-128 遵循此四部分資料結構:

(數字前綴 01)GTIN(全球貿易項目號碼):唯一的產品 ID,可識別該產品是什麼。 必須是準確的 14 位數字。 如果您的產品代碼較短,請填充前導零。 例如,123456789012 變成 00123456789012。

(數字前綴 10)Batch/Lot:產品的批次或批號。 1 到 20 個字母數字字符。 當放置在 BarCode 中間時,需要 FNC1 分隔符,以便掃描器知道其結束位置。 如果在 BarCode 端,則不需要分隔符。

(數字前綴 17)到期日:產品的到期日。 按照 YYMMDD 格式固定為六位數。 例如,251231 就是 2025 年 12 月 31 日。使用 2 位數的年份與世紀滑動視窗。

(數字前綴 21)序號:識別獨特個別單位的產品序號。 長度可變,包含 1-20 個字母數字字符。

請注意Only the GTIN is required; 批次、到期日和序列屬性為選項。

警告雖然只需要 GTIN,但有些零售商有特定的合規指引。 在建立 BarCode 時檢查它們的要求。

如何使用 IronBarcode 生成 GSI-128?

生成 GS1-128 BarCode 非常簡單; 在呼叫 Create 時,傳入 BarcodeEncoding.Code128GS1 作為第二個參數。

以下範例顯示符合 GS1-128 標準的完整值,包含所有四種屬性,並將其匯出為影像。

請注意每個數字前綴都必須加上圓括號,否則該部分將不會被識別為有效的屬性。

[{i:(IronBarcode 在建立 GSI-128 時會自動插入 FNC1 分隔符。

:path=/static-assets/barcode/content-code-examples/how-to/create-gsi-128-barcode.cs
using IronBarCode;

// Valid GS1-128 String: GTIN + Expiry + Batch + Serial Number
string gs1Value = "(01)01234567890128(17)251231(10)BATCH001(21)111111";

// Create the barcode
GeneratedBarcode barcode = BarcodeWriter.CreateBarcode(gs1Value, BarcodeEncoding.Code128GS1);

// Display the value below the barcode
barcode.AddBarcodeValueTextBelowBarcode();

// Save as a JPG file
barcode.SaveAsJpeg("gsi128-sample.jpg");
Imports IronBarCode

' Valid GS1-128 String: GTIN + Expiry + Batch + Serial Number
Dim gs1Value As String = "(01)01234567890128(17)251231(10)BATCH001(21)111111"

' Create the barcode
Dim barcode As GeneratedBarcode = BarcodeWriter.CreateBarcode(gs1Value, BarcodeEncoding.Code128GS1)

' Display the value below the barcode
barcode.AddBarcodeValueTextBelowBarcode()

' Save as a JPG file
barcode.SaveAsJpeg("gsi128-sample.jpg")
$vbLabelText   $csharpLabel

GSI-128 的輸出是什麼樣子?

GS1-128 範例輸出

如何修正 GSI-128 格式錯誤?

如果您的字串不符合 GS1-128 標準,IronBarcode 會產生異常。 使用上表驗證您的字串值,並據此修改。

GS1-128 例外

如何建立 Code 39 BarCode?

Code 39 是最早的字母數字條碼標準之一,以可靠性著稱。 常見於汽車和國防領域。 它能夠對字母和數字進行編碼,而無需校驗位元,這使得它在通過其他方式驗證資料完整性的環境中非常簡單。

然而,Code 39 的字元集有限。 它只允許大寫的英文字元、數字和符號,如 /、.、- 等。標準模式不支援小寫。 如需閱讀標準與擴充 Code 39 條碼的相關資訊,請參閱我們的 Code 39 閱讀指南

Code 39 需要哪些程式碼?

若要產生 Code39,請在使用 Create 時將 BarcodeEncoding.Code39 傳送為第二個參數。

以下是一個範例:

:path=/static-assets/barcode/content-code-examples/how-to/create-code-39-barcode.cs
using IronBarCode;

// Specify Code39 Format
GeneratedBarcode barcode = BarcodeWriter.CreateBarcode("IRON-1234", BarcodeEncoding.Code39);

// Stamp barcode value below the barcode
barcode.AddBarcodeValueTextBelowBarcode();

// Save as a JPG file
barcode.SaveAsJpeg("code39.jpg");
Imports IronBarCode

' Specify Code39 Format
Dim barcode As GeneratedBarcode = BarcodeWriter.CreateBarcode("IRON-1234", BarcodeEncoding.Code39)

' Stamp barcode value below the barcode
barcode.AddBarcodeValueTextBelowBarcode()

' Save as a JPG file
barcode.SaveAsJpeg("code39.jpg")
$vbLabelText   $csharpLabel

Code 39 輸出是什麼樣子?

Code39 範例輸出

為什麼我會收到 Code 39 錯誤?

Code39 例外

如果字串不符合 Code39 標準,IronBarcode 會拋出此異常。

[{w:(IronBarcode目前不支援產生延伸的Code39條碼; 僅支援閱讀。 有關閱讀擴展和標準 Code39,請參閱我們的 Code 39 閱讀教學