条码控件Aspose.BarCode入门教程(8):C#从图像中读取条形码

Aspose.BarCode for .NET 是一个功能强大的API,可以从任意角度生成和识别多种图像类型的一维和二维条形码。开发人员可以轻松添加条形码生成和识别功能,以及在.NET应用程序中将生成的条形码导出为高质量的图像格式。

Aspose API支持流行文件格式处理,并允许将各类文档导出或转换为固定布局文件格式和最常用的图像/多媒体格式。

Aspose.BarCode 最新下载(qun:767755948)icon-default.png?t=N3I4https://www.evget.com/product/576/download

您是否正在寻找一种以编程方式从图像中读取条形码或 QR 码的方法?如果您是开发人员,并且需要创建自己的条码阅读器应用程序?你来对地方了。条形码对于准确跟踪库存和产品从制造到销售点的交付至关重要。我们可以在.NET应用程序中轻松检测、识别和读取不同类型的条形码和二维码。在本文中,我们将通过几个简单的步骤向您展示如何使用 C# 从图像中读取条形码。您可以将本文用作开发条形码阅读器或扫描器应用程序的分步指南。

首先,我们将了解 C# 条码阅读器 API,以读取输入图像中可用的条码。接下来,我们将介绍如何从图像中检测、识别和提取条形码数据的步骤。您将找到详细的步骤和代码片段。最后,我们将提供有用的链接以进一步增强功能。让我们开始吧!

用于从图像读取条形码的 C# API

为了从图像中读取条形码,我们将使用Aspose.BarCode for .NET API。API 允许生成、扫描和读取范围广泛的条码符号。它支持以JPEG、TIFF、PNG、BMP和GIF格式呈现条码图像。

API 提供了BarCodeReader类,可以从给定的图像中识别 60 多种不同的条形码类型。检测条形码的第一步是指定带有条形码的图像的来源。这可以是文件、位图对象或流。然后需要在DecodeType参数中指定目标符号。我们可以通过指定DecodeType.AllSupportedTypes来查看所有不同类型的支持符号。此类的ReadBarCodes ()方法返回一个已识别条码数组。API的BarCodeResult类存储识别出的条码数据,如条码类型、条码文本、区域等参数。

API 还允许指定条形码阅读器应读取的图像区域。这可以使用 .NET Rectangle 对象来完成,并且允许避免在默认情况下不包含条码的图像区域中查找条码的需要。

请下载 API 的 DLL或使用NuGet安装它。

PM> Install-Package Aspose.BarCode

使用 C# 从图像中读取条形码

我们可以按照以下步骤轻松地从图像中读取条形码:

  1. 首先,使用BarCodeReader类加载图像。
  2. 调用readBarCodes()方法,在BarCodeResult类对象中获取识别结果。
  3. 最后,遍历结果并显示条形码的类型和文本。

以下代码示例显示了如何在 C# 中从图像中读取条形码

// This code example demonstrates how to read barcode from an image file.
// Initialize barcode reader
BarCodeReader reader = new BarCodeReader("C:\\Files\\BarCode\\Sample.png");

// Read barcode and show results
foreach(BarCodeResult result in reader.ReadBarCodes())
{
Console.Out.WriteLine("CodeText: " + result.CodeText);
Console.Out.WriteLine("Symbology type: " + result.CodeType);
}

C#从位图中读取条码

我们可以按照以下步骤轻松地从图像中读取条形码:

  1. 首先,使用Bitmap类加载图像。
  2. 接下来,使用Bitmap对象创建BarCodeReader类的实例。
  3. 调用ReadBarCodes()方法,在BarCodeResult类对象中获取识别结果。
  4. 最后,遍历结果并显示条形码的类型和文本。

以下代码示例显示了如何在 C# 中从位图中读取条形码

// This code example demonstrates how to read barcode from bitmap.
// Load image in Bitmap
Bitmap bmp = new Bitmap("C:\\Files\\BarCode\\Code128.jpg");

// Initialize Barcode reader
BarCodeReader reader = new BarCodeReader(bmp);

// Read all barcodes in the provided area
foreach (BarCodeResult result in reader.ReadBarCodes())
{
Console.Out.WriteLine("CodeText: " + result.CodeText);
Console.Out.WriteLine("Symbology type: " + result.CodeType);
}

在 C# 中使用 Stream 从图像中读取条码

我们还可以使用文件流加载条码图像并按照以下步骤读取条码:

  1. 首先,使用FileStream类加载图像。
  2. 接下来,使用流对象创建BarCodeReader类的实例。
  3. 调用ReadBarCodes()方法,在BarCodeResult类对象中获取识别结果。
  4. 最后,遍历结果并显示条形码的类型和文本。

以下代码示例展示了如何在 C# 中使用 Stream 从图像中读取条形码

// This code example demonstrates how to read barcode from an image using file stream.
// Load image
Stream stream = new FileStream("C:\\Files\\BarCode\\MultipleBarcodes.jpeg", FileMode.Open, FileAccess.Read);

// Initialize Barcode reader
BarCodeReader reader = new BarCodeReader(stream);

// Read all barcodes in the provided area
foreach (BarCodeResult result in reader.ReadBarCodes())
{
Console.Out.WriteLine("CodeText: " + result.CodeText);
Console.Out.WriteLine("Symbology type: " + result.CodeType);
}

C#从图像中读取特定类型的条码

建议选择将考虑进行识别的目标条码符号体系,以最大程度地减少完成识别所需的时间并避免尝试识别过时的条码。

我们可以按照以下步骤指定目标条码类型并从图像中读取条码:

  1. 首先,使用BarCodeReader类加载图像。
  2. 接下来,设置条形码解码类型,例如Code39Standard
  3. 之后,使用ReadBarCodes()方法在BarCodeResult类对象中获取识别结果。
  4. 最后,遍历结果并显示条形码的类型和文本。

以下代码示例展示了如何使用 C# 从图像中读取特定类型的条形码

// This code example demonstrates how to read barcode of a specific decode type from an image.
// Initialize barcode reader
BarCodeReader reader = new BarCodeReader("C:\\Files\\BarCode\\Code39Standard.jpg", DecodeType.Code39Standard);

// Read barcode of type Code39Extended
foreach (BarCodeResult result in reader.ReadBarCodes())
{
Console.Out.WriteLine("CodeText: " + result.CodeText);
Console.Out.WriteLine("Symbology type: " + result.CodeType);
}

C#读取图片中多种类型的条码

我们还可以按照以下步骤指定多种条形码类型:

  1. 首先,使用BarCodeReader类加载图像。
  2. 接下来,使用SetBarCodeReadType()方法设置条码解码类型。
  3. 之后,使用ReadBarCodes()方法在BarCodeResult类对象中获取识别结果。
  4. 最后,遍历结果并显示条形码的类型和文本。

以下代码示例展示了如何使用 C# 从图像中读取多种类型的条形码

// This code example demonstrates how to read barcode of multiple decode types from an image.
// Initialize barcode reader
BarCodeReader reader = new BarCodeReader("C:\\Files\\BarCode\\MultipleBarcodes.png");
reader.SetBarCodeReadType(DecodeType.DataMatrix, DecodeType.QR, DecodeType.Code39Extended);

// Read barcodes
foreach (BarCodeResult result in reader.ReadBarCodes())
{
Console.Out.WriteLine("CodeText: " + result.CodeText);
Console.Out.WriteLine("Symbology type: " + result.CodeType);
Console.Out.WriteLine("-------------------------");
}

我们还可以在 BarCodeReader 类的构造函数中指定多种解码类型,如下所示:

BarCodeReader reader = new BarCodeReader("C:\\Files\\BarCode\\Code39Standard.jpg", DecodeType.DataMatrix, DecodeType.QR, DecodeType.Code39Extended);

在 C# 中从图像中读取预定义的条码类型集

我们可以读取DecodeTypes类中定义的一组预定义的符号体系以进行识别。我们可以设置以下任何预定义集:

  • AllSupportedTypes - 所有支持的条形码类型
  • Types1D - 所有支持的一维符号
  • Types2D - 所有支持的二维符号
  • PostalTypes - 所有受支持的邮政符号体系,主要由邮政服务使用
  • MostCommonTypes - 定义了一组最广泛使用的条码标准

我们可以按照以下步骤指定预定义集:

  1. 首先,首先,使用BarCodeReader类加载图像。
  2. 接下来,在BarCodeReader构造函数或SetBarCodeReadType方法中设置条码解码类型,例如DecodeType.Types2D
  3. 之后,使用ReadBarCodes()方法在BarCodeResult类对象中获取识别结果。
  4. 最后,遍历结果并显示条形码的类型和文本。

以下代码示例显示了如何使用 C# 中预定义的一组符号来读取条形码

// This code example demonstrates how to read a barcode using predefined set of symbologies.
// Initialize barcode reader
BarCodeReader reader = new BarCodeReader("C:\\Files\\BarCode\\MultipleBarcodes.png", DecodeType.Types1D);

// Read barcode and show results
foreach (BarCodeResult result in reader.ReadBarCodes())
{
Console.Out.WriteLine("CodeText: " + result.CodeText);
Console.Out.WriteLine("Symbology type: " + result.CodeType);
Console.Out.WriteLine("-------------------------");
}

在 C# 中从图像中读取多个条形码

我们还可以按照以下步骤从图像中读取所有可用的条形码:

  1. 首先,使用BarCodeReader类加载图像。
  2. 接下来,将条形码解码类型设置为ALL_SUPPORTED_TYPES
  3. 之后,使用ReadBarCodes()方法在BarCodeResult类对象中获取识别结果。
  4. 最后,遍历结果并显示条形码的类型和文本。

以下代码示例显示了如何使用 C# 从图像中读取多个条形码

// This code example demonstrates how to read barcode multiple barcodes from an image.
// Initialize barcode reader
BarCodeReader reader = new BarCodeReader("C:\\Files\\BarCode\\MultipleBarcodes.png", DecodeType.AllSupportedTypes);

// Read all types of barcode available on the input image
foreach (BarCodeResult result in reader.ReadBarCodes())
{
Console.Out.WriteLine("CodeText: " + result.CodeText);
Console.Out.WriteLine("Symbology type: " + result.CodeType);
Console.Out.WriteLine("-------------------------");
}

使用 C# 获取条形码的 X 和 Y 坐标

我们可以按照以下步骤从图像中读取检测到的条形码的 X 和 Y 坐标:

  1. 首先,使用BarCodeReader类加载图像。
  2. 接下来,使用ReadBarCodes()方法在BarCodeResult类对象中获取识别结果。
  3. 然后,遍历结果并检查Region是否不为空。
  4. 之后,使用Points获取限制条形码区域的 Points 数组。
  5. 最后,显示条形码的 X 和 Y 点。

以下代码示例显示如何使用 C# 从图像中获取条形码的 X 和 Y 坐标点

// This code example demonstrates how to read X & Y region point of barcodes from an image.
// Initialize barcode reader
BarCodeReader reader = new BarCodeReader("C:\\Files\\BarCode\\Code39Standard.jpg", DecodeType.AllSupportedTypes);

// Read barcode
foreach (BarCodeResult result in reader.ReadBarCodes())
{
if (result.Region != null)
{
// Display x and y coordinates of all the barcodes detected
Point[] point = result.Region.Points;
Console.Out.WriteLine("Top left coordinates: X = " + point[0].X + ", Y = " + point[0].Y);
Console.Out.WriteLine("Bottom left coordinates: X = " + point[1].X + ", Y = " + point[1].Y);
Console.Out.WriteLine("Bottom right coordinates: X = " + point[2].X + ", Y = " + point[2].Y);
Console.Out.WriteLine("Top right coordinates: X = " + point[3].X + ", Y = " + point[3].Y);
}
}

在 C# 中从图像的特定区域读取条形码

我们可以按照以下步骤从特定区域或图像区域读取条形码:

  1. 首先,使用位图方法读取图像文件的缓冲区数据。
  2. 接下来,创建BarCodeReader类的一个实例。它以图像、矩形区域和解码类型作为参数。
  3. 之后,使用ReadBarCodes()方法在BarCodeResult类对象中获取识别结果。
  4. 最后,遍历结果并显示条形码的类型和文本。

以下代码示例展示了如何使用 C# 从图像的特定区域读取条形码

// This code example demonstrates how to read barcode from specific region of an image.
// Load image
Bitmap img = new Bitmap("C:\\Files\\BarCode\\MultipleBarcodes.jpeg");

// Create an instance of BarCodeReader class
// and specify an area to look for the barcode
BarCodeReader reader = new BarCodeReader(img, new Rectangle(0, 0, 400, 200));

// Read all barcodes in the provided area
foreach (BarCodeResult result in reader.ReadBarCodes())
{
Console.Out.WriteLine("CodeText: " + result.CodeText);
Console.Out.WriteLine("Symbology type: " + result.CodeType);
}

在 C# 中从图像的多个区域读取条形码

我们还可以按照以下步骤从图像的多个区域读取条形码:

  1. 首先,使用Bitmap类加载图像。
  2. 接下来,使用 Rectangle 类定义矩形。
  3. 然后,创建BarCodeReader类的实例。
  4. 同时,使用图像对象和矩形作为参数调用SetBarCodeImage() 。
  5. 或者,使用SetBarCodeReadType()方法设置条码读取类型。
  6. 之后,使用ReadBarCodes()方法在BarCodeResult类对象中获取识别结果。
  7. 最后,遍历结果并显示条形码的类型和文本。

以下代码示例显示如何使用 C# 从图像的多个区域读取条形码

// This code example demonstrates how to read barcode from specific region of an image.
// Load image in Bitmap
Bitmap bmp = new Bitmap("C:\\Files\\BarCode\\MultipleBarcodes.png");

// Rectangle of a 2D barcode in the source image
Rectangle rect2D = new Rectangle(0, 0, 400, 200);

// Rectangle of Code128 barcode in the source image
Rectangle rectCode128 = new Rectangle(450, 100, 600, 180);

// Initialize Barcode reader
BarCodeReader reader = new BarCodeReader();
reader.SetBarCodeImage(bmp, new Rectangle[] { rect2D, rectCode128 });
reader.SetBarCodeReadType(DecodeType.AllSupportedTypes);

// Read all barcodes in the provided area
foreach (BarCodeResult result in reader.ReadBarCodes())
{
Console.Out.WriteLine("CodeText: " + result.CodeText);
Console.Out.WriteLine("Symbology type: " + result.CodeType);
Console.Out.WriteLine("-------------------------");
}

以上便是如何在C#从图像中读取条形码,希望能帮到您,除此之外,你有其他方面的需求,也欢迎和我们互动,或这下体验我们更多的产品~

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值