如何使用 IronBarcode 在 C# 中輸出資料格式
IronBarcode 提供多種 BARCODE 讀取輸出格式,包括 BinaryValue、座標、尺寸、頁碼、方向、文字及數值屬性。 這些格式可讓您透過程式化方式處理BARCODE資料,以因應各種應用情境。
IronBarcode 不僅僅是讀取 BARCODE 並將數值 PRINT 至控制台,它還能提供更多功能。 它提供多種輸出格式,為使用者後續處理讀取結果鋪平道路。 這些格式包含諸如BarCode圖像、BarCode類型、text 及 value 等屬性。
使用者可在程式內進一步操作這些屬性。 讓我們來探討如何使用這些屬性,以及它們在哪些情境下能派上用場。
快速入門:單行讀取BarCode值與類型
此範例展示如何使用 IronBarcode 從圖片中讀取 BARCODE——僅需一行程式碼即可載入,隨即列印 BARCODE 的數值與類型。 非常適合快速上手。如需更完整的範例,請參閱《BarCode快速入門指南》。
簡化工作流程(5 個步驟)
- 下載用於讀取 BarCode 的 C# 函式庫
- 準備 PDF 檔案與圖片以進行 BARCODE 偵測
- 存取偵測到的BarCode類型與影像
- 擷取BARCODE的
x與y座標,以及其height以及width - 讀取BARCODE的
text並value
有哪些可用的輸出格式及其應用情境?
BarcodeResult 儲存了各種有用的屬性。 這些屬性列舉如下:
BarcodeImageBarcodeTypeBinaryValue- 座標,
Height與Width PageNumberBarcode和PageOrientationText&Value
每個屬性在BarCode處理工作流程中皆有其特定用途。 無論是建置庫存管理系統、文件處理流程,還是品質控管應用程式,這些資料格式都能提供讀取各種來源BarCode所需的靈活性。
如何擷取並儲存BarCode圖像?
當 IronBarcode 讀取影像後,影像中找到的 BarCode 會以 BarcodeResult 格式儲存,作為類型為 BarcodeImage 的 AnyBitmap 屬性。 BarcodeImage 屬性用於儲存偵測到的 BARCODE 影像。 使用者可取得此物件以進一步處理影像,或將其儲存為永久副本。 此功能透過省去從圖片中擷取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")
上方的程式碼片段展示了此輸出格式的一個應用案例。 具體而言,它能根據 PDF 文件中偵測到的 BarCode,生成多頁 TIFF 影像。 首先,我們會掃描或偵測範例 PDF 中的 BarCode。 接著,我們建立一個名為 AnyBitmap 的清單,用以儲存來自 BarcodeImage 屬性的資訊。 最後,我們將使用此清單透過 CreateMultiFrameTiff 方法生成多頁 TIFF 檔案。 此技術在處理多頁面的 GIF 和 TIFF 檔案時特別有用。
BarcodeImage 來自 BarcodeResult 的屬性僅儲存讀取過程中發現的 BARCODE 影像,而非輸入影像的完整內容。
如何透過程式設計識別不同的BarCode類型?
此屬性有助於判定輸入影像或文件中包含何種BarCode。 然而,其限制在於圖片中的 BARCODE 類型必須為 IronBarcode 所支援且可讀取。 如需進一步了解 IronBarcode 支援的條碼類型,請參閱這篇文章。 此外,請查閱支援的BarCode格式完整清單,以確保符合您的特定需求。
以下程式碼片段示範如何從圖片中擷取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
根據上方的程式碼片段,我們透過在輸入影像上呼叫 BarcodeReader.Read() 方法來執行 BARCODE 讀取。 此方法會傳回一個 BarcodeResults 物件,該物件儲存了透過讀取影像中所有 BARCODE 所取得的 BarcodeResult。 接著,我們遍歷 BarcodeResults 物件以擷取 BarcodeResult,並將 BARCODE value 和 type 輸出至主控台。 此方案可無縫支援各種 BARCODE 類型,包括 Code 39 BARCODE 等特殊格式。
何時應使用二進位值輸出?
Using IronBarcode, users can retrieve the BARCODE value byte array by accessing the BinaryValue property of the BarcodeResult object. 這讓使用者能在程式內部進一步處理BARCODE value。 二進位 value 輸出在處理加密資料、以 BARCODE 編碼的檔案附件,或與需要位元組級資料處理的系統整合時,特別有用。
以下程式碼片段示範了將 BARCODE value 作為二進位資料擷取的其中一個應用案例:
: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
參照上方的程式碼片段,我們建立了一個簡單的程式,能將圖片中的多個BarCode轉換為獨立的新二進位編碼檔案。 首先,我們會掃描範例 PNG 圖片中的 BARCODE。 一旦偵測到這些 BARCODE,我們便會遍歷它們,存取 BinaryValue 屬性,並利用它來建立新的二進位檔案。 當您需要讀取多個BARCODE並分別處理其二進位資料時,此技術尤為實用。
如何取得BarCode的位置與尺寸?
使用者可存取的 BarcodeResult 物件的另一項屬性是 BARCODE 的座標,包括 Y2,以及其位於影像檔案或文件中的 Height 和 Width。 當使用者需要取得BarCode的位置與尺寸資訊時,這些屬性便十分有用。 此空間資訊對於需要精確定位的應用程式至關重要,例如自動化文件處理、品質控制系統,或是在實施裁切區域以優化BarCode掃描時。
讓我們來展示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

校對前

經修訂後
上方的程式碼片段會將影像檔案中的多個BarCode進行遮蔽。為達成此目的,我們結合了 IronBarcode 與 IronDrawing 這兩套函式庫。 要取得 BarcodeResult 物件並從中擷取屬性,我們首先會使用 BarcodeReader.Read() 方法讀取影像檔案中的 BARCODE。 同時,需將輸入的圖像檔案轉換為 AnyBitmap 物件,以便對圖像套用遮蔽處理方法。 取得 BarcodeResults 物件後,我們可以套用迴圈並遍歷該物件,以取得圖中每個條碼的 Width 以及 Height,並將其用於 CropRectangle 方法的 AnyBitmap.Redact() 屬性中。
對於多頁文件而言,頁碼為何如此重要?
使用者可取得發現BarCode的頁碼。 對於使用包含多個BarCode的多頁文件,且需要了解文件中BarCode位置以便進行後續處理的用戶而言,這是一項實用的功能。 此功能對於從 PDF 文件讀取 BarCode,或在 Enterprise 應用程式中處理批次文件而言至關重要。
請參閱以下程式碼片段:
: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
上方的程式碼片段展示了一個使用案例,其中使用者需要程式返回多頁 PDF 文件中發現的 BARCODE value 及其對應的頁碼。 該程式碼使用 BarcodeReader.ReadPdf() 方法讀取多頁 PDF 文件中的 BarCode,此方法會傳回 BarcodeResults 物件,其中儲存了文件中找到的每個 BarcodeResult。 我們使用迴圈遍歷物件中的每個項目,以擷取BARCODE的 value 以及發現BARCODE的頁碼。 除了此使用情境外,使用者亦可利用此屬性進行除錯,以確認文件中的所有BARCODE是否皆已讀取。
如何偵測BarCode旋轉與頁面方向?
Using IronBarcode,使用者可取得條碼方向以及發現條碼之頁面的方向資訊。 若要擷取這兩項資訊,請從 BarcodeResult 物件存取 Rotation 和 PageOrientation 屬性。 Rotation 會傳回一個整數,代表所找到 BarCode 的旋轉角度。 此功能結合影像方向校正能力,確保無論掃描角度為何,皆能準確讀取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
上方的程式碼片段已搭配附帶的範例 PDF 檔案執行,以證明使用者可透過分別取得 pageOrientation 和 BARCODE rotation,來檢索頁面方向與 BARCODE 旋轉角度。 此功能主要用於除錯。
180 以及 270 度的旋轉角度。 若 BarCode 的旋轉角度值不在上述範圍內,IronBarcode 將不會傳回任何值。 PageOrientation 會傳回一個 PageOrientation 物件,該物件由 Portrait 或 Landscape 組成。)}]文字屬性與值屬性有何區別?
當然,使用者在使用 IronBarcode 時最想擷取的主要屬性,就是其 value 和 text。 這兩個屬性常被互換使用,且會返回相同的 value。 此外,使用者亦可使用 BarcodeResult.ToString() 方法來達到相同效果。 當您使用專業應用程式或將BarCode資料匯出為資料流時,這些屬性可提供靈活的方式,讓您以偏好的格式存取BarCode內容。
以下程式碼片段示範:
: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
從上方的程式碼片段可見,使用者只需幾行程式碼,即可透過 IronBarcode 讀取圖片中的 BARCODE。 在遍歷 BarcodeResults 方法所傳回的 BarcodeReader.Read() 後,我們將取得 Value 和 Text 屬性的結果,並呼叫 BarcodeResult.ToString() 方法,以展示所有這些操作皆會返回相同的 value。
簡而言之,IronBarcode 是一套完美的 API,讓使用者能執行與 BARCODE 相關的多項操作,不僅限於 BARCODE 的寫入與解碼。 由於支援多種輸出資料格式,使用者能透過 IronBarcode 所回傳的 BarcodeResult 物件執行更多操作。
常見問題
C# 條碼讀取支持哪些輸出格式?
IronBarcode 提供多種輸出格式,包括 BarcodeImage、BarcodeType、BinaryValue、坐標、尺寸、頁碼、方向、文字和價值屬性。這些格式可實現條碼數據的全面處理,用於各種 .NET 應用程式。
如何在一行代碼中讀取條碼價值?
使用 IronBarcode,您可以使用以下方法在一行中讀取條碼:var result = IronBarCode.BarcodeReader.Read('input.png'); 這立即讓您能夠通過 result[0].Value 和 result[0].BarcodeType 訪問條碼的價值和類型。
BarcodeResult 中有哪些屬性可用?
IronBarcode 的 BarcodeResult 物件包含屬性,包括 BarcodeImage、BarcodeType、BinaryValue、坐標、高度和寬度、頁碼、條碼、頁面方向、文字和價值 - 提供條碼處理工作流的全面數據。
讀取後,我可以提取並保存條碼影像嗎?
可以,IronBarcode 將發現的條碼存儲為 BarcodeImage 屬性中的 AnyBitmap 物件。您可以檢索此物件以進一步處理影像或保存為永久副本,無需額外的代碼提取條碼影像。
如何訪問條碼的坐標和尺寸?
IronBarcode 提供坐標數據,包括 x 和 y 位置以及檢測到的條碼的高度和寬度尺寸。這些屬性通過 BarcodeResult 物件訪問,以精確跟蹤條碼位置。
Text 和 Value 屬性之間有什麼區別?
在 IronBarcode 中,Text 和 Value 屬性都包含條碼的數據內容。這些屬性是 BarcodeResult 物件的一部分,可互換使用以檢索解碼的條碼信息。
我能否確定在哪一頁找到條碼?
可以,在 BarcodeResult 物件中,IronBarcode 包含一個 PageNumber 屬性,允許您準確識別多頁文檔或 PDF 中的哪一頁包含檢測到的每一個條碼。
如何確定檢測到的條碼類型?
IronBarcode 的 BarcodeResult 物件中的 BarcodeType 屬性識別出檢測到的特定條碼格式(例如 QR Code、Code 128 等),允許在您的應用程式中進行格式特定的處理。
將IronBarcode實現於專案中需要什麼程式設計技能?
基本的C#程式設計知識足以將IronBarcode實現於專案中,因為它提供了簡單的方法和全面的文檔來指導開發者。
IronBarcode適合於小型專案和大型企業應用嗎?
IronBarcode設計為可擴展且多功能,使其適合小型專案和需要強大條碼解決方案的大型企業應用。

