如何使用C#从图像中读取条形码
IronBarcode可以通过使用BarcodeReader.Read()在C#中用一行代码从图像中读取条码,支持多种图像格式,包括PNG、JPEG、GIF、BMP、TIFF和SVG,并具有可定制的选项以提高性能和准确性。
IronBarcode 的一个关键特性是它能够开箱即用地读取多种图像格式的条形码。 IronBarcode目前支持以下图像格式:
- 可缩放矢量图形 (SVG)
- 联合图像专家组 (JPEG)
- 便携式网络图形 (PNG)
- 图形交换格式 (GIF)
- 标签图像文件格式 (TIFF)
- 位图图像文件(BMP)
借助我们的开源库IronDrawing ,这一切才得以实现。 有关支持的条码格式的完整列表,包括1D和2D类型,请查看我们的综合文档。
快速入门:几秒钟内从图像中读取BarCode只需简单调用IronBarCode.BarcodeReader.Read(),您就可以直接从PNG、JPEG、GIF、BMP和TIFF等图像文件格式中提取条码数据。 立即开始 - 无需复杂设置,只需立即获得结果。
-
1使用 IronBarcode 安装 NuGet Package Manager
-
2复制并运行这段代码。
var results = IronBarCode.BarcodeReader.Read("path/to/image.png");Dim results = IronBarCode.BarcodeReader.Read("path/to/image.png")Dim results = IronBarCode.BarcodeReader.Read("path/to/image.png") -
3部署到您的生产环境中进行测试
今天就用免费试用在您的项目中开始使用IronBarcode
最小工作流程(5 个步骤)

- 下载C#库以从图像中读取条码
- 使用
Read方法从各种图像格式中读取条形码值 - 使用BarcodeReaderOptions类配置读取设置
- 使用CropArea属性指定图像中的条形码区域
- 通过设置ExpectBarcodeTypes属性来指定要读取的条形码类型。
如何直接从图像读取条码?
下面介绍如何使用 IronBarcode 读取条码。 有关在C# / .NET中读取条码的全面教程,包括PDF处理和批量操作的高级技术,请访问我们的详细指南:
using IronBarCode;
using System;
var myBarcode = BarcodeReader.Read(@"image_file_path.jpg"); //image file path
foreach (var item in myBarcode)
{
Console.WriteLine(item.ToString());
}using IronBarCode;
using System;
var myBarcode = BarcodeReader.Read(@"image_file_path.jpg"); //image file path
foreach (var item in myBarcode)
{
Console.WriteLine(item.ToString());
}Imports IronBarCode
Imports System
Private myBarcode = BarcodeReader.Read("image_file_path.jpg") 'image file path
For Each item In myBarcode
Console.WriteLine(item.ToString())
Next itemImports IronBarCode
Imports System
Private myBarcode = BarcodeReader.Read("image_file_path.jpg") 'image file path
For Each item In myBarcode
Console.WriteLine(item.ToString())
Next item

要使用 IronBarcode,首先需要做的是通过 Microsoft Visual Studio NuGet 包管理器将 IronBarcode 库安装到您的项目中,如下图所示。 这将允许您访问IronBarcode的BarcodeReader.Read()方法以直接读取条码图像。
使用 IronBarcode 时,只需调用BarcodeReader.Read(),并将项目中图像文件的文件名字符串或文件路径字符串作为参数,即可读取图像。 最佳实践是在方法中指定文件路径时使用带有“@”前缀的逐字字符串字面量,以避免在文件路径字符串中添加多个转义字符“\”。
在BarcodeReader.Read()方法调用末尾附加Values()方法,即可获取以System.String[]对象形式返回的条形码值。
要将结果输出到控制台,您可以使用foreach循环迭代存储在string[]数组中的值,并在循环块中使用迭代器变量作为参数调用Console.WriteLine()方法。
IronBarcode能够读取一维条码格式(Codabar、Code128、Code39、Code93、EAN13、EAN8、ITF、MSI、UPCA、UPCE)以及各种图像格式的二维条码格式(Aztec、DataMatrix、QRCode)。
如何配置条码读取器选项以获得更好的性能?
条形码读取速度太慢? 图片中的条形码是否太小,导致 IronBarcode 无法读取? 只想读取图像的特定区域? 想从包含多种条形码的图像中只读取特定类型的条形码吗? 想要提高整体读取性能吗?
BarcodeReaderOptions允许用户调整或修改条码读取器的行为以解决所有这些问题。 有关设置条码读取器选项的详细示例,请查看我们的指南。以下部分逐一讨论BarcodeReaderOptions中所有可调整的属性。
如何指定图像中要读取的区域?
CropArea是BarcodeReaderOptions中可用的IronSoftware.Drawing.CropRectangle类型的属性,允许用户指定IronBarcode应读取的图像区域。 这有助于提高读取性能,因为条形码读取器无需扫描整个图像来查找条形码,同时由于读取区域已指定,读取精度也得以提高。 了解更多关于如何指定裁剪区域以获得最佳性能。
要设置CropArea属性,只需实例化一个新的Rectangle类型对象,并将矩形的坐标、宽度和长度作为参数指定。 接受的测量单位为像素 (px)。
// Example of setting CropArea
var cropArea = new IronSoftware.Drawing.Rectangle(x: 100, y: 100, width: 300, height: 300);
var options = new BarcodeReaderOptions()
{
CropArea = cropArea
};// Example of setting CropArea
var cropArea = new IronSoftware.Drawing.Rectangle(x: 100, y: 100, width: 300, height: 300);
var options = new BarcodeReaderOptions()
{
CropArea = cropArea
};Imports IronSoftware.Drawing
' Example of setting CropArea
Dim cropArea As New Rectangle(x:=100, y:=100, width:=300, height:=300)
Dim options As New BarcodeReaderOptions() With {
.CropArea = cropArea
}Imports IronSoftware.Drawing
' Example of setting CropArea
Dim cropArea As New Rectangle(x:=100, y:=100, width:=300, height:=300)
Dim options As New BarcodeReaderOptions() With {
.CropArea = cropArea
}我应该为更快的读取指定哪些条码类型?
默认情况下,IronBarcode 会扫描图像中所有支持的条形码类型。 不过,如果用户知道有哪些类型的条形码或希望在图像中读取哪些类型的条形码,那么将此属性设置为只读取特定类型的条形码将大大提高读取性能和准确性,因为条形码读取器无需遍历条形码集合来解释和读取条形码。
要使用此属性,只需将ExpectBarcodeTypes设置为BarcodeEncoding枚举的一个字段。 以下是IronBarcode支持的每种条形码类型的示例。
// Example: Expect only QR codes and Code128
var options = new BarcodeReaderOptions()
{
ExpectBarcodeTypes = BarcodeEncoding.QRCode | BarcodeEncoding.Code128
};// Example: Expect only QR codes and Code128
var options = new BarcodeReaderOptions()
{
ExpectBarcodeTypes = BarcodeEncoding.QRCode | BarcodeEncoding.Code128
};' Example: Expect only QR codes and Code128
Dim options As New BarcodeReaderOptions() With {
.ExpectBarcodeTypes = BarcodeEncoding.QRCode Or BarcodeEncoding.Code128
}' Example: Expect only QR codes and Code128
Dim options As New BarcodeReaderOptions() With {
.ExpectBarcodeTypes = BarcodeEncoding.QRCode Or BarcodeEncoding.Code128
}我应该在何时禁用多条码读取?
IronBarcode 默认会扫描图像中所有可用的条形码,包括扫描整个图像文件并将读取的条形码值添加到字符串数组中。 但是,如果用户不希望读取图像文件中的多个条形码,用户可以将此属性设置为false ,这样条形码读取器在找到条形码值后就会停止扫描。 这将再次提高 IronBarcode 的性能和读取速度。 有关读取多个条码的更多信息,请查看我们的专用指南。
图像滤镜如何提高条码识别率?
可以在BarcodeReaderOptions中添加的属性之一是图像滤镜的集合。 图像滤镜对于预处理输入到 IronBarcode 的原始图像非常重要。 要在BarcodeReaderOptions中应用图像滤镜,用户必须首先初始化并指定要使用的ImageFilter集合。 有关图像校正技术的全面指导,包括滤镜应用,请访问我们的教程。
如何优化线程以获得更好的性能?
IronBarcode 允许用户启用和调整并行线程执行的数量,从而提高处理速度和效率。 并行线程是指在不同的处理器核心上同时执行多个线程。 IronBarcode中MaxParallelThreads属性的默认值为4。用户可以根据其机器的能力和资源数量进行调整。
我应该启用多线程处理吗?
该特性使 IronBarcode 能够并行读取多个图像。 #Multithreaded的默认值为true,因此多个线程将自动管理,以提高批量条码读取任务的性能。
为什么我应该去除误读的条码?
此属性可消除所有误报的条形码读取结果。 假阳性条形码读取是指对条形码值的错误读取,但却被识别为有效。 这可能是由于排序过程中的错误,或是条形码标签或准备过程中的错误造成的。 因此,将RemoveFalsePositive设置为true将去除误读的条码,从而提高条码读取的准确性。 但是,如果用户为了追求性能而牺牲准确性,则将此属性设置为false会有所帮助。此属性的默认值为true 。
MinScanLines 如何影响检测准确率?
MinScanLines设置将条码结果视为有效所需的最少扫描线数量,这些扫描线必须得出相同结果。 默认值为 2。增加此值可减少误报,但可能会导致无法检测到条纹较细或质量较低的 BARCODE。 将其调低至 1 可提高对难以识别的 BarCode 的灵敏度,但可能会引入噪点。
不同用例有哪些扫描模式可用?
定义 IronBarcode 如何扫描和检测图像中的条形码。
Auto:使用自动图像预处理和配置最优读码选项读取条码。 为获得最佳效果和性能,建议采用此方法。OnlyDetectionModel:扫描图像中的条码,并将其位置作为IronSoftware.Drawing.PointF数组返回。 此模式无法读取检测到的条形码; 它只返回每个条形码的位置。MachineLearningScan:使用机器学习检测扫描图像中的条码并读取它们。OnlyBasicScan:读取条码时不进行机器学习检测、自动图像预处理或读码选项配置。 此选项可以单独与IronBarCode.Slim一起使用。
读取速度设置如何影响准确性?
顾名思义,Speed属性使用户可以进一步优化IronBarcode读取器的性能。 与RemoveFalsePositive属性类似,调整此属性可以提高性能,但会降低准确性。 有关读取速度选项的详细信息,包括性能基准,请参阅我们的详细指南。它接受ReadingSpeed枚举,具有以下4个级别:
Faster:启用最快速的条码读取,但降低准确性。 该过程跳过了图像预处理,经常导致条形码结果为空。 仅当输入图像清晰锐利时才使用此设置。Balanced:此设置推荐用于Speed属性。 它通过尝试对图像进行轻微处理,使条形码区域更加清晰,从而在准确性和读取性能之间取得平衡,使条形码读取器能够检测到它。 大多数情况下,这种设置足以让 IronBarcode 读取条形码图像并产生准确的输出。Detailed:对于使用ReadingSpeed.Balanced设置无法成功读取条码值的情况,用户可以选择使用ReadingSpeed.Detailed。 IronBarcode 将对图像进行中等强度的处理,以使条形码区域更加清晰,以便条形码读取器能够检测到条形码。 此设置对于检测较小或不太清晰的条形码图像非常有用。ExtremeDetail:由于其CPU密集型过程,此设置是最不推荐的。 将对条形码图像进行大量处理,以便读取器能够读取条形码。 建议用户在选择此设置之前,先对图像进行预处理/应用滤镜。
我应该在何时使用Code39扩展模式?
此设置允许以扩展模式读取和解释 Code39 类型的条形码,其中将应用完整的 ASCII 字符集。 将UseCode39ExtendedMode设置为true将启用更准确的Code39条码读取。
如何实现具有自定义选项的高级条码读取?
既然我们已经了解了用户可以调整的所有选项,无论是为了提高性能还是提高准确性,现在介绍如何在代码中应用它们。 下面的代码片段展示了BarcodeReaderOptions的全面使用。
using IronBarCode;
using IronSoftware.Drawing;
using System;
using System.Linq;
// Create custom reader options
var options = new BarcodeReaderOptions()
{
// Specify expected barcode types for better performance
ExpectBarcodeTypes = BarcodeEncoding.QRCode | BarcodeEncoding.Code128 | BarcodeEncoding.Code39,
// Define specific area to scan (x, y, width, height in pixels)
CropArea = new Rectangle(100, 100, 500, 400),
// Set reading speed to balance accuracy and performance
Speed = ReadingSpeed.Balanced,
// Enable multithreading for better performance
Multithreaded = true,
MaxParallelThreads = 4,
// Remove false positives for accuracy
RemoveFalsePositive = true,
// Minimum scan lines that must agree for a valid result (default 2)
MinScanLines = 1,
// Enable Code39 extended mode if needed
UseCode39ExtendedMode = true,
// Set scan mode
ScanMode = BarcodeScanMode.Auto,
// Add image filters for better recognition
ImageFilters = new ImageFilterCollection() {
new SharpenFilter(),
new InvertFilter(),
new ContrastFilter()
}
};
// Read barcodes with custom options
var results = BarcodeReader.Read(@"C:\path\to\your\barcode-image.png", options);
// Process results
if (results.Any())
{
foreach (var barcode in results)
{
Console.WriteLine($"Barcode Type: {barcode.BarcodeType}");
Console.WriteLine($"Value: {barcode.Value}");
Console.WriteLine($"Position: {string.Join(", ", barcode.Points.Select(p => $"({p.X}, {p.Y})"))}");
Console.WriteLine("---");
}
}
else
{
Console.WriteLine("No barcodes found in the image.");
}using IronBarCode;
using IronSoftware.Drawing;
using System;
using System.Linq;
// Create custom reader options
var options = new BarcodeReaderOptions()
{
// Specify expected barcode types for better performance
ExpectBarcodeTypes = BarcodeEncoding.QRCode | BarcodeEncoding.Code128 | BarcodeEncoding.Code39,
// Define specific area to scan (x, y, width, height in pixels)
CropArea = new Rectangle(100, 100, 500, 400),
// Set reading speed to balance accuracy and performance
Speed = ReadingSpeed.Balanced,
// Enable multithreading for better performance
Multithreaded = true,
MaxParallelThreads = 4,
// Remove false positives for accuracy
RemoveFalsePositive = true,
// Minimum scan lines that must agree for a valid result (default 2)
MinScanLines = 1,
// Enable Code39 extended mode if needed
UseCode39ExtendedMode = true,
// Set scan mode
ScanMode = BarcodeScanMode.Auto,
// Add image filters for better recognition
ImageFilters = new ImageFilterCollection() {
new SharpenFilter(),
new InvertFilter(),
new ContrastFilter()
}
};
// Read barcodes with custom options
var results = BarcodeReader.Read(@"C:\path\to\your\barcode-image.png", options);
// Process results
if (results.Any())
{
foreach (var barcode in results)
{
Console.WriteLine($"Barcode Type: {barcode.BarcodeType}");
Console.WriteLine($"Value: {barcode.Value}");
Console.WriteLine($"Position: {string.Join(", ", barcode.Points.Select(p => $"({p.X}, {p.Y})"))}");
Console.WriteLine("---");
}
}
else
{
Console.WriteLine("No barcodes found in the image.");
}Imports IronBarCode
Imports IronSoftware.Drawing
Imports System
Imports System.Linq
' Create custom reader options
Dim options = New BarcodeReaderOptions() With {
' Specify expected barcode types for better performance
.ExpectBarcodeTypes = BarcodeEncoding.QRCode Or BarcodeEncoding.Code128 Or BarcodeEncoding.Code39,
' Define specific area to scan (x, y, width, height in pixels)
.CropArea = New Rectangle(100, 100, 500, 400),
' Set reading speed to balance accuracy and performance
.Speed = ReadingSpeed.Balanced,
' Enable multithreading for better performance
.Multithreaded = True,
.MaxParallelThreads = 4,
' Remove false positives for accuracy
.RemoveFalsePositive = True,
' Minimum scan lines that must agree for a valid result (default 2)
.MinScanLines = 1,
' Enable Code39 extended mode if needed
.UseCode39ExtendedMode = True,
' Set scan mode
.ScanMode = BarcodeScanMode.Auto,
' Add image filters for better recognition
.ImageFilters = New ImageFilterCollection() From {
New SharpenFilter(),
New InvertFilter(),
New ContrastFilter()
}
}
' Read barcodes with custom options
Dim results = BarcodeReader.Read("C:\path\to\your\barcode-image.png", options)
' Process results
If results.Any() Then
For Each barcode In results
Console.WriteLine($"Barcode Type: {barcode.BarcodeType}")
Console.WriteLine($"Value: {barcode.Value}")
Console.WriteLine($"Position: {String.Join(", ", barcode.Points.Select(Function(p) $"({p.X}, {p.Y})"))}")
Console.WriteLine("---")
Next
Else
Console.WriteLine("No barcodes found in the image.")
End IfImports IronBarCode
Imports IronSoftware.Drawing
Imports System
Imports System.Linq
' Create custom reader options
Dim options = New BarcodeReaderOptions() With {
' Specify expected barcode types for better performance
.ExpectBarcodeTypes = BarcodeEncoding.QRCode Or BarcodeEncoding.Code128 Or BarcodeEncoding.Code39,
' Define specific area to scan (x, y, width, height in pixels)
.CropArea = New Rectangle(100, 100, 500, 400),
' Set reading speed to balance accuracy and performance
.Speed = ReadingSpeed.Balanced,
' Enable multithreading for better performance
.Multithreaded = True,
.MaxParallelThreads = 4,
' Remove false positives for accuracy
.RemoveFalsePositive = True,
' Minimum scan lines that must agree for a valid result (default 2)
.MinScanLines = 1,
' Enable Code39 extended mode if needed
.UseCode39ExtendedMode = True,
' Set scan mode
.ScanMode = BarcodeScanMode.Auto,
' Add image filters for better recognition
.ImageFilters = New ImageFilterCollection() From {
New SharpenFilter(),
New InvertFilter(),
New ContrastFilter()
}
}
' Read barcodes with custom options
Dim results = BarcodeReader.Read("C:\path\to\your\barcode-image.png", options)
' Process results
If results.Any() Then
For Each barcode In results
Console.WriteLine($"Barcode Type: {barcode.BarcodeType}")
Console.WriteLine($"Value: {barcode.Value}")
Console.WriteLine($"Position: {String.Join(", ", barcode.Points.Select(Function(p) $"({p.X}, {p.Y})"))}")
Console.WriteLine("---")
Next
Else
Console.WriteLine("No barcodes found in the image.")
End If从代码片段中,我们可以看到要使用BarcodeReaderOptions,我们首先必须初始化它,然后根据上述属性确定和调整BarcodeReaderOptions的属性。 已初始化的BarcodeReaderOptions可以作为参数与图像文件一起用于BarcodeReader.Read()方法。这将在从图像读取条码时应用BarcodeReaderOptions中的所有设置。
如果您遇到条码未被识别的问题,我们的故障排查指南提供了常见问题的解决方案和提高条码扫描准确性的提示。
常见问题
如何使用IronBarcode在C#中读取图像中的条形码?
您可以通过使用IronBarcode中的BarcodeReader.Read()方法在C#中从图像读取条形码。该方法通过一行代码简化了条形码读取,并支持多种图像格式,如PNG、JPEG、GIF、BMP和TIFF。
IronBarcode 支持哪些图像格式的条码读取?
IronBarcode支持多种图像格式进行条形码读取,包括PNG、JPEG、GIF、BMP、TIFF和SVG。
如何自定义IronBarcode中的条形码读取选项?
您可以使用IronBarcode中的BarcodeReaderOptions类自定义条形码读取选项。它允许您配置如CropArea、ExpectBarcodeTypes、Speed等设置,以提高性能和准确性。
IronBarcode可以读取1D和2D条形码吗?
是的,IronBarcode可以读取1D和2D条形码。支持的格式包括Codabar、Code128、QRCode、DataMatrix等。
IronBarcode中的`CropArea`属性有什么用途?
IronBarcode中的CropArea属性用于指定图像中特定区域以读取条形码。这有助于通过集中在定义的区域来提高读取性能和准确性。
如何提高IronBarcode读取条形码的速度?
您可以通过调整BarcodeReaderOptions中的Speed属性来提高IronBarcode中的条形码读取速度。这允许您根据需要在性能和准确性之间进行平衡。
在IronBarcode中使用多线程有哪些好处?
IronBarcode中的多线程允许同时处理多个条形码,提高了条形码读取的速度和效率,尤其是在批量处理任务中。
图像滤镜如何在IronBarcode中改善条形码识别?
IronBarcode中的图像滤镜通过改善条形码图像的清晰度来增强预处理。像SharpenFilter和ContrastFilter这样的滤镜有助于优化图像质量以获得更好的识别效果。
IronBarcode中的`MinScanLines`属性有什么用?
MinScanLines属性设置必须对条形码结果达成一致的最小扫描线数量,以便认为其有效,从而减少误检并确保检测准确性。
如何在IronBarcode中处理多种条形码类型?
在IronBarcode中,您可以通过在BarcodeReaderOptions中设置ExpectBarcodeTypes属性来指定要读取的条形码类型,从而通过关注特定的条形码类型来提高性能。