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

How to Output Data Formats

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

IronBarcode 不僅僅是讀取條碼並在控制台中打印數據,它提供了更多的功能。 它提供了幾種輸出格式,為用戶進一步處理讀取結果鋪平了道路。 這些格式包括條碼圖像、條碼類型、BinaryValue、坐標、高度、寬度、頁數、條碼、頁面方向、文本和數值等屬性。

用戶可以在程序中進一步操作這些屬性。 讓我們來探索如何使用這些屬性以及它們在哪些用例中有幫助。

快速入門:一行讀取條碼值和類型

此示例展示了如何簡單地使用 IronBarcode 從圖像中讀取條碼——只需一行即可加載,然後立即打印條碼的值和類型。 這對快速入門非常完美。

Nuget IconGet started making PDFs with NuGet now:

  1. Install IronBarcode with NuGet Package Manager

    PM > Install-Package BarCode

  2. Copy and run this code snippet.

    var result = IronBarCode.BarcodeReader.Read("input.png");
    Console.WriteLine($"Value: {result[0].Value}, Type: {result[0].BarcodeType}");
  3. Deploy to test on your live environment

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

簡約工作流程(5 步)

  1. 下載讀取條碼的 C# 庫
  2. 準備用於條碼檢測的 PDF 和圖像
  3. 訪問檢測到的條碼類型和圖像
  4. 檢索條碼的 x 和 y 坐標,以及它的高度和寬度
  5. 讀取條碼的文本和數值

輸出格式和用例

BarcodeResult 儲存了各種有用的屬性。 這些屬性列在下面:

  • BarcodeImage
  • BarcodeType
  • BinaryValue
  • 坐標、高度及寬度
  • PageNumber
  • BarcodePageOrientation
  • 文本和數值

條碼圖像

一旦 IronBarcode 對圖像進行讀取,存在於圖像中的條碼將被儲存在 BarcodeResult 中作為類型為 AnyBitmapBarcodeImage 屬性。 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,以及其在圖像文件或文檔中的 高度寬度。 當用戶需要檢索條碼位置和尺寸信息時,這些屬性非常有用。

讓我們用一個例子來突出條碼的位置和尺寸。

: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
class="competitors-section__wrapper-even-1">
重編輯前的樣本輸入
重編輯後圖像

上面的代碼片段用於重編輯圖像文件中找到的多個條碼。為此,我們使用了 IronBarcode 和 IronDrawing 兩個庫的組合。 要獲取 BarcodeResult 對象並從中提取屬性,我們首先使用 BarcodeReader.Read() 方法讀取圖像文件中的條碼。 同時,輸入圖像文件還需要轉換為 AnyBitmap 對象,以將重編輯方法應用於圖像。 一旦我們得到了 BarcodeResults 對象,我們可以應用一個循環並迭代它,以獲取每個條碼的 X1Y1寬度高度,並將它們用於 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-基準,即第一頁永遠是不是零

條碼旋轉和頁面方向

使用 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 僅能讀取旋轉角度為 090180270 度的條碼。 如果條碼旋轉角度與此不同,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# 中可用於條碼處理的輸出數據格式有哪些?

IronBarcode 在 .NET C# 中提供了多種條碼處理的輸出數據格式,包括條碼圖像、類型、`BinaryValue`、坐標、高度、寬度、頁碼、方向、文本和價值。這些格式促進了多樣的條碼相關操作。

如何使用 C# 捕捉和存儲條碼圖像?

您可以使用 IronBarcode 中的 `BarcodeImage` 屬性來捕捉和存儲條碼圖像,這對於如從檢測到的條碼創建多頁 TIFF 文件等任務非常有幫助。

如何在圖像中識別條碼的類型使用 C#?

IronBarcode 中的 `BarcodeType` 屬性可幫助您識別圖像中存在的條碼類型。這有助於有效地處理支持的條碼類型。

如何將條碼數據轉換為字節數組?

通過訪問 IronBarcode 中的 `BinaryValue` 屬性,您可以將條碼數據轉換為字節數組,這可以用於進一步的數據操作或存儲。

如何獲取圖像中條碼的坐標和尺寸?

IronBarcode 提供了位於 `BarcodeResult` 對象中的坐標、高度和寬度屬性,允許您獲取有關條碼在圖像中的位置和尺寸的詳細信息。

如何使用條碼數據管理多頁文檔?

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,935,276 | 版本: 2025.11 剛剛發布