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

How to Output Data Formats

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

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

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

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

此示例展示了如何轻松地使用IronBarcode从图像中读取条码——只需一行代码即可加载,然后立即打印条码的值和类型。 非常适合快速入门。

Nuget IconGet started making PDFs with NuGet now:

  1. Install IronBarcode with NuGet Package Manager

    PM > Install-Package BarCode

  2. Copy and run this code snippet.

    var result = IronBarCode.BarcodeReader.Read("input.png");
    Console.WriteLine($"Value: {result[0].Value}, Type: {result[0].BarcodeType}");
  3. Deploy to test on your live environment

    Start using IronBarcode in your project today with a free trial
    arrow pointer
class="hsg-featured-snippet">

最小工作流(5个步骤)

  1. 下载用于读取条码的C#库
  2. 准备用于条码检测的PDF和图像
  3. 访问检测到的条码类型和图像
  4. 检索条码的x和y坐标以及其高度和宽度
  5. 读取条码的文本和值

输出格式和使用案例

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

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

条码图像

一旦IronBarcode在图像上进行读取过程,图像中找到的条码将存储在BarcodeResult中作为BarcodeImage属性,其类型为AnyBitmapBarcodeImage属性存储找到的条码图像。 用户可以检索此对象以进一步处理图像或将其保存为永久副本。 这通过消除编写额外代码来从图像中提取条码图像的需要,提高了效率和使用便利性。

让我们查看下面的代码片段,演示此输出格式的一个可能使用案例:

: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文档中检测到的条码创建多页TIFF图像。 首先,我们扫描或检测样本PDF中的条码。 然后,我们创建了一个AnyBitmap列表,在其中存储了来自BarcodeImage属性的信息。 最后,我们利用此列表通过CreateMultiFrameTiff方法生成多页TIFF。

请注意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,并将条码值和类型打印到控制台。

二进制值

使用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
$vbLabelText   $csharpLabel

观察上面的代码片段,我们创建了一个简单的程序,将图像中多个条码转换为单独的新的二进制编码文件。 首先,我们扫描样本PNG图像中的条码。 一旦检测到这些条码,我们就会迭代它们,访问BinaryValue属性,然后使用它创建新的二进制文件。

条码坐标、高度和宽度

用户可以访问条码的坐标,包括X1Y1X2Y2以及图像文件或文档中的HeightWidth。 这些属性在用户需要检索条码的位置和尺寸信息时非常有用。

让我们用一个图示来突出显示条码的位置和尺寸。

: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
class="competitors-section__wrapper-even-1">
Sample input before redaction
Redacted image

上面的代码片段用于编辑图像文件中找到的多个条码。为了实现这一点,我们结合使用了两个库,IronBarcode和IronDrawing。 要获得BarcodeResult对象并从中提取属性,我们首先使用BarcodeReader.Read()方法读取图像文件中可用的条码。 同时,需要将输入图像文件转换为AnyBitmap对象,以便对图像应用编辑方法。 一旦我们得到了BarcodeResults对象,我们可以应用循环并遍历它,以获取每个可用条码的X1Y1WidthHeight,并将它们用在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
$vbLabelText   $csharpLabel

上面的简单代码片段演示了一个使用场景,用户需要程序返回多页PDF文档中找到的条码的值及其相应的页码。 上面的代码片段使用BarcodeReader.ReadPdf()方法来读取多页PDF文档内的条码,这将返回储存文档中找到的每个BarcodeResultBarcodeResults对象。 我们应用循环并遍历对象中的每个项目,以检索条码的值和发现条码的页码。 除了这个用例,用户还可以使用此属性来调试文档中的所有条码是否能够被读取。

[{i:(此属性返回的值是1-Based,这意味着首页始终是1不是零)]

条码旋转和页面方向

使用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的值,分别检索页面方向和条码旋转。 此功能主要用来用于调试目的。

[{i:(IronBarcode只能读取旋转为090180270度的条码。 如果条码的旋转值不在上述提到的范围内,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后,我们将在控制台上输出获取值和文本属性的结果,以及调用BarcodeResult.ToString()方法以显示所有这些返回了相同的值。

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

常见问题解答

在 .NET C# 中有哪些可用于条码处理的输出数据格式?

IronBarcode 在 .NET C# 中为条码处理提供了多种输出数据格式,包括条码图像、类型、`BinaryValue`、坐标、高度、宽度、页码、方向、文本和值。这些格式促进了多样的条码相关操作。

如何使用 C# 捕获并存储条码图像?

您可以在 IronBarcode 中使用 `BarcodeImage` 属性捕获并存储条码图像,这有助于完成从检测到的条码创建多页 TIFF 文件等任务。

如何在图像中识别条码的类型?

IronBarcode 中的 `BarcodeType` 属性帮助您识别图像中存在的条码类型,这有助于有效处理支持的条码类型。

如何将条码数据转换为字节数组?

通过访问 IronBarcode 中的 `BinaryValue` 属性,您可以将条码数据转换为字节数组,该数组可用于进一步的数据操作或存储。

如何获取图像中条码的坐标和尺寸?

IronBarcode 中的 `BarcodeResult` 对象提供了坐标、高度和宽度属性,让您可以获取关于图像中条码位置和尺寸的详细信息。

如何使用条码数据管理多页文档?

IronBarcode 的 `PageNumber` 和 `PageOrientation` 属性帮助您通过识别每个条码的位置及其方向来管理多页文档,从而提高文档处理效率。

在 C# 中,条码的 `text` 和 `value` 属性有什么区别?

在 IronBarcode 中,条码的 `text` 和 `value` 属性返回相同的输出。您可以使用 `BarcodeResult.ToString()` 方法实现类似的结果。

IronBarcode 在条码旋转检测方面有没有限制?

是的,IronBarcode 可以读取旋转 0、90、180 和 270 度的条码。它不支持其他旋转角度的条码检测。

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