如何在C#中輸出條碼數據格式

如何使用 IronBarcode 在 C# 中輸出資料格式。

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

IronBarcode 提供多種條碼讀取的輸出格式,包括 BarcodeImage, BarcodeType, BinaryValue, 座標、尺寸、頁碼、方向、文字和值屬性。 這些格式可針對各種使用情況,以程式化的方式處理 BarCode 資料。

IronBarcode 不只是簡單地讀取條碼並在控制台中列印數值,而是提供更多的功能。 它提供多種輸出格式,為使用者處理讀取結果鋪路。 這些格式包含條碼圖像、條碼類型、 BinaryValue 、座標、高度、寬度、頁碼、條碼、頁面方向、文字和值等屬性。

使用者可以在程式中進一步操作這些屬性。 讓我們來探討一下如何使用這些屬性,以及在哪些用例中這些屬性會有所幫助。

快速入門:讀取條碼值並在一行中輸入

本範例展示如何使用 IronBarcode 從影像讀取條碼 - 一行載入,然後馬上列印條碼的值和類型。 非常適合快速上手。如需更全面的範例,請參閱 BarCode 快速入門指南

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

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

    PM > Install-Package BarCode

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

    var result = IronBarCode.BarcodeReader.Read("input.png");
    Console.WriteLine($"Value: {result[0].Value}, Type: {result[0].BarcodeType}");
  3. 部署到您的生產環境進行測試

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

有哪些可用的輸出格式及其使用案例?

<! -- 輸出顯示 IronPDF 中的輸出格式和用例結果 --> -->。 <!--說明:顯示程式碼執行輸出或結果的截圖 -->

BarcodeResult儲存了各種有用的屬性。 這些房產清單如下:

BarcodeImage BarcodeType -二BinaryValue

  • 座標、高度和寬度 PageNumber BarcodePageOrientation
  • 文本與價值

每個屬性在 BarCode 處理工作流程中都有特定的用途。 無論是建立庫存管理系統、文件處理管道或品質控制應用程式,這些資料格式都能提供讀取各種來源的 BarCode 所需的靈活性。

如何擷取並儲存 BarCode 影像?

一旦 IronBarcode 讀取影像,在影像中找到的條碼會儲存在 BarcodeResult 中,作為類型為 AnyBitmapBarcodeImage 屬性。 BarcodeImage屬性用於儲存找到的條碼影像。 使用者可擷取此物件以進一步處理影像,或將其儲存為永久副本。 這樣可以省去從影像中擷取 BarCode 影像的額外程式碼,提供效率與易用性。

請看下面的程式碼片段,它展示了此輸出格式的可能用例:

:path=/static-assets/barcode/content-code-examples/how-to/output-data-formats-BarcodeImage.cs
using IronBarCode;
using IronSoftware.Drawing;
using System.Collections.Generic;

// Read barcode from PDF file
BarcodeResults result = BarcodeReader.ReadPdf("test.pdf");

// Create list for barcodes
List<AnyBitmap> barcodeList = new List<AnyBitmap>();

foreach (BarcodeResult barcode in result)
{
    barcodeList.Add(barcode.BarcodeImage);
}

// Create multi-page TIFF
AnyBitmap.CreateMultiFrameTiff(barcodeList).SaveAs("barcodeImages.tif");
Imports IronBarCode
Imports IronSoftware.Drawing
Imports System.Collections.Generic

' Read barcode from PDF file
Private result As BarcodeResults = BarcodeReader.ReadPdf("test.pdf")

' Create list for barcodes
Private barcodeList As New List(Of AnyBitmap)()

For Each barcode As BarcodeResult In result
	barcodeList.Add(barcode.BarcodeImage)
Next barcode

' Create multi-page TIFF
AnyBitmap.CreateMultiFrameTiff(barcodeList).SaveAs("barcodeImages.tif")
$vbLabelText   $csharpLabel

上面的程式碼片段說明了此輸出格式的一個使用案例。 具體而言,它會從 PDF 文件中偵測到的 BarCode 建立多頁 TIFF 影像。 首先,我們掃描或偵測樣本 PDF 中的條碼。 然後,我們建立一個AnyBitmap列表,並將BarcodeImage屬性中的資訊儲存在其中。 最後,我們使用此清單以 CreateMultiFrameTiff 方法產生多頁 TIFF。 此技術在處理 多頁 GIF 和 TIFF 檔案時特別有用。

