如何輸出數據格式
與其僅讀取條碼並在控制台中列印值,IronBarcode 提供了更多功能。 它提供了多種輸出格式,為用戶進一步處理讀取結果鋪平了道路。 這些格式包括如條碼圖像、條碼類型、BinaryValue
、坐標、高度、寬度、頁碼、條碼、頁面方向、文字和值等屬性。
用戶可以在程序中進一步操作這些屬性。 讓我們探索如何使用這些屬性以及它們可能有幫助的使用案例。
如何輸出數據格式
- 下載讀取條碼的C#庫
- 準備用於條碼檢測的 PDF 和圖像
- 訪問檢測到的條碼類型和圖像
- 檢索條碼的 x 和 y 座標,以及其高度和寬度
- 讀取條碼的文本和數值
開始使用 IronBarcode
立即在您的專案中使用IronBarcode,並享受免費試用。
輸出格式和使用案例
BarcodeResult
儲存了多種有用的屬性。 以下列出了這些屬性:
條碼圖像
條碼類型
二進位值
- 座標、高度和寬度
頁碼
- 條碼和頁面方向
- 文字與值
條碼圖像
一旦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
執行條碼讀取。()方法在輸入圖像上。 此操作返回一個
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
觀察上述的程式碼片段,我們創建了一個簡單的程序,本質上將圖像中的多個條形碼轉換為分開的新二維碼圖像。 最初,我們掃描範例 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**、**寬度** 和 **高度**,並將它們作為
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
上述簡單的代碼片段展示了一種使用案例,其中用戶需要程序返回在多頁PDF文檔中找到的條形碼的值及其頁碼。 上面的代碼片段使用了 BarcodeReader.ReadPdf
()用於讀取多頁PDF文件中的條形碼的方法,該方法返回了一個儲存文件中每個BarcodeResult
的BarcodeResults
對象。 在迴圈中遍歷物件中的每個項目,以取得條碼的值和找到條碼的頁碼。 除了這個使用案例之外,使用者還可以使用此屬性來調試文檔中的所有條形碼是否都能被讀取。
請注意
條碼旋轉和頁面方向
使用 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
的值來檢索頁面方向和條碼旋轉。 此功能主要用於調試目的。
[{我(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,讓用戶執行多項條碼相關操作,不僅限於寫入和解碼條碼。 使用BarcodeResult
物件,用戶可以支援各種輸出數據格式,從而做更多事情。