如何輸出數據格式
IronBarcode 不僅僅是讀取條碼並將數值顯示在控制台中,它還提供了更多功能。它提供了多種輸出格式,使用戶能夠進一步處理讀取結果。這些格式包括條碼圖片、條碼類型、BinaryValue
、座標、高度、寬度、頁碼、條碼、頁面方向、文本和數值等屬性。
用戶可以在程序中進一步操作這些屬性。讓我們來探討如何使用這些屬性以及它們在哪些使用案例中會有所幫助。
如何輸出數據格式
- 下載讀取條碼的C#庫
- 準備用於條碼檢測的 PDF 和圖像
- 訪問檢測到的條碼類型和圖像
- 檢索條碼的 x 和 y 座標,以及其高度和寬度
- 讀取條碼的文本和數值
立即開始在您的專案中使用IronPDF,並享受免費試用。
查看 IronBarcode 上 Nuget 快速安裝和部署。已被下載超過800萬次,它正用C#改變。
Install-Package BarCode
請考慮安裝 IronBarcode DLL 直接下載並手動安裝到您的專案或GAC表單: IronBarCode.zip
手動安裝到您的項目中
下載DLL輸出格式和使用案例
BarcodeResult
存儲各種有用的屬性。這些屬性如下所示:
BarcodeImage
BarcodeType
BinaryValue
- 座標、高度和寬度
PageNumber
Barcode
和PageOrientation
- 文本和值
條碼圖像
一旦 IronBarcode 對圖像進行讀取處理,圖像中找到的條碼將被存儲在 BarcodeResult
中,作為類型為 AnyBitmap
的 BarcodeImage
屬性。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")
上面的代碼片段展示了這種輸出格式的一個使用案例。具體來說,它被設計用來從 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
從上面的程式碼片段中,我們透過調用 BarcodeReader.Read
執行條碼讀取。()在輸入圖像上使用 method
。這返回一個 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
觀察上面的代碼片段,我們創建了一個簡單的程序,實質上將圖像中的多個條碼轉換為單獨的新的 QR 碼圖像。首先,我們在範例 PNG 圖像中掃描條碼。一旦檢測到這些條碼,我們遍歷它們,訪問 BinaryValue 屬性,然後使用它來創建新的 QR 碼條碼。
條碼座標、高度與寬度
使用者可以存取 BarcodeResult
物件的另一個屬性,就是條碼的座標,包括 X1、Y1 和 X2、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);
}
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
在區塊處理之前
經過編輯後
上面的代碼片段用於在圖像文件中遮蔽多個條碼。為了實現這一點,我們使用了兩個庫的組合,即IronBarcode和IronDrawing。要獲取 BarcodeResult
對象並從中提取屬性,我們首先使用 BarcodeReader.Read
來讀取圖像文件中可用的條碼。()方法。同時,輸入的影像檔案也需要轉換成 AnyBitmap
對象,以便在圖像上使用和應用編輯方法。一旦我們有了 BarcodeResults
對象,就可以應用循環並迭代它來獲取圖像中每個條形碼的 X1、Y1、寬度 和 高度,並將它們用作 CropRectangle
屬性在 AnyBitmap.Redact()
方法。
頁碼
使用者還可以檢索找到條碼的頁碼。這對於需要使用包含多個條碼的多頁文件的使用者來說是一個有用的功能,並且需要知道在文件中找到條碼的位置以便進一步處理。
讓我們看看下面的程式碼片段:
: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文檔中找到的條碼值及其頁碼時。上面的代碼片段使用了 BarcodeReader.ReadPdf()從多頁 PDF 文件中讀取條碼的方法,會返回
BarcodeResults物件,該物件存儲在文件中找到的每個
BarcodeResult`。應用迴圈並遍歷物件中的每個項目以檢索條碼的值和找到條碼的頁碼。除了這個用例之外,用戶也可以使用此屬性來調試文件中的所有條碼是否都能夠被讀取。
請注意
條碼旋轉和頁面方向
使用 IronBarcode,使用者還可以檢索有關條碼方向以及找到條碼的頁面方向的資訊。要提取這兩個資訊,使用者可以從 BarcodeResult
對象的 Rotation
和 PageOrientation
屬性中檢索。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
上面的簡單代碼片段使用附加的示例PDF輸入運行,以證明用戶可以通過分別獲取BarcodeResult.PageOrientation
和BarcodeResult.Rotation
的值來檢索頁面方向和條形碼旋轉。此功能主要用於除錯目的。
[{ i:(IronBarcode 只能讀取旋轉為 0, 90, 180 和 270 度的條碼。如果條碼的旋轉值不在所述範圍內,IronBarcode 將不會返回任何值。PageOrientation
將返回 頁面方向
物件,包括縱向或橫向。)}]
文字與數值
當然,使用 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
從上面的代碼片段來看,用戶只需使用幾行代碼即可使用 IronBarcode 在圖像中讀取條形碼。在迭代 BarcodeReader.Read
返回的 BarcodeResults
之後,()方法中,我們將取得值和文字屬性、以及調用BarcodeResult.ToString
的結果輸出到控制台。()簡而言之,IronBarcode 是一個完美的 API,允許用戶執行多種條碼相關操作,不僅限於書寫和解碼條碼。IronBarcode 所返回的 BarcodeResult
物件支持多種輸出數據格式,用戶可以做更多的事情。