請注意 BarcodeResult中的BarcodeImage屬性僅儲存讀取過程中找到的條碼影像,而不是整個輸入影像本身。

如何以程式方式識別不同的 BarCode 類型?

此屬性有助於判斷輸入影像或文件中存在何種類型的 BarCode。 但是,限制條件是圖像內的條碼類型必須是 IronBarcode 所支援和可讀取的。 要瞭解更多關於 IronBarcode 所支援的條碼類型,請參考此文章。 此外,請探索支援的條碼格式的完整清單,以確保與您的特定需求相容。

下面的程式碼片段示範如何透過列印值到控制台來擷取影像中的 BarCode 值和 BarCode 類型。

:path=/static-assets/barcode/content-code-examples/how-to/output-data-formats-BarcodeType.cs
using IronBarCode;
using System;

// Read barcode from PNG
BarcodeResults result = BarcodeReader.Read("bc3.png");

// Output barcode type to console
foreach (BarcodeResult barcode in result)
{
    Console.WriteLine("The barcode value is " + barcode.ToString() + " and the barcode type is " + barcode.BarcodeType);
}
Imports IronBarCode
Imports System

' Read barcode from PNG
Private result As BarcodeResults = BarcodeReader.Read("bc3.png")

' Output barcode type to console
For Each barcode As BarcodeResult In result
	Console.WriteLine("The barcode value is " & barcode.ToString() & " and the barcode type is " & barcode.BarcodeType)
Next barcode
$vbLabelText   $csharpLabel

從上面的程式碼片段可以看出,我們透過對輸入圖像呼叫BarcodeReader.Read()方法來執行條碼讀取。 這將傳回一個BarcodeResults對象,該物件儲存從圖像中讀取所有可用條碼所得到的所有BarcodeResult 。 接下來,我們遍歷BarcodeResults物件以檢索BarcodeResult ,並將條碼值和類型列印到控制台。 此方法可與各種條碼類型無縫配合,包括 Code 39 條碼等專門格式。

何時應該使用二進值輸出?

使用 IronBarcode,使用者可從 BarcodeResult 物件存取 BinaryValue 屬性,以擷取條碼值的位元組陣列。 這樣使用者就可以在程式內部進一步操作條碼值。 二進值輸出在處理加密資料、以 BarCode 編碼的檔案附件,或與需要位元組層級資料處理的系統整合時特別有用。

以下程式碼片段示範了以二進位資料形式檢索條碼值的一種用例:

:path=/static-assets/barcode/content-code-examples/how-to/output-data-formats-BinaryValue.cs
using IronBarCode;

// Read barcode from PNG
BarcodeResults result = BarcodeReader.Read("multiple-barcodes.png");

int i = 1;
foreach (BarcodeResult barcode in result)
{
    var binaryValue = barcode.BinaryValue;
    var barcodeType = IronBarCode.BarcodeEncoding.QRCode;

    // Create QR code
    GeneratedBarcode generatedBarcode = BarcodeWriter.CreateBarcode(binaryValue, barcodeType);

    // Export QR code
    generatedBarcode.SaveAsPng($"qrFromBinary{i}.png");
    i++;
}
Imports IronBarCode

' Read barcode from PNG
Private result As BarcodeResults = BarcodeReader.Read("multiple-barcodes.png")

Private i As Integer = 1
For Each barcode As BarcodeResult In result
	Dim binaryValue = barcode.BinaryValue
	Dim barcodeType = IronBarCode.BarcodeEncoding.QRCode

	' Create QR code
	Dim generatedBarcode As GeneratedBarcode = BarcodeWriter.CreateBarcode(binaryValue, barcodeType)

	' Export QR code
	generatedBarcode.SaveAsPng($"qrFromBinary{i}.png")
	i += 1
Next barcode
$vbLabelText   $csharpLabel

透過觀察上面的程式碼片段,我們創建了一個簡單的程序,可以將圖像中的多個條碼轉換為單獨的新的二進位編碼檔案。 首先,我們掃描範例 PNG 影像中的條碼。 偵測到這些 BarCode 後,我們會遍歷它們,存取 BinaryValue 屬性,並使用它建立新的二進位檔案。 當您需要 讀取多個 BarCode 並單獨處理其二進位資料時,此技術尤其有價值。

如何存取 BarCode 位置和尺寸?

使用者可以存取BarcodeResult物件的另一個屬性是條碼的座標,包括X1Y1X2Y2 ,以及其在圖像檔案或文件中的HeightWidth 。 當使用者需要擷取有關 BarCode 位置和尺寸的資訊時,這些屬性將非常有用。 此空間資訊對於需要精確定位的應用程式至關重要,例如自動化文件處理、品質控制系統,或在執行 rop regions 以優化條碼掃描時。

讓我們來展示 BarCode 的位置和尺寸。

:path=/static-assets/barcode/content-code-examples/how-to/output-data-formats-height-width.cs
using IronBarCode;
using IronSoftware.Drawing;
using System.Linq;

// Read barcode from PNG
BarcodeResults result = BarcodeReader.Read("multiple-barcodes.png");

AnyBitmap bitmap = AnyBitmap.FromFile("multiple-barcodes.png");

foreach (BarcodeResult barcode in result)
{
    PointF[] barcodePoints = barcode.Points;

    float x1 = barcodePoints.Select(b => b.X).Min();
    float y1 = barcodePoints.Select(b => b.Y).Min();

    Rectangle rectangle = new Rectangle((int)x1, (int)y1, (int)barcode.Width!, (int)barcode.Height!);

    bitmap = bitmap.Redact(rectangle, Color.Magenta);

    // Save the image
    bitmap.SaveAs("redacted.png", AnyBitmap.ImageFormat.Png);
}
Imports System
Imports IronBarCode
Imports IronSoftware.Drawing
Imports System.Linq

' Read barcode from PNG
Private result As BarcodeResults = BarcodeReader.Read("multiple-barcodes.png")

Private bitmap As AnyBitmap = AnyBitmap.FromFile("multiple-barcodes.png")

For Each barcode As BarcodeResult In result
	Dim barcodePoints() As PointF = barcode.Points

	Dim x1 As Single = barcodePoints.Select(Function(b) b.X).Min()
	Dim y1 As Single = barcodePoints.Select(Function(b) b.Y).Min()

'INSTANT VB TODO TASK: There is no VB equivalent to the C# 'null-forgiving operator':
'ORIGINAL LINE: Rectangle rectangle = new Rectangle((int)x1, (int)y1, (int)barcode.Width!, (int)barcode.Height!);
	Dim rectangle As New Rectangle(CInt(Math.Truncate(x1)), CInt(Math.Truncate(y1)), CInt(barcode.Width), CInt(barcode.Height))

	bitmap = bitmap.Redact(rectangle, Color.Magenta)

	' Save the image
	bitmap.SaveAs("redacted.png", AnyBitmap.ImageFormat.Png)
Next barcode
$vbLabelText   $csharpLabel
Three barcode samples (A, B, C) showing different encoded data with similar visual patterns
Three redacted content blocks with illegible text fragments

上面的程式碼片段會刪除影像檔案中的多個 BarCode。為了達成這個目標,我們結合使用了 IronBarcode 和 IronDrawing 這兩個函式庫。 為了取得BarcodeResult物件並從中提取屬性,我們首先使用BarcodeReader.Read()方法讀取影像檔案中可用的條碼。 同時,需要將輸入的影像檔案轉換為 AnyBitmap 物件,以便對影像套用編輯方法。 一旦我們有了BarcodeResults對象,我們就可以應用一個循環並遍歷它,以獲取圖像中每個可用條碼的X1Y1WidthHeight ,並將它們用於AnyBitmap.Redact()方法的CropRectangle屬性中。

為什麼頁碼對多頁文件很重要?

使用者可檢索找到 BarCode 的頁碼。 對於使用包含多個 BarCode 的多頁文件,並需要知道在文件中找到的 BarCode 位置以便進一步處理的使用者而言,這是一項很有幫助的功能。 當 從 PDF 文件讀取 BarCode 或在企業應用程式中處理批次文件時,此功能是不可或缺的。

請看下面的程式碼片段:

:path=/static-assets/barcode/content-code-examples/how-to/output-data-formats-page-number.cs
using IronBarCode;
using System;

// Read barcode from PDF
BarcodeResults result = BarcodeReader.ReadPdf("test.pdf");

// Output page number to console
foreach (BarcodeResult barcode in result)
{
    Console.WriteLine("The barcode value " + barcode.ToString() + " is found on page number " + barcode.PageNumber);
}
Imports IronBarCode
Imports System

' Read barcode from PDF
Private result As BarcodeResults = BarcodeReader.ReadPdf("test.pdf")

' Output page number to console
For Each barcode As BarcodeResult In result
	Console.WriteLine("The barcode value " & barcode.ToString() & " is found on page number " & barcode.PageNumber)
Next barcode
$vbLabelText   $csharpLabel

上面的程式碼片段展示了一個使用個案,使用者需要程式回傳在多頁 PDF 文件中找到的 BarCode 值及其各自的頁碼。 該代碼使用 BarcodeReader.ReadPdf() 方法讀取多頁 PDF 文件內的條碼,該方法會返回 BarcodeResults 物件,其中儲存在文件中找到的每個 BarcodeResults 物件。 我們使用循環遍歷物件中的每個項目,以檢索條碼的值以及找到條碼的頁碼。 除了這個使用案例之外,使用者也可以使用這個屬性來調試文件中的所有 BarCode 是否都被讀取。

請注意此屬性傳回的值是基於 1 ,這表示第一頁始終為1不是 0。

如何檢測 BarCode 旋轉和頁面方向?

使用 IronBarcode,用戶可以檢索條碼方向和找到條碼的頁面方向等資訊。 若要擷取這兩項資訊,請存取 BarcodeResult 物件中的 RotationPageOrientation 屬性。 Rotation 返回一個整數,代表找到的條碼的旋轉角度。 此功能與影像方向矯正功能結合使用,可確保無論掃描角度為何,都能精確讀取 BarCode。

請看下面的程式碼片段:

:path=/static-assets/barcode/content-code-examples/how-to/output-data-formats-orientation.cs
using IronBarCode;
using System;

// Read barcode from PDF
BarcodeResults result = BarcodeReader.ReadPdf("test.pdf");

// Output page orientation and rotation to console
foreach (BarcodeResult barcode in result)
{
    Console.WriteLine(barcode.Value);
    Console.WriteLine(barcode.PageOrientation);
    Console.WriteLine(barcode.Rotation);
}
Imports IronBarCode
Imports System

' Read barcode from PDF
Private result As BarcodeResults = BarcodeReader.ReadPdf("test.pdf")

' Output page orientation and rotation to console
For Each barcode As BarcodeResult In result
	Console.WriteLine(barcode.Value)
	Console.WriteLine(barcode.PageOrientation)
	Console.WriteLine(barcode.Rotation)
Next barcode
$vbLabelText   $csharpLabel

上面的程式碼片段是以所附的 PDF 輸入樣本執行,以證明使用者可以透過分別取得 BarcodeResult.PageOrientationBarcodeResult.Rotation 的值來擷取頁面方向和條碼旋轉。 此功能主要用於調試目的。

IronBarcode 只能讀取旋轉角度為0180 270 90條碼。 如果條碼的旋轉值與所列值不同,IronBarcode 將不會傳回任何值。 PageOrientation 返回一個 PageOrientation 物件,它由 PortraitLandscape 組成。)}]

文字屬性與值屬性有何差異?

當然,使用者在使用 IronBarcode 時最想取得的屬性是它的文字。 這兩個屬性通常可以交替使用,並且回傳相同的值。 除此之外,使用者可以使用 BarcodeResult.ToString() 方法來達到相同的結果。 當使用專門的應用程式或將條碼資料匯出為串流時,這些屬性可提供彈性的方式,以您偏好的格式存取條碼內容。

以下程式碼片段演示了:

:path=/static-assets/barcode/content-code-examples/how-to/output-data-formats-text-value.cs
using IronBarCode;
using System;

// Read barcode from PDF
BarcodeResults result = BarcodeReader.ReadPdf("barcodestamped3.pdf");

// Output text value to console
foreach (BarcodeResult barcode in result)
{
    Console.WriteLine(barcode.Value);
    Console.WriteLine(barcode.Text);
    Console.WriteLine(barcode.ToString());
}
Imports IronBarCode
Imports System

' Read barcode from PDF
Private result As BarcodeResults = BarcodeReader.ReadPdf("barcodestamped3.pdf")

' Output text value to console
For Each barcode As BarcodeResult In result
	Console.WriteLine(barcode.Value)
	Console.WriteLine(barcode.Text)
	Console.WriteLine(barcode.ToString())
Next barcode
$vbLabelText   $csharpLabel

從上面的程式碼片段來看,使用者只需要幾行程式碼就可以使用 IronBarcode 讀取影像中的 BarCode。 在遍歷 BarcodeReader.Read() 方法返回的 BarcodeResults 之後,我們將獲得 ValueText 屬性的結果輸出到控制台,同時呼叫 BarcodeResult.ToString() 方法,以顯示所有這些都返回相同的值。

簡而言之,IronBarcode 是一個完美的 API,使用者可以對條碼執行多種操作,不僅限於條碼的寫入和解碼。 IronBarcode 支援多種輸出資料格式,使用者可以利用 IronBarcode 傳回的 BarcodeResult 物件做更多的事情。

常見問題解答

C# 條碼讀取支援哪些輸出格式?

IronBarcode 提供多種輸出格式,包括 BarcodeImage、BarcodeType、BinaryValue、座標、尺寸、頁碼、方向、文字和值屬性。這些格式使各種 .NET 應用程式能夠全面處理條碼資料。

如何只用一行代碼就能讀取 BarCode 值?

使用 IronBarcode,您可以在一行中讀取一個條碼,使用: var result = IronBarCode.BarcodeReader.Read('input.png'); 這會立即讓您透過 result[0].Value 和 result[0].BarcodeType 獲得條碼的值和類型。

BarcodeResult 中有哪些屬性?

IronBarcode 中的 BarcodeResult 物件包含的屬性包括 BarcodeImage、BarcodeType、BinaryValue、Coordinates、Height & Width、PageNumber、Barcode、PageOrientation、Text 和 Value - 為條碼處理工作流程提供全面的資料。

閱讀後可以擷取並儲存 BarCode 影像嗎?

是的,IronBarcode 在 BarcodeImage 属性中将找到的条码存储为 AnyBitmap 对象。您可以擷取此物件以進一步處理影像,或將其儲存為永久副本,而不需要額外的程式碼來擷取條碼影像。

如何存取 BarCode 座標和尺寸?

IronBarcode 為每個檢測到的條碼提供坐標資料,包括 x 和 y 位置以及高度和寬度尺寸。這些屬性可透過 BarcodeResult 物件存取,以進行精確的條碼位置追蹤。

Text 與 Value 屬性有何差異?

在 IronBarcode 中,Text 和 Value 兩個屬性都包含條碼的資料內容。這些屬性是 BarcodeResult 物件的一部分,可以交替使用來擷取已解碼的條碼資訊。

我可以確定在哪一頁找到 BarCode 嗎?

是的,IronBarcode 在 BarcodeResult 对象中包含 PageNumber 属性,允许您准确识别多页文档或 PDF 中的哪一页包含每个检测到的条形码。

如何識別檢測到的 BarCode 類型?

IronBarcode 的 BarcodeResult 物件中的 BarcodeType 屬性可識別檢測到的特定條碼格式(如 QR Code、Code 128 等),從而在您的應用程式中實現特定格式處理。

Hairil Hasyimi Bin Omar
軟體工程師
就像所有優秀的工程師一樣,Hairil 也是一位狂熱的學習者。他不斷精進 C#、Python 和 Java 的知識,利用這些知識為整個 Iron Software 的團隊成員增加價值。Hairil 從馬來西亞的 Universiti Teknologi MARA 大學加入 Iron Software 團隊,畢業於該校的化學與流程工程學系,並取得學士學位。
準備好開始了嗎?
Nuget 下載 2,070,733 | 版本: 2026.2 剛剛發布