如何使用IronBarcode在C#中输出数据格式
IronBarcode提供多种条码读取后的输出格式,包括BarcodeImage、BarcodeType、BinaryValue、坐标、尺寸、页码、方向、文本和数值属性。 这些格式能够以编程方式处理条形码数据,适用于各种用例。
IronBarcode 不只是简单地读取条形码并在控制台中打印数值,它还提供了更多的功能。 它提供了多种输出格式,便于用户处理读取结果。 这些格式包括条码图像、条码类型、BinaryValue、坐标、高度、宽度、页码、条码、页面方向、text和value等属性。
用户可以在程序中进一步操作这些属性。 让我们来探讨一下如何使用这些属性以及在哪些用例中它们会有所帮助。
快速入门:一行代码读取条形码值和类型这个示例展示了如何使用IronBarcode从图像中读取条码——一行代码加载,然后立即打印条码的值和类型。 非常适合快速入门。有关更全面的示例,请查看条码快速入门指南。
-
1使用 IronBarcode 安装 NuGet Package Manager
-
2复制并运行这段代码。
var result = IronBarCode.BarcodeReader.Read("input.png"); Console.WriteLine($"Value: {result[0].Value}, Type: {result[0].BarcodeType}");Dim result = IronBarCode.BarcodeReader.Read("input.png") Console.WriteLine($"Value: {result(0).Value}, Type: {result(0).BarcodeType}")Dim result = IronBarCode.BarcodeReader.Read("input.png") Console.WriteLine($"Value: {result(0).Value}, Type: {result(0).BarcodeType}") -
3部署到您的生产环境中进行测试
今天就用免费试用在您的项目中开始使用IronBarcode
最小工作流程(5 个步骤)
- 下载用于读取条码的C#库
- 准备 PDF 和图像以进行条码检测
- 访问检测到的条码类型和图像
- 检索条形码的
x和y坐标,以及其height和width - 读取条形码的
text和value
可用的输出格式及其使用场景有哪些?
BarcodeResult存储各种有用的属性。 这些属性如下所列:
BarcodeImageBarcodeTypeBinaryValue- 坐标、
Height&Width PageNumberBarcode和PageOrientationText&Value
每种属性在 BarCode 处理工作流程中都有特定用途。 无论是构建库存管理系统、文档处理流水线,还是质量控制应用,这些数据格式都提供了从各种来源读取条码所需的灵活性。
如何提取和保存条码图像?
一旦IronBarcode读取图像,图像中发现的条码将存储在BarcodeResult中,作为BarcodeImage属性,其类型为AnyBitmap。 BarcodeImage属性存储找到的条码图像。 用户可以检索该对象以进一步处理图像,或将其保存为永久副本。 这样可以省去从图像中提取 BarCode 图像的额外代码,从而提供效率和易用性。
下面的代码片段演示了这种输出格式的可能用例:
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");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")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中的条码。 然后,我们创建一个AnyBitmap列表,将BarcodeImage属性中的信息存储下来。 最后,我们使用这个列表通过CreateMultiFrameTiff方法生成一个多页TIFF。 这种技术在处理多页GIF和TIFF文件时特别有用。
BarcodeImage 属性来自 BarcodeResult,只存储在读取过程中找到的条码图像,而不是整个输入图像本身。如何通过编程识别不同的条码类型?
该属性有助于确定输入图像或文档中的条形码类型。 不过,限制条件是图像内的条码类型必须是 IronBarcode 支持和可读的。 要了解IronBarcode支持的条码类型,请参考这篇文章。 此外,探索支持的条码格式完整列表,以确保与您的特定要求兼容。
下面的代码片段演示了如何通过将值打印到控制台来检索图像中的条形码值和条形码类型。
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);
}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 barcodeImports 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,并将条码value和type打印到控制台。 这种方法适用于各种条码类型,包括像Code 39条码这样的专业格式。
何时应使用二进制值输出?
使用IronBarcode,用户可以通过访问BarcodeResult对象的BinaryValue属性来检索条码value的字节数组。 这允许用户在程序内部进一步操作条码value。 二进制value输出在处理加密数据、条码中编码的文件附件或与需要字节级数据处理的系统集成时特别有用。
下面的代码段演示了将条码value作为二进制数据检索的一种用例:
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++;
}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 barcodeImports 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属性,并用它来创建新的二进制文件。 当您需要读取多个条码并单独处理它们的二进制数据时,这种技术尤其有价值。
如何获取条码位置和尺寸?
用户可以访问BarcodeResult对象的另一个属性,即通过其Points获取条码位置(条码角点坐标的PointF[]),以及图像文件或文档内的Height和Width。 当用户需要检索有关 BarCode 位置和尺寸的信息时,这些属性非常有用。 这些空间信息对于需要精确定位的应用程序至关重要,如自动文档处理、质量控制系统,或在实现裁剪区域以优化条码扫描时。
让我们来演示一下 BarCode 的位置和尺寸。
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);
}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 barcodeImports 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对象,我们可以应用循环并通过它获取每个可用条码的Points、Width和Height(从Points导出每个条码的左上x1/y1角),并在AnyBitmap.Redact()方法的CropRectangle属性中使用它们。
为什么页码对于多页文档很重要?
用户可以检索发现 BarCode 的页码。 对于使用包含多个条形码的多页文档并需要知道在文档中发现的条形码位置以便进一步处理的用户来说,这是一项非常有用的功能。 此功能在从PDF文档读取条码或在企业应用程序中处理批量文档时非常重要。
请看下面的代码片段:
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);
}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 barcodeImports 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文档中找到的条码value及其各自的页码。 代码使用BarcodeReader.ReadPdf()方法读取多页PDF文档内的条码,返回存储在文档中找到的每个BarcodeResult的BarcodeResults对象。 我们应用循环遍历对象中的每个项目以检索条码的value以及找到条码的页码。 除此用例之外,用户还可以使用此属性来调试文档中的所有 BarCode 是否都已读取。
如何检测条码旋转和页面方向?
使用 IronBarcode,用户可以检索条形码方向和发现条形码的页面方向信息。 要提取这两条信息,请从BarcodeResult对象访问Rotation和PageOrientation属性。 Rotation返回一个代表找到条码旋转角度的整数。 此功能与图像方向校正功能结合使用,以确保无论扫描角度如何都能准确读取条码。
请看下面的代码片段:
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);
}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 barcodeImports 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和条码rotation分别检索页面方向和条码旋转。 此功能主要用于调试目的。
BarcodeResult.PageOrientation、BarcodeResult.Rotation、0、90、180和270度的条码。 如果条码的旋转值不在上述提到的范围内,IronBarcode将不返回任何值。 PageOrientation返回一个PageOrientation对象,其中包含Portrait或Landscape。文本和值属性之间的区别是什么?
当然,使用IronBarcode时,用户最想获取的主要属性是其value和text。 这两个属性通常可以互换使用,并返回相同的value。 除此之外,用户还可以使用BarcodeResult.ToString()方法实现相同的结果。 在使用专用应用程序或将条码数据导出为流时,这些属性提供了灵活的方式以您首选的格式访问条码内容。
下面的代码片段演示了:
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());
}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 barcodeImports 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进行迭代后,我们在控制台上输出获取Value和Text属性的结果,并调用BarcodeResult.ToString()方法以显示所有这些均返回相同的value。
总之,IronBarcode是一个完美的API,允许用户执行与条码相关的多个操作,不仅限于生成和解码条码。 支持各种输出数据格式,用户可以通过IronBarcode返回的BarcodeResult对象做更多事情。
常见问题
IronBarcode提供哪些可用的条形码输出格式?
IronBarcode提供多种输出格式,如BarcodeImage、BarcodeType、BinaryValue、坐标、尺寸、页码、方向、文本和值属性。这些格式满足.NET应用程序中各种数据处理需求。
如何读取并立即打印条形码的值和类型?
使用IronBarcode,您可以从图像中读取条形码,并用一行代码立即打印其值和类型。例如:var result = IronBarCode.BarcodeReader.Read("input.png"); Console.WriteLine($"Value: {result[0].Value}, Type: {result[0].BarcodeType}");
如何提取和保存IronBarcode的条形码图像?
一旦读取条形码,可以使用BarcodeImage属性访问该图像。然后可以根据需要使用IronBarcode的工具对其进行处理或保存。
在条形码处理中,BinaryValue输出有何作用?
BinaryValue允许用户检索条形码的字节数组,对于处理加密数据或需要字节级数据操作的系统集成特别有用。
如何确定图像中条形码的位置和尺寸?
IronBarcode提供了如Points、Height和Width等属性,用于确定条形码的位置和尺寸,使得需要精确空间数据的应用程序能够准确地处理。
在读取多页文档时,页码信息为何重要?
页码信息有助于识别每个条形码在多页文档中的位置,从而提高对包含多个条形码的文档的处理和调试效率。
IronBarcode如何处理条形码的旋转和页面方向?
IronBarcode提供Rotation和PageOrientation属性,以帮助您识别和正确处理可能被旋转或位于不同方向页面上的条形码。
IronBarcode的Text和Value属性有什么区别?
Text和Value属性通常返回相同的数据,表示条形码的内容。然而,它们提供了以首选格式访问条形码信息的灵活性。
什么类型的应用程序受益于使用IronBarcode的输出数据格式?
库存管理、文档处理和质量控制等应用领域极大地受益于IronBarcode多样的数据格式,因为它们提供广泛的条形码数据处理选项。
IronBarcode可以读取多页PDF文档中的条形码吗?
是的,IronBarcode可以使用如BarcodeReader.ReadPdf()的方法读取多页PDF文档中的条形码,还提供有关每个条形码所在页码的信息。