如何在 C# 中輸出 BarCode 資料格式

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

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

IronBarcode 的功能遠不止於讀取條碼並在控制台中列印值。 它提供了多種輸出格式,為使用者進一步處理讀取結果鋪平了道路。 這些格式包含條碼圖像、條碼類型、 BinaryValue 、座標、高度、寬度、頁碼、條碼、頁面方向、文字和值等屬性。

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

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

這個例子展示了使用 IronBarcode 從圖像中讀取條碼是多麼容易——只需加載一行程式碼,然後立即列印條碼的值和類型。 它非常適合快速入門。

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

輸出格式和使用案例

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

BarcodeImage BarcodeType -二BinaryValue

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

條碼影像

IronBarcode 對影像執行讀取過程後,影像中找到的條碼將作為AnyBitmap類型的BarcodeImage屬性儲存在BarcodeResult中。 BarcodeImage屬性用於儲存找到的條碼影像。 使用者可以檢索此物件以進一步處理影像或將其儲存為永久副本。 這樣就無需編寫額外的程式碼來從圖像中提取條碼圖像,從而提高了效率並方便使用。

讓我們來看看下面的程式碼片段,它展示了這種輸出格式的一個可能用例:

: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 文件中檢測到的條碼創建多頁 TIFF 影像。 首先,我們掃描或偵測樣本 PDF 中的條碼。 然後,我們建立一個AnyBitmap列表,並將BarcodeImage屬性中的資訊儲存在其中。 最後,我們利用該列表,透過CreateMultiFrameTiff方法產生多頁 TIFF 檔案。

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

條碼類型

此屬性可協助使用者確定輸入影像或文件中存在的條碼類型。 但是,此功能的限制在於影像內的條碼類型必須是 IronBarcode 支援且可讀取的類型。 要了解 IronBarcode 支援的條碼類型,使用者可以參考這篇文章

下面的程式碼片段示範了使用者如何透過在控制台上列印值來檢索影像中的條碼值以及條碼類型。

: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 ,並將條碼值和類型列印到控制台。

二進制值

使用 IronBarcode,使用者還可以透過存取BarcodeResult物件的BinaryValue屬性來檢索條碼值的位元組數組。 這樣使用者就可以在程式內部進一步操作條碼值。

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

: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 影像中的條碼。 一旦我們偵測到這些條碼,我們就會遍歷它們,存取BinaryValue屬性,然後使用它來建立新的二進位檔案。

條碼座標,高度和寬度

使用者可以存取BarcodeResult物件的另一個屬性是條碼的座標,包括X1Y1X2Y2 ,以及其在圖像檔案或文件中的HeightWidth 。 當使用者需要檢索有關條碼的位置和尺寸的資訊時,這些屬性非常有用。

讓我們用圖示來突出條碼的位置和尺寸。

: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
Sample input before redaction
Redacted image

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

頁碼

使用者也可以檢索條碼所在的頁碼。 對於需要使用包含多個條碼的多頁文檔,並且需要知道文檔中條碼位置以便進行進一步處理的使用者來說,這是一個很有用的功能。

讓我們來看看下面的程式碼片段:

: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 文件中找到的條碼的值及其各自的頁碼。 上面的程式碼片段使用BarcodeReader.ReadPdf()方法讀取多頁 PDF 文件中的條碼,該方法傳回BarcodeResults對象,該物件儲存文件中找到的每個BarcodeResult 。 我們使用循環遍歷物件中的每個項目,以檢索條碼的值以及找到條碼的頁碼。 除了上述用例之外,使用者還可以使用此屬性來調試文件中的所有條碼是否都能被讀取。

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

條碼旋轉和頁面方向

使用 IronBarcode,使用者還可以擷取有關條碼方向和條碼所在頁面方向的資訊。 要提取這兩個訊息,使用者可以從BarcodeResult物件存取RotationPageOrientation屬性。 Rotation將傳回一個整數,表示所找到的條碼的旋轉角度。

讓我們來看看下面的程式碼片段:

: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 來讀取影像中的條碼。 在遍歷BarcodeReader.Read()方法傳回的BarcodeResults之後,我們將取得值和文字屬性的結果輸出到控制台,並呼叫BarcodeResult.ToString()方法以顯示所有這些都傳回了相同的值。

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

常見問題解答

.NET C# 中的 BarCode 處理有哪些輸出資料格式?

IronBarcode 為 .NET C# 中的條碼處理提供多種輸出資料格式,包括條碼影像、類型、`BinaryValue`、座標、高度、寬度、頁碼、方向、文字和數值。這些格式方便了多樣化的條碼相關操作。

如何使用 C# 擷取並儲存 BarCode 影像?

您可以使用 IronBarcode 中的 `BarcodeImage` 屬性來擷取和儲存條碼影像,這對於從偵測到的條碼建立多頁 TIFF 檔案等工作很有幫助。

如何使用 C# 識別影像中的 BarCode 類型?

IronBarcode 中的 `BarcodeType` 属性使您能够识别图像中存在的条形码类型。這有助於有效地處理支援的條碼類型。

如何將 BarCode 資料轉換成位元組陣列?

通過訪問 IronBarcode 中的 `BinaryValue` 屬性,您可以將條碼資料轉換成一個位元組陣列,該陣列可用於進一步的資料處理或存儲。

如何在影像中取得 BarCode 的座標和尺寸?

IronBarcode 在 `BarcodeResult` 物件中提供了坐標、高度和寬度屬性,使您可以獲得條碼在圖像中的位置和尺寸的詳細資訊。

如何使用 BarCode 資料管理多頁文件?

IronBarcode 中的 `PageNumber` 和 `PageOrientation` 屬性可識別每個條碼的位置及其方向,有助於管理多頁文件,從而提高文件處理效率。

C# 中條碼的 `text` 和 `value` 屬性有何差異?

在IronBarcode中,`text`和`value`屬性都會為條碼返回相同的輸出。您可以使用 `BarcodeResult.ToString()` 方法來達成相似的結果。

使用 IronBarcode 進行條碼旋轉檢測是否有任何限制?

是的,IronBarcode 可以讀取以 0、90、180 和 270 度旋轉的條碼。它不支援其他旋轉角度的條碼偵測。

Hairil Hasyimi Bin Omar
軟體工程師
和所有优秀的工程师一样,Hairil 是个努力学习者。他正在细化自己的 C# 、Python 和 Java 知识,将这些知识应用于 Iron Software 各个团队成员以增加价值。Hairil 自马来西亚 Universiti Teknologi MARA 加入 Iron Software 团队,并以化学与工艺工程学士学位毕业。
準備好開始了嗎?
Nuget 下載 1,979,979 | Version: 2025.11 剛發表