如何在 C# 中使用 OCR 讀取條碼和二維碼

How to Read Barcodes and QR Codes

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

在條碼和 QR 碼作為打印或數字文件的一部分需要自動處理的情況下,使用 OCR 技術來讀取這些碼是非常有用的。 它允許從各種來源自動化和提取數據,為企業和開發人員提供了一個多功能的解決方案。

快速入門:立即從 PDF 讀取條碼

只需一個設置就能啟用條碼檢測,並使用 IronOCR 輕鬆掃描 PDF。 下面的代碼顯示如何開啟條碼讀取,處理 PDF 並在幾行代碼中立即檢索解碼的值。

Nuget IconGet started making PDFs with NuGet now:

  1. Install IronOCR with NuGet Package Manager

    PM > Install-Package IronOcr

  2. Copy and run this code snippet.

    var result = new IronOcr.IronTesseract() { Configuration = new IronOcr.TesseractConfiguration { ReadBarCodes = true } }.Read(new IronOcr.OcrPdfInput("document.pdf"));
    foreach(var bc in result.Barcodes) Console.WriteLine(bc.Value);
  3. Deploy to test on your live environment

    Start using IronOCR in your project today with a free trial
    arrow pointer
class="hsg-featured-snippet">

最小工作流程(5 步)

  1. 下載用於讀取條碼和 QR 碼的 C# 庫。
  2. 導入目標圖像和 PDF 文件。
  3. 通過將 ReadBarCodes 屬性設置為 true 來啟用條碼讀取。
  4. 使用 Read 方法像往常一樣執行 OCR。
  5. 輸出檢測到的文本和條碼值。


讀取條碼範例

構造 IronTesseract 對象來執行讀取操作。 通過將 ReadBarCodes 屬性設置為 true 來啟用條碼讀取。 通過將 PDF 文件傳遞給 OcrPdfInput 構造函數來導入 PDF 文件。 然後,使用 Read 方法對導入的 PDF 文件執行 OCR。

現在,我們來對以下 PDF 文件執行 OCR:

:path=/static-assets/ocr/content-code-examples/how-to/barcodes-read-barcodes.cs
using IronOcr;
using System;

// Instantiate IronTesseract
IronTesseract ocrTesseract = new IronTesseract();

// Enable barcode reading
ocrTesseract.Configuration.ReadBarCodes = true;

// Add PDF
using var imageInput = new OcrPdfInput("pdfWithBarcodes.pdf");

// Perform OCR
OcrResult ocrResult = ocrTesseract.Read(imageInput);

// Output detected barcodes and text values
Console.WriteLine("Extracted text:");
Console.WriteLine(ocrResult.Text);
Console.WriteLine("Extracted barcodes:");
foreach (var barcode in ocrResult.Barcodes)
{
    Console.WriteLine(barcode.Value);
}
Imports IronOcr
Imports System

' Instantiate IronTesseract
Private ocrTesseract As New IronTesseract()

' Enable barcode reading
ocrTesseract.Configuration.ReadBarCodes = True

' Add PDF
Dim imageInput = New OcrPdfInput("pdfWithBarcodes.pdf")

' Perform OCR
Dim ocrResult As OcrResult = ocrTesseract.Read(imageInput)

' Output detected barcodes and text values
Console.WriteLine("Extracted text:")
Console.WriteLine(ocrResult.Text)
Console.WriteLine("Extracted barcodes:")
For Each barcode In ocrResult.Barcodes
	Console.WriteLine(barcode.Value)
Next barcode
$vbLabelText   $csharpLabel
class="content-img-align-center">
class="center-image-wrapper"> Reading result

如您所見,提取文本中還包含多個條碼值,並在條碼下方顯示。

讀取 QR 碼範例

與讀取條碼類似,ReadBarCodes 屬性必須設置為 true。 除了更改文件路徑,代碼中不需要其他更改。 現在,我們來對含有 QR 碼的 PDF 文件執行 OCR:

:path=/static-assets/ocr/content-code-examples/how-to/barcodes-read-qr-codes.cs
using IronOcr;
using System;

// Instantiate IronTesseract
IronTesseract ocrTesseract = new IronTesseract();

// Enable barcode reading
ocrTesseract.Configuration.ReadBarCodes = true;

// Add PDF
using var imageInput = new OcrPdfInput("pdfWithQrCodes.pdf");

// Perform OCR
OcrResult ocrResult = ocrTesseract.Read(imageInput);

// Output detected barcodes and text values
Console.WriteLine("Extracted text:");
Console.WriteLine(ocrResult.Text);
Console.WriteLine("Extracted barcodes:");
foreach (var barcode in ocrResult.Barcodes)
{
    Console.WriteLine(barcode.Value);
}
Imports IronOcr
Imports System

' Instantiate IronTesseract
Private ocrTesseract As New IronTesseract()

' Enable barcode reading
ocrTesseract.Configuration.ReadBarCodes = True

' Add PDF
Dim imageInput = New OcrPdfInput("pdfWithQrCodes.pdf")

' Perform OCR
Dim ocrResult As OcrResult = ocrTesseract.Read(imageInput)

' Output detected barcodes and text values
Console.WriteLine("Extracted text:")
Console.WriteLine(ocrResult.Text)
Console.WriteLine("Extracted barcodes:")
For Each barcode In ocrResult.Barcodes
	Console.WriteLine(barcode.Value)
Next barcode
$vbLabelText   $csharpLabel
class="content-img-align-center">
class="center-image-wrapper"> Reading result

常見問題解答

如何在.NET C#中讀取條碼和二維碼?

在 .NET C# 中,您可以透過從 NuGet 下載 IronOCR 庫、匯入目標影像或 PDF、將ReadBarCodes屬性設為 true 以啟用條碼讀取,然後使用Read方法執行 OCR 來讀取條碼和二維碼。

使用 IronOCR 對帶有條碼的文件進行 OCR 識別的流程是什麼?

若要使用 IronOCR 對帶有條碼的文檔執行 OCR,請建構 IronTesseract 對象,透過將ReadBarCodes屬性設為 true 來啟用條碼讀取,使用OcrPdfInput建構函數匯入文檔,並使用Read方法提取文字和條碼值。

IronOCR 能否讀取同一份文件中的條碼和二維碼?

是的,IronOCR 可以透過啟用ReadBarCodes屬性,從同一文件中讀取條碼和二維碼。該庫會輸出所有偵測到的文字和條碼值。

如何在IronOCR中啟用條碼讀取功能?

在 IronOCR 中,透過將ReadBarCodes屬性設為 true 來啟用條碼讀取功能。此設定允許庫檢測並解碼文件中的條碼和二維碼。

從讀取條碼切換到讀取二維碼時,是否需要更改代碼?

使用 IronOCR 從讀取條碼切換到讀取二維碼時ReadBarCodes無需對程式碼進行任何重大更改。 ReadBarCodes 屬性保持不變;可能只需要更改檔案路徑。

IronOCR 可以處理哪些類型的文件以提取條碼和二維碼?

IronOCR 可以處理各種紙本和電子文檔,提取條碼和二維碼,因此適用於從各種來源自動提取資料。

IronOCR 如何增強資料處理任務的自動化程度?

IronOCR 透過讓開發人員自動從文件中提取和解碼條碼和二維碼,增強了資料處理任務的自動化,從而簡化了工作流程並提高了效率。

A PHP Error was encountered

Severity: Warning

Message: Illegal string offset 'name'

Filename: sections/author_component.php

Line Number: 18

Backtrace:

File: /var/www/ironpdf.com/application/views/main/sections/author_component.php
Line: 18
Function: _error_handler

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 63
Function: view

File: /var/www/ironpdf.com/application/views/products/sections/three_column_docs_page_structure.php
Line: 64
Function: main_view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/views/products/how-to/index.php
Line: 2
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 552
Function: view

File: /var/www/ironpdf.com/application/controllers/Products/Howto.php
Line: 31
Function: render_products_view

File: /var/www/ironpdf.com/index.php
Line: 292
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: Illegal string offset 'title'

Filename: sections/author_component.php

Line Number: 38

Backtrace:

File: /var/www/ironpdf.com/application/views/main/sections/author_component.php
Line: 38
Function: _error_handler

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 63
Function: view

File: /var/www/ironpdf.com/application/views/products/sections/three_column_docs_page_structure.php
Line: 64
Function: main_view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/views/products/how-to/index.php
Line: 2
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 552
Function: view

File: /var/www/ironpdf.com/application/controllers/Products/Howto.php
Line: 31
Function: render_products_view

File: /var/www/ironpdf.com/index.php
Line: 292
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: Illegal string offset 'comment'

Filename: sections/author_component.php

Line Number: 48

Backtrace:

File: /var/www/ironpdf.com/application/views/main/sections/author_component.php
Line: 48
Function: _error_handler

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 63
Function: view

File: /var/www/ironpdf.com/application/views/products/sections/three_column_docs_page_structure.php
Line: 64
Function: main_view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/views/products/how-to/index.php
Line: 2
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 552
Function: view

File: /var/www/ironpdf.com/application/controllers/Products/Howto.php
Line: 31
Function: render_products_view

File: /var/www/ironpdf.com/index.php
Line: 292
Function: require_once

準備好開始了嗎?
Nuget 下載 5,044,537 | 版本: 2025.11 剛剛發布