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

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

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

IronBarcode提供多種條碼讀取輸出格式,包括 BinaryValue、座標、尺寸、頁碼、方向、文字和值屬性。 這些格式支援以程式設計方式處理條碼數據,適用於各種應用場景。

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

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

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

本範例展示如何使用IronBarcode從圖像中讀取條碼——只需一行程式碼加載,即可立即列印條碼的值和類型。 非常適合快速入門。如需更全面的範例,請查看條碼快速入門指南

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

    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讀取影像後,影像中找到的條碼將儲存在 BarcodeResult 中,作為 BarcodeImage 類型為 AnyBitmap 的屬性。 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");
$vbLabelText   $csharpLabel

上面的程式碼片段展示了這種輸出格式的一個使用場景。 具體來說,它可以根據在 PDF 文件中檢測到的條碼建立多頁 TIFF 影像。 首先,我們掃描或偵測樣本 PDF 中的條碼。 然後,我們建立一個 AnyBitmap 列表,並將 BarcodeImage 屬性中的資訊儲存在其中。 最後,我們使用此列表透過 CreateMultiFrameTiff 方法產生多頁 TIFF。 該技術在處理多頁 GIF 和 TIFF 檔案時特別有用。

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

如何透過程式設計方式辨識不同的條碼類型?

此屬性有助於確定輸入影像或文件中存在的條碼類型。 但是,其限制在於圖像內的條碼類型必須是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);
}
$vbLabelText   $csharpLabel

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

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

使用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++;
}
$vbLabelText   $csharpLabel

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

如何取得條碼的位置和尺寸?

使用者可以存取的 BarcodeResult 物件的另一個屬性是條碼的座標,包括 Y2 當使用者需要檢索有關條碼的位置和尺寸的資訊時,這些屬性非常有用。 這種空間資訊對於需要精確定位的應用至關重要,例如自動化文件處理、品質控制系統,或在實施作物區域以優化條碼掃描時。

讓我們來示範一下條碼的位置和尺寸。

: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);
}
$vbLabelText   $csharpLabel
Three barcode samples (A, B, C) showing different encoded data with similar visual patterns
Three redacted content blocks with illegible text fragments

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

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

使用者可以檢索到條碼所在的頁碼。 對於使用包含多個條碼的多頁文檔,並且需要知道文檔中找到的條碼的位置以便進行進一步處理的用戶來說,這是一個很有用的功能。 在企業應用程式中,讀取 PDF 文件中的條碼或處理批次文件時,此功能至關重要。

請查看以下程式碼片段:

: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);
}
$vbLabelText   $csharpLabel

上面的程式碼片段示範了一個用例,使用者需要程式傳回在多頁 PDF 文件中找到的條碼的值及其各自的頁碼。 該代碼使用 BarcodeReader.ReadPdf() 方法讀取多頁 PDF 文件中的條碼,返回 BarcodeResults 對象,該對象存儲文檔中找到的每個 BarcodeResult。 我們使用循環遍歷物件中的每個項目,以檢索條碼的值以及找到條碼的頁碼。 除了上述用例之外,使用者還可以使用此屬性來偵錯文件中的所有條碼是否都已讀取。

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

如何偵測條碼旋轉和頁面方向?

使用IronBarcode,使用者可以檢索有關條碼方向和條碼所在頁面方向的資訊。 要提取這兩個訊息,請從 @@--CODE-695--CODE-695 物件存取 @@--CODE-693--CODE-694--CODE-695 屬性。 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);
}
$vbLabelText   $csharpLabel

上面的程式碼片段與附加的範例 PDF 輸入一起運行,以證明使用者可以分別透過取得 BarcodeResult.PageOrientationBarcodeResult.Rotation 的值來檢索頁面方向和條碼旋轉。 此功能主要用於調試目的。

請注意IronBarcode只能讀取旋轉角度為 180270 度的條碼。 如果條碼的旋轉值與所列值不同, 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());
}
$vbLabelText   $csharpLabel

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

簡而言之, IronBarcode是一個完美的 API,使用者可以對條碼執行多種操作,不僅限於條碼的寫入和解碼。 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,121,847 | 版本: 2026.3 剛剛發布
Still Scrolling Icon

還在捲動嗎?

想要快速證明? PM > Install-Package BarCode
執行範例 看您的字串變成 BarCode。