快速且簡單地在 C# 中讀取 Code 39 條碼

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

IronBarcode 簡化了在 C# 中使用 BarcodeReaderOptions 類別以及指定 BarcodeEncoding.Code39 並啟用 UseCode39ExtendedMode 以在需要時支持全 ASCII 字元的讀取標準和擴展的 Code 39 條碼。

Code 39 是一種用途廣泛的條碼格式,廣泛應用於庫存、物流和工業應用中。 Code 39 條碼的長度可變,這使其能夠靈活應用於不同用例。

原始標準 Code 39 編碼大寫字母(A-Z)、數字(0-9)和幾個特殊字元(空格、-、$、+、% 和 .)。 這對於基本的 ID 非常有效,但現代應用程式通常需要編碼所有 128 個 ASCII 字元。 Code 39 擴展規範滿足了這一需求。

本指南演示如何使用 IronBarcode 讀取標準和擴展的 Code 39 變體。 無論您是在構建庫存管理系統、跟蹤運輸還是處理工業條碼,IronBarcode 都能提供可靠的 Code 39 讀取功能。 如需完整的條碼讀取功能概覽,請查看我們的條碼快速入門指南


快速入門:在 C# 中讀取 Code 39 條碼

using IronBarcode 的 BarcodeReader 在一行程式碼中從圖像中解碼 Code 39 條碼。 立即開始——指定編碼型別,傳遞圖像並閱讀結果。

  1. 使用NuGet套件管理器安裝https://www.nuget.org/packages/BarCode

    PM > Install-Package BarCode
  2. 複製並運行這段程式碼片段。

    IronBarCode.BarcodeReader.Read("code39.png", new IronBarCode.BarcodeReaderOptions { ExpectBarcodeTypes = IronBarCode.BarcodeEncoding.Code39 }).First().ToString();
  3. 部署以在您的實時環境中測試

    今天就開始在您的專案中使用IronBarcode,透過免費試用

    arrow pointer

如何讀取標準 Code 39 條碼?

using IronBarcode 讀取 Code 39 條碼非常簡單。 首先,初始化一個新的 BarcodeReaderOptions 並將條碼型別指定為 BarcodeEncoding.Code39。 這通過告訴讀取器要尋找的確切條碼格式來優化讀取。

接下來,使用 Read 方法讀取條碼,將條碼圖像和選項作為參數傳入。 然後遍歷結果集合並將每個條碼的字串值列印到控制台。 如需更高級的配置,請查看我們有關條碼讀取器設置的詳細指南。

標準 Code 39 條碼是什麼樣的?

此圖像包含一個標準的 Code 39 條碼。 注意條碼是如何以條紋和其下方的可讀文字形式顯示其編碼值的。 這種雙重表示是 Code 39 條碼在工業和物流應用中的典型特徵。

Code 39 條碼編碼 'ABC-1234',顯示可見條紋和其下方的可讀文字

我需要什麼程式碼來讀取標準 Code 39?

:path=/static-assets/barcode/content-code-examples/how-to/read-code39-barcode.cs
using IronBarCode;
using System;

BarcodeReaderOptions options = new BarcodeReaderOptions()
{
    // Tell the reader to only look for Code 39.
    ExpectBarcodeTypes = BarcodeEncoding.Code39
};

// Read barcode(s) from the image file using the specified options
var results = BarcodeReader.Read("code39.png", options);

// Loop through each BarcodeResult found in the image
foreach (var result in results)
{
    // Print the decoded string value of the standard Code 39 barcode
    Console.WriteLine(result.ToString());
}
Imports IronBarCode
Imports System

Dim options As New BarcodeReaderOptions() With {
    .ExpectBarcodeTypes = BarcodeEncoding.Code39
}

' Read barcode(s) from the image file using the specified options
Dim results = BarcodeReader.Read("code39.png", options)

' Loop through each BarcodeResult found in the image
For Each result In results
    ' Print the decoded string value of the standard Code 39 barcode
    Console.WriteLine(result.ToString())
Next
$vbLabelText   $csharpLabel

指定預期的條碼型別顯著提高了讀取性能。 IronBarcode 沒有浪費時間尋找其他條碼格式,這對於大圖像集的批量處理特別有利。 了解更多有關優化條碼讀取性能的資訊,請參閱我們的讀取速度選項指南

我應該期望什麼輸出?

Visual Studio 控制台顯示 Code 39 條碼讀取輸出,解碼值為 ABC-1234,退出程式碼為 0

控制台輸出顯示成功解碼的值 "ABC-1234" 來自我們的 Code 39 條碼。 退出程式碼 0 確認無錯誤地成功執行。 在生產應用中,針對可能無法識別的條碼實施適當的錯誤處理。 如果您遇到問題,請查看我們的條碼未識別故障排除指南


如何讀取擴展 Code 39 條碼?

讀取擴展 Code 39 條碼的過程與標準 Code 39 類似。關鍵區別在於將 UseCode39ExtendedMode 屬性設置為 true。

此設置指導 IronBarcode 解析特殊字元對(如 +T、%O)並將其解碼為完整的 ASCII 等價字元(如 t、!)。 擴展 Code 39 使用兩字元序列來表示超出標準集的字元。 這使得條碼稍長,但能夠編碼小寫字母、額外的標點符號和控制字元。

我應該什麼時候使用擴展 Code 39?

當您的應用程式需要編碼以下內容時,擴展 Code 39 是理想的選擇:

  • 混合大小寫文字(大寫和小寫字母)
  • 特殊字元,如 @、#、&、!、?
  • 用於資料傳輸的控制字元
  • 支持完整的 ASCII 字元集

常見應用包括醫療系統、文件跟蹤和需要豐富資料編碼的高級庫存管理。

擴展的 Code 39 條碼是什麼樣的?

此圖像包含一個擴展的 Code 39 條碼。 值 Test-Data! 包含小寫字母和驚嘆號,這些字元僅在完整的 ASCII 集中可用,並需要擴展模式。

擴展 Code 39 條碼顯示 'Test-Data!' 被編碼的資料,作為其下方條紋顯示的解碼輸出

擴展 Code 39 需要什麼程式碼?

:path=/static-assets/barcode/content-code-examples/how-to/read-extended-code39-barcode.cs
using IronBarCode;
using System;

BarcodeReaderOptions options = new BarcodeReaderOptions()
{
    // Enable extended Code 39 mode
    UseCode39ExtendedMode = true,

    // Specify that we are expecting Code 39 barcodes
    ExpectBarcodeTypes = BarcodeEncoding.Code39
};

// Read barcode(s) from the extended code 39 image
var results = BarcodeReader.Read("code39extended.png", options);

// Loop through each BarcodeResult found in the image
foreach (var result in results)
{
    // Print the fully decoded ASCII string (e.g., "Test-Data!")
    Console.WriteLine(result.ToString());
}
Imports IronBarCode
Imports System

Dim options As New BarcodeReaderOptions() With {
    .UseCode39ExtendedMode = True,
    .ExpectBarcodeTypes = BarcodeEncoding.Code39
}

' Read barcode(s) from the extended code 39 image
Dim results = BarcodeReader.Read("code39extended.png", options)

' Loop through each BarcodeResult found in the image
For Each result In results
    ' Print the fully decoded ASCII string (e.g., "Test-Data!")
    Console.WriteLine(result.ToString())
Next
$vbLabelText   $csharpLabel

擴展模式會有什麼樣的輸出?

控制台輸出顯示 'Test-Data!' 結果來自擴展 Code 39 條碼讀取應用程式

提示控制台輸出可能不正確顯示所有 ASCII 字元。
在這些情況下,請將輸出導入到 .txt 文件以驗證提取結果。 )}]

高級 Code 39 讀取技術

處理多個條碼

IronBarcode 自動檢測並讀取單個圖像中的多個 Code 39 條碼。 Read 方法返回的結果集合允許您單獨處理每個條碼。 對於處理條碼表或複雜文件的應用程式,請參閱我們有關讀取多個條碼的指南。

處理劣質圖像

Code 39 條碼有時會以不理想的狀況出現——褪色的印刷品、傾斜的角度或低解析度掃描。 IronBarcode 包括強大的圖像校正濾鏡,可以顯著提高讀取準確度:

:path=/static-assets/barcode/content-code-examples/how-to/read-code39-barcodes-4.cs
BarcodeReaderOptions options = new BarcodeReaderOptions()
{
    ExpectBarcodeTypes = BarcodeEncoding.Code39,
    UseCode39ExtendedMode = true,
    
    // Apply image correction filters
    ImageFilters = new ImageFilterCollection() {
        new SharpenFilter(),
        new ContrastFilter(),
        new BrightnessFilter()
    }
};
Imports System

Dim options As New BarcodeReaderOptions() With {
    .ExpectBarcodeTypes = BarcodeEncoding.Code39,
    .UseCode39ExtendedMode = True,
    
    ' Apply image correction filters
    .ImageFilters = New ImageFilterCollection() From {
        New SharpenFilter(),
        New ContrastFilter(),
        New BrightnessFilter()
    }
}
$vbLabelText   $csharpLabel

性能優化

對於高量條碼讀取應用,請考慮以下優化策略:

  1. 指定準確的條碼型別 - 始終設置 ExpectBarcodeTypes 以避免不必要的掃描
  2. 使用合適的讀取速度 - 根據您的需求平衡速度和準確度
  3. 並行處理圖像 - 利用多執行緒進行批量處理
  4. 預處理圖像 - 僅在必要時應用修正以維持性能

總結

IronBarcode 簡化了 C# 中的 Code 39 條碼讀取,無論是標準格式還是擴展格式。 關鍵步驟是:

掌握這些基本知識後,您就可以將 Code 39 條碼讀取整合到您的 .NET 應用程式中。 有關完整的 API 文件和其他條碼格式,請存取我們的全面的 API 參考資料。 如需 Code 39 的實際操作範例,請查看我們的專用 Code 39 教程

常見問題

Code 39是什麼以及它的常見用途是什麼?

Code 39是一種用途廣泛的條碼格式,廣泛應用於庫存、物流和工業應用。它的長度可變,適合不同的使用情境。標準Code 39編碼了大寫字母(A-Z)、數字(0-9)和幾個特殊字元,而Code 39 Extended可以編碼所有128個ASCII字元。IronBarcode提供了可靠的功能來讀取標準和擴展的Code 39變體。

如何在C#中讀取標準的Code 39條碼?

要使用IronBarcode讀取Code 39條碼,首先初始化一個新的BarcodeReaderOptions,並將條碼型別指定為BarcodeEncoding.Code39。然後使用Read方法,將條碼圖像和選項作為參數傳遞。最後,迭代結果集合以存取每個條碼的字串值。

標準Code 39可以編碼哪些字元?

標準Code 39可編碼大寫字母(A-Z)、數字(0-9)和若干特殊字元,包括空格、連字號(-)、美元符號($)、加號(+)、百分比(%)和句號(.)。若要編碼所有128個ASCII字元,您需要使用Code 39 Extended模式,IronBarcode通過UseCode39ExtendedMode選項支持。

標準和擴展的Code 39有什麼區別?

標準Code 39限於大寫字母、數字及少數特殊字元,非常適合基礎ID。Code 39 Extended解決了現代應用編碼所有128個ASCII字元的需求。IronBarcode透過啟用BarcodeReaderOptions類中的UseCode39ExtendedMode簡化了這兩種變體的讀取,實現完整的ASCII字元支持。

Code 39條碼可以包括人眼可讀文字嗎?

是的,Code 39條碼通常以條和條下方的人眼可讀文字來顯示其編碼值。在工業和物流應用中,這種雙重顯示很常見,讓操作員更容易驗證條碼內容。IronBarcode可以讀取條碼資料,無論人眼可讀文字是否存在。

使用IronBarcode進行條碼操作有什麼好處?

IronBarcode提供了如易於整合、支持多種條碼格式、高品質圖像生成和強大讀取能力等好處,使其成為C#中條碼操作的全面工具。

IronBarcode是否提供自定義條碼外觀的支持?

是的,IronBarcode提供了廣泛的條碼外觀自定義選項,包括顏色、大小和文字註釋,讓您可以根據具體設計需求定制條碼。

IronBarcode如何幫助改善業務流程效率?

IronBarcode通過使條碼生成和讀取快速且準確來提高業務流程效率,減少手動資料輸入錯誤,並改善庫存和資產追蹤。

將IronBarcode實現於專案中需要什麼程式設計技能?

基本的C#程式設計知識足以將IronBarcode實現於專案中,因為它提供了簡單的方法和全面的文件來指導開發者。

IronBarcode適合於小型專案和大型企業應用嗎?

IronBarcode設計為可擴展且多功能,使其適合小型專案和需要強大條碼解決方案的大型企業應用。

Curtis Chau
技術作家

Curtis Chau擁有Carleton大學的電腦科學學士學位,專精於前端開發,擁有Node.js、TypeScript、JavaScript和React的專業知識。Curtis熱衷於建立直觀且美觀的使用者介面,喜愛使用現代框架並建立結構良好、視覺吸引力的手冊。

除了開發,Curtis對物聯網(IoT)有濃厚的興趣,探索創新的方法來整合硬體和軟體。在空閒時間,他喜歡玩遊戲和建立Discord機器人,結合他對技術的熱愛與創造力。

準備好開始了嗎?
Nuget 下載 2,331,688 | 版本: 2026.7 剛剛發布
Still Scrolling Icon

還在滾動嗎?

想快速驗證嗎? PM > Install-Package BarCode
運行範例觀看您的字串成為條碼。