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

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

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

IronBarcode 提供多种 BarCode 读取输出格式,包括 BinaryValue、坐标、尺寸、页码、方向、文本及数值属性。 这些格式支持通过编程方式处理BarCode数据,以满足各种应用场景的需求。

IronBarcode 不仅能读取 BarCode 并在控制台打印数值,它还能提供更多功能。 它提供了多种输出格式,为用户处理读取结果铺平了道路。 这些格式包含BARCODE图像、BARCODE类型、textvalue 等属性。

用户可以在程序中进一步操作这些属性。 让我们来探讨如何使用这些属性,以及它们在哪些场景下能发挥作用。

快速入门:一行代码读取BarCode值和类型

此示例演示了如何使用 IronBarcode 从图像中读取 BARCODE——仅需一行代码即可加载,并立即打印 BARCODE 的值和类型。 非常适合快速入门。如需更全面的示例,请参阅《BarCode快速入门指南》。

  1. 使用 NuGet 包管理器安装 https://www.nuget.org/packages/BarCode

    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

有哪些可用的输出格式及其应用场景?

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

  • BarcodeImage
  • BarcodeType
  • BinaryValue
  • 坐标,Height & Width
  • PageNumber
  • BarcodePageOrientation
  • Text & Value

每个属性在BarCode处理工作流中都发挥着特定的作用。 无论是构建库存管理系统、文档处理管道还是质量控制应用程序,这些数据格式都能提供从各种来源读取BarCode所需的灵活性。

如何提取并保存BarCode图像?

一旦 IronBarcode 读取了图像,图像中发现的 BARCODE 就会以 BarcodeResult 类型存储在 BarcodeImage 属性中。 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")
$vbLabelText   $csharpLabel

上方的代码片段展示了此输出格式的一个使用场景。 具体而言,它能根据PDF文档中检测到的BarCode生成多页TIFF图像。 首先,我们扫描或检测样本 PDF 中的 BarCode。 然后,我们创建一个名为 AnyBitmap 的列表,用于存储来自 BarcodeImage 属性的信息。 最后,我们将使用此列表通过 CreateMultiFrameTiff 方法生成多页 TIFF 文件。 此技术在处理多页 GIF 和 TIFF 文件时尤为有用。

BarcodeImage 属性从 BarcodeResult 仅存储读取过程中发现的 BARCODE 图像,而非整个输入图像本身。

如何通过编程识别不同的BarCode类型?

此属性有助于确定输入图像或文档中包含何种类型的BarCode。 但需注意,图像中的 BarCode 类型必须是 IronBarcode 支持且可识别的。 如需了解 IronBarcode 支持的 BARCODE 类型,请参阅此文章。 此外,请查阅支持的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
$vbLabelText   $csharpLabel

从上面的代码片段中可以看出,我们通过在输入图像上调用 BarcodeReader.Read() 方法来执行 BarCode 读取。 这将返回一个 BarcodeResults 对象,该对象存储了通过读取图像中所有可用BARCODE所获取的 BarcodeResult。 接下来,我们遍历 BarcodeResults 对象以检索 BarcodeResult,并将 BARCODE valuetype 打印到控制台。 该方案可无缝处理各种 BARCODE 类型,包括 Code 39 BARCODE 等专用格式。

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

Using IronBarcode,用户可以通过访问 BarcodeResult 对象的 BinaryValue 属性,获取条形码 value 的字节数组。 这使得用户能够在程序内部进一步处理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
$vbLabelText   $csharpLabel

通过观察上面的代码片段,我们编写了一个简单的程序,该程序能将图像中的多个BarCode转换为独立的新二进制编码文件。 首先,我们扫描示例 PNG 图片中的 BARCODE。 检测到这些 BARCODE 后,我们会遍历它们,访问 BinaryValue 属性,并利用该属性创建新的二进制文件。 当您需要读取多个BarCode并分别处理其二进制数据时,此技术尤为有用。

如何获取BarCode的位置和尺寸?

用户可以访问的 BarcodeResult 对象的另一个属性是 BARCODE 的坐标,包括 Y1Y2,以及图像文件或文档中的 HeightWidth。 当用户需要获取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
$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() 方法读取图像文件中的 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
$vbLabelText   $csharpLabel

上方的代码片段展示了一个典型用例:用户需要程序返回多页PDF文档中BARCODE的value及其对应的页码。 该代码使用 BarcodeReader.ReadPdf() 方法读取多页 PDF 文档中的 BarCode,该方法会返回一个 BarcodeResults 对象,其中存储了文档中找到的每个 BarcodeResult。 我们使用循环遍历对象中的每个项,以获取BarCode的 value 以及发现BarCode的页码。 除此用例外,用户还可利用此属性进行调试,以确认文档中的所有BarCode是否均已被读取。

请注意此属性返回的值采用1为起始的计数方式,即第一页始终为1而非0

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

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

上方的代码片段已结合附带的示例 PDF 文件运行,以证明用户可以通过获取 pageOrientation 和 BarCode rotation,分别获取页面方向和 BarCode 旋转角度。 此功能主要用于调试目的。

请注意IronBarcode 仅能读取旋转角度为 180 以及 270 度的条码。 如果BarCode的旋转角度不在上述范围内,IronBarcode 将不会返回任何值。 PageOrientation 返回一个 PageOrientation 对象,该对象由 PortraitLandscape 组成。)}]

文本属性与值属性有何区别?

当然,用户在使用 IronBarcode 时最想获取的核心功能是其 valuetext。 这两个属性常被互换使用,且返回相同的结果 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
$vbLabelText   $csharpLabel

从上面的代码片段可以看出,用户只需几行代码,即可使用 IronBarcode 读取图像中的条形码。 遍历 BarcodeResults 方法返回的 BarcodeReader.Read() 后,我们将获取 ValueText 属性的结果,并调用 BarcodeResult.ToString() 方法,以展示所有这些操作均返回相同的 value

简而言之,IronBarcode 是一款完美的 API,可帮助用户执行多种 BarCode 相关操作,不仅限于 BarCode 的生成和解码。 由于支持多种输出数据格式,用户可以利用 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 等),从而可在应用程序中进行特定格式处理。

在项目中实现IronBarcode需要哪些编程技能?

了解C#编程的基础知识就足以在项目中实现IronBarcode,因为它提供了简单的方法和全面的文档来指导开发人员。

IronBarcode适合小项目和大型企业应用吗?

IronBarcode被设计为可扩展且多功能,适合需要强大条码解决方案的小项目和大型企业应用。

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

还在滚动吗?

想快速获得证据? PM > Install-Package BarCode
运行示例 观看您的字符串变成 BarCode。