如何在C#中输出条形码数据格式

如何使用 IronBarcode 在 C# 中输出数据格式。

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

IronBarcode 可从条码读取中提供多种输出格式,包括 BarcodeImage, BarcodeType, BinaryValue, 坐标、尺寸、页码、方向、文本和值属性。 这些格式能够以编程方式处理条形码数据,适用于各种用例。

IronBarcode 不只是简单地读取条形码并在控制台中打印数值,它还提供了更多的功能。 它提供了多种输出格式,为用户处理阅读结果铺平了道路。 这些格式包括条码图像、条码类型、BinaryValue、坐标、高度、宽度、页码、条码、页面方向、文本和值等属性。

用户可以在程序中进一步操作这些属性。 让我们来探讨一下如何使用这些属性以及在哪些用例中它们会有所帮助。

快速入门:在一行中读取条码值和类型

本示例展示了如何使用 IronBarcode 从图像中读取条形码--一行加载,然后立即打印条形码的值和类型。 非常适合快速入门。如需了解更全面的示例,请查看Barcode快速入门指南

Nuget Icon立即开始使用 NuGet 创建 PDF 文件:

  1. 使用 NuGet 包管理器安装 IronBarcode

    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

有哪些可用的输出格式及其使用案例?

<! -- 待办事项:在此处添加图片 --> <! -- 输出显示 IronPDF 中的输出格式和用例结果 --> -->。 <!--说明:显示代码执行输出或结果的截图 -->

BarcodeResult储存了多种有用的属性。 这些属性如下所列:

  • BarcodeImage
  • BarcodeType
  • BinaryValue
  • 坐标、高度和宽度
  • PageNumber
  • BarcodePageOrientation
  • 文本和值

每种属性在 BarCode 处理工作流程中都有特定用途。 无论是构建库存管理系统、文档处理流水线还是质量控制应用程序,这些数据格式都为读取各种来源的条形码提供了所需的灵活性。

如何提取和保存条形码图像?

IronBarcode 读取图像后,图像中的条形码将作为 AnyBitmap 类型的 BarcodeImage 属性存储在 BarcodeResult 中。 BarcodeImage属性存储找到的条码图像。 用户可以检索该对象以进一步处理图像,或将其保存为永久副本。 这样可以省去从图像中提取 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")
$vbLabelText   $csharpLabel

上面的代码片段说明了这种输出格式的一个用例。 具体来说,它可以从 PDF 文档中检测到的 BarCode 创建多页 TIFF 图像。 首先,我们扫描或检测样本PDF中的条码。 然后,我们创建了一个AnyBitmap列表,在其中存储了来自BarcodeImage属性的信息。 最后,我们使用该列表,使用 CreateMultiFrameTiff 方法生成多页 TIFF。 这种技术在处理 多页 GIF 和 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
$vbLabelText   $csharpLabel

从上面的代码片段中,我们通过在输入图像上调用BarcodeReader.Read()方法进行条码读取。 这返回一个BarcodeResults对象,存储读取图像中所有可用条码的BarcodeResult。 接下来,我们迭代BarcodeResults对象以检索BarcodeResult,并将条码值和类型打印到控制台。 这种方法可与各种条形码类型无缝衔接,包括 Code 39 条形码等专门格式。

何时应使用二进制值输出?

使用 IronBarcode,用户可以通过访问 BarcodeResult 对象中的 BinaryValue 属性来检索条形码值的字节数组。 这使用户可以在程序中进一步操作条码值。 二进制值输出在处理加密数据、以 BarCode 编码的文件附件或与需要字节级数据处理的系统集成时特别有用。

下面的代码片段演示了检索条码值为二进制数据的一个用例:

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

观察上面的代码片段,我们创建了一个简单的程序,将图像中多个条码转换为单独的新的二进制编码文件。 首先,我们扫描样本PNG图像中的条码。 检测到这些 BarCode 后,我们会遍历它们,访问 BinaryValue 属性,并使用它创建新的二进制文件。 当您需要读取多个条形码并单独处理其二进制数据时,这种技术尤其有价值。

如何访问 BarCode 位置和尺寸?

用户可以访问条码的坐标,包括X1Y1X2Y2以及图像文件或文档中的HeightWidth。 当用户需要检索有关 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
$vbLabelText   $csharpLabel
Three barcode samples (A, B, C) showing different encoded data with similar visual patterns
Three redacted content blocks with illegible text fragments

上面的代码片段编辑了图像文件中的多个 BarCode。为此,我们结合使用了 IronBarcode 和 IronDrawing 这两个库。 要获得BarcodeResult对象并从中提取属性,我们首先使用BarcodeReader.Read()方法读取图像文件中可用的条码。 同时,需要将输入的图像文件转换为 AnyBitmap 对象,以便对图像应用节录方法。 一旦我们得到了BarcodeResults对象,我们可以应用循环并遍历它,以获取每个可用条码的X1Y1WidthHeight,并将它们用在AnyBitmap.Redact()方法的CropRectangle属性中。

为什么页码对多页文档很重要?

用户可以检索发现 BarCode 的页码。 对于使用包含多个条形码的多页文档并需要知道在文档中发现的条形码位置以便进一步处理的用户来说,这是一项非常有用的功能。 在 从 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);
}
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
$vbLabelText   $csharpLabel

上面的代码片段演示了一个使用案例,用户需要程序返回在多页 PDF 文档中发现的 BarCode 的值及其各自的页码。 代码使用 BarcodeReader.ReadPdf() 方法读取多页 PDF 文档中的条形码,并返回 BarcodeResults 对象,该对象存储了在文档中发现的每个 BarcodeResult 。 我们应用循环并遍历对象中的每个项目,以检索条码的值和发现条码的页码。 除此用例之外,用户还可以使用此属性来调试文档中的所有 BarCode 是否都已读取。

请注意此属性返回的值基于 1 ,这意味着第一页始终为1不是 0。

如何检测 BarCode 旋转和页面方向?

使用 IronBarcode,用户可以检索条形码方向和发现条形码的页面方向信息。 要提取这两条信息,请访问 BarcodeResult 对象中的 RotationPageOrientation 属性。 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
$vbLabelText   $csharpLabel

上面的代码片段与所附的 PDF 输入示例一起运行,以证明用户可以分别通过获取 BarcodeResult.PageOrientationBarcodeResult.Rotation 的值来检索页面方向和条形码旋转。 此功能主要用于调试目的。

IronBarcode 只能读取旋转角度为0180 270 90条形码。 如果条码的旋转值不在上述提到的范围内,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());
}
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
$vbLabelText   $csharpLabel

从上面的代码片段来看,用户只需要几行代码就可以使用 IronBarcode 读取图像中的条形码。 在遍历 BarcodeReader.Read() 方法返回的 BarcodeResults 之后,我们向控制台输出获取 ValueText 属性以及调用 BarcodeResult.ToString() 方法的结果,以显示所有这些返回值相同。

总之,IronBarcode是一个完美的API,允许用户执行与条码相关的多个操作,不仅限于编写和解码条码。 IronBarcode 支持多种输出数据格式,用户可以使用 IronBarcode 返回的 BarcodeResult 对象进行更多操作。

常见问题解答

C# BarCode 阅读支持哪些输出格式?

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 对象访问,以实现精确的条形码位置跟踪。

文本属性和值属性有什么区别?

在 IronBarcode 中,Text 和 Value 属性都包含条码的数据内容。这些属性是 BarcodeResult 对象的一部分,可交替使用以检索解码后的条形码信息。

我能否确定条形码是在哪一页上找到的?

是的,IronBarcode 在 BarcodeResult 对象中包含 PageNumber 属性,允许您准确识别多页文档或 PDF 中的哪一页包含每个检测到的条码。

如何识别检测到的 BarCode 类型?

IronBarcode 的 BarcodeResult 对象中的 BarcodeType 属性可识别检测到的特定条码格式(如 QR 码、Code 128 等),从而可在应用程序中进行特定格式处理。

Hairil Hasyimi Bin Omar
软件工程师
如所有伟大的工程师一般,Hairil 是个热心的学习者。他正在提高对 C#、Python 和 Java 的知识,并利用这些知识为 Iron Software 团队成员增值。Hairil 从马来西亚的玛拉工业大学加入 Iron Software 团队,获得化学与工艺工程学士学位。
准备开始了吗?
Nuget 下载 2,070,733 | 版本: 2026.2 刚刚发布