.NET 的 2D 条形码阅读器
引入 2D Barcode Reader 为 .NET
2D Barcode Reader for .NET 是一款插件,允许开发人员从图像中阅读2D barcode. 本指南提供了可用的功能的概述,并解释了如何使用代码示例进行常见任务。
支持的条形码类型
.NET 的 2D 条形码阅读器支持以下类型:
- QR代码
- 数据矩阵
- PDF417
- 阿塞拜疆
阅读图像中的条形码
To read a barcode from an image, you can use the BarCodeReader
class. Here is an example of how to do this in C#:
using (BarCodeReader reader = new BarCodeReader("image.png"))
{
foreach (BarCodeResult result in reader.ReadBarCodes())
{
Console.WriteLine("Code Text: " + result.CodeText);
Console.WriteLine("Symbology: " + result.CodeType);
}
}
定制阅读过程
您可以通过设置各种选项来自定义阅读过程,如图像质量等。
using (BarCodeReader reader = new BarCodeReader("image.png"))
{
reader.QualitySettings = QualitySettings.HighQuality;
foreach (BarCodeResult result in reader.ReadBarCodes())
{
Console.WriteLine("Code Text: " + result.CodeText);
Console.WriteLine("Symbology: " + result.CodeType);
}
}
处理错误和例外
.NET 的 2D 条形码阅读器在阅读过程中出现错误时会排除例外,您可以使用尝试捕获区块处理这些例子。
try
{
using (BarCodeReader reader = new BarCodeReader("image.png"))
{
foreach (BarCodeResult result in reader.ReadBarCodes())
{
Console.WriteLine("Code Text: " + result.CodeText);
Console.WriteLine("Symbology: " + result.CodeType);
}
}
}
catch (BarCodeRecognitionException ex)
{
Console.WriteLine("Error: " + ex.Message);
}
最佳实践与性能优化
To optimize performance, it is recommended to use high-quality images and to set the QualitySettings
property to HighPerformance
此外,您可以通过使用多威胁或平行处理来提高性能,以下是如何在C#中做到这一点的例子:
BarCodeReader.ProcessorSettings.UseAllCores = true;
Parallel.ForEach(Files.GetFiles("images"), file =>
{
using (BarCodeReader reader = new BarCodeReader(file))
{
reader.QualitySettings = QualitySettings.HighPerformance;
foreach (BarCodeResult result in reader.ReadBarCodes())
{
Console.WriteLine("Code Text: " + result.CodeText);
Console.WriteLine("Symbology: " + result.CodeType);
}
}
});