C# 条形码扫描器:在 .NET 应用程序中读取条形码和二维码
需要在 .NET 应用程序中快速扫描条形码或二维码吗?无论处理的是清晰的数字图像,还是难以识别的实拍照片,IronBarcode 都能让条形码读取变得简单可靠。本指南通过可直接使用的实用示例,展示如何在 C# 中实现条形码扫描。
快速入门:从文件中即时读取条码这个简单的示例向您展示了IronBarcode入门是多么容易。 只需一行代码,您就可以从图像文件中读取条码,无需复杂设置。
-
1使用 IronBarcode 安装 NuGet Package Manager
-
2复制并运行这段代码。
var results = IronBarCode.BarcodeReader.Read("path/to/barcode.png");Dim results = IronBarCode.BarcodeReader.Read("path/to/barcode.png")Dim results = IronBarCode.BarcodeReader.Read("path/to/barcode.png") -
3部署到您的生产环境中进行测试
今天就用免费试用在您的项目中开始使用IronBarcode
最小工作流程(5 个步骤)
- 从NuGet或通过DLL下载安装IronBarcode
- 使用
BarcodeReader.Read方法扫描任何条形码或二维码 - 在单个扫描件、PDF 或多帧 TIFF 文件中读取多个条形码或二维码
- 启用 IronBarcode,利用高级滤镜解码不完美的扫描件和照片。
- 下载教程项目并立即开始扫描
如何在我的.NET项目中安装IronBarcode?
IronBarcode 可以通过 NuGet 包管理器轻松安装,也可以直接下载 DLL 文件进行安装。 建议使用 NuGet 安装,因为它能够自动管理依赖项和更新。
安装后,将using IronBarCode;添加到您的C#文件中以访问条码扫描功能。 有关不同开发环境的详细安装说明,请参阅我们的安装指南。
如何使用 C# 读取我的第一个条形码?
使用 IronBarcode 读取条形码只需要一行代码。 该库可自动检测条形码格式并提取所有编码数据。
IronBarcode 可以立即读取的标准 Code128 条形码using IronBarCode;
BarcodeReaderOptions options = new BarcodeReaderOptions()
{
// Choose which filters are to be applied (in order)
ImageFilters = new ImageFilterCollection() {
new AdaptiveThresholdFilter(),
},
// Uses machine learning to auto rotate the barcode
AutoRotate = true,
};
// Read barcode
BarcodeResults results = BarcodeReader.Read("TryHarderQR.png", options);using IronBarCode;
BarcodeReaderOptions options = new BarcodeReaderOptions()
{
// Choose which filters are to be applied (in order)
ImageFilters = new ImageFilterCollection() {
new AdaptiveThresholdFilter(),
},
// Uses machine learning to auto rotate the barcode
AutoRotate = true,
};
// Read barcode
BarcodeResults results = BarcodeReader.Read("TryHarderQR.png", options);Imports IronBarCode
Private options As New BarcodeReaderOptions() With {
.ImageFilters = New ImageFilterCollection() From {New AdaptiveThresholdFilter()},
.AutoRotate = True
}
' Read barcode
Private results As BarcodeResults = BarcodeReader.Read("TryHarderQR.png", options)Imports IronBarCode
Private options As New BarcodeReaderOptions() With {
.ImageFilters = New ImageFilterCollection() From {New AdaptiveThresholdFilter()},
.AutoRotate = True
}
' Read barcode
Private results As BarcodeResults = BarcodeReader.Read("TryHarderQR.png", options)BarcodeReader.Read 方法返回一个 BarcodeResults 集合,包含所有检测到的条形码。 每个BarcodeResult提供对条码文本值、格式类型、位置坐标和二进制数据的访问。 此方法适用于常见条码格式,包括Code128、Code39、QR码和Data Matrix码。
哪些方法可以帮助读取难以辨认或损坏的条形码?
现实世界中的条形码扫描经常会遇到图像不完美的情况——角度倾斜、光线不足或部分损坏。 IronBarcode 的高级选项可以有效应对这些挑战。
using IronBarCode;
using System;
// Multiple barcodes may be scanned up from a single document or image. A PDF document may also used as the input image
BarcodeResults results = BarcodeReader.ReadPdf("MultipleBarcodes.pdf");
// Work with the results
foreach (var pageResult in results)
{
string Value = pageResult.Value;
int PageNum = pageResult.PageNumber;
System.Drawing.Bitmap Img = pageResult.BarcodeImage;
BarcodeEncoding BarcodeType = pageResult.BarcodeType;
byte[] Binary = pageResult.BinaryValue;
Console.WriteLine(pageResult.Value + " on page " + PageNum);
}using IronBarCode;
using System;
// Multiple barcodes may be scanned up from a single document or image. A PDF document may also used as the input image
BarcodeResults results = BarcodeReader.ReadPdf("MultipleBarcodes.pdf");
// Work with the results
foreach (var pageResult in results)
{
string Value = pageResult.Value;
int PageNum = pageResult.PageNumber;
System.Drawing.Bitmap Img = pageResult.BarcodeImage;
BarcodeEncoding BarcodeType = pageResult.BarcodeType;
byte[] Binary = pageResult.BinaryValue;
Console.WriteLine(pageResult.Value + " on page " + PageNum);
}Imports IronBarCode
Imports System
' Multiple barcodes may be scanned up from a single document or image. A PDF document may also used as the input image
Private results As BarcodeResults = BarcodeReader.ReadPdf("MultipleBarcodes.pdf")
' Work with the results
For Each pageResult In results
Dim Value As String = pageResult.Value
Dim PageNum As Integer = pageResult.PageNumber
Dim Img As System.Drawing.Bitmap = pageResult.BarcodeImage
Dim BarcodeType As BarcodeEncoding = pageResult.BarcodeType
Dim Binary() As Byte = pageResult.BinaryValue
Console.WriteLine(pageResult.Value & " on page " & PageNum)
Next pageResultImports IronBarCode
Imports System
' Multiple barcodes may be scanned up from a single document or image. A PDF document may also used as the input image
Private results As BarcodeResults = BarcodeReader.ReadPdf("MultipleBarcodes.pdf")
' Work with the results
For Each pageResult In results
Dim Value As String = pageResult.Value
Dim PageNum As Integer = pageResult.PageNumber
Dim Img As System.Drawing.Bitmap = pageResult.BarcodeImage
Dim BarcodeType As BarcodeEncoding = pageResult.BarcodeType
Dim Binary() As Byte = pageResult.BinaryValue
Console.WriteLine(pageResult.Value & " on page " & PageNum)
Next pageResult
IronBarcode 使用高级选项成功读取了旋转后的二维码ExpectBarcodeTypes 属性将搜索范围限定为特定格式,从而显著提高性能。处理质量不佳的图像时,可结合使用图像滤镜和自动旋转功能,以获得最高识别准确率:
using IronBarCode;
// Multi frame TIFF and GIF images can also be scanned
BarcodeResults multiFrameResults = BarcodeReader.Read("Multiframe.tiff");
foreach (var pageResult in multiFrameResults)
{
//...
}using IronBarCode;
// Multi frame TIFF and GIF images can also be scanned
BarcodeResults multiFrameResults = BarcodeReader.Read("Multiframe.tiff");
foreach (var pageResult in multiFrameResults)
{
//...
}Imports IronBarCode
' Multi frame TIFF and GIF images can also be scanned
Private multiFrameResults As BarcodeResults = BarcodeReader.Read("Multiframe.tiff")
For Each pageResult In multiFrameResults
'...
Next pageResultImports IronBarCode
' Multi frame TIFF and GIF images can also be scanned
Private multiFrameResults As BarcodeResults = BarcodeReader.Read("Multiframe.tiff")
For Each pageResult In multiFrameResults
'...
Next pageResult这些高级功能使 IronBarcode 非常适合从照片中扫描条形码,也适用于监控摄像头或移动设备拍摄的图像,这些图像的质量往往差异很大。
如何扫描PDF文档中的多个条形码?
PDF条形码扫描对于处理发票、发货标签和库存文件至关重要。 IronBarcode 可以高效读取每一页上的所有条形码。
从PDF文件中读取条形码
using IronBarCode;
// The Multithreaded property allows for faster barcode scanning across multiple images or PDFs. All threads are automatically managed by IronBarCode.
var ListOfDocuments = new[] { "image1.png", "image2.JPG", "image3.pdf" };
BarcodeReaderOptions options = new BarcodeReaderOptions()
{
// Enable multithreading
Multithreaded = true,
};
BarcodeResults batchResults = BarcodeReader.Read(ListOfDocuments, options);using IronBarCode;
// The Multithreaded property allows for faster barcode scanning across multiple images or PDFs. All threads are automatically managed by IronBarCode.
var ListOfDocuments = new[] { "image1.png", "image2.JPG", "image3.pdf" };
BarcodeReaderOptions options = new BarcodeReaderOptions()
{
// Enable multithreading
Multithreaded = true,
};
BarcodeResults batchResults = BarcodeReader.Read(ListOfDocuments, options);Imports IronBarCode
' The Multithreaded property allows for faster barcode scanning across multiple images or PDFs. All threads are automatically managed by IronBarCode.
Private ListOfDocuments = { "image1.png", "image2.JPG", "image3.pdf" }
Private options As New BarcodeReaderOptions() With {.Multithreaded = True}
Private batchResults As BarcodeResults = BarcodeReader.Read(ListOfDocuments, options)Imports IronBarCode
' The Multithreaded property allows for faster barcode scanning across multiple images or PDFs. All threads are automatically managed by IronBarCode.
Private ListOfDocuments = { "image1.png", "image2.JPG", "image3.pdf" }
Private options As New BarcodeReaderOptions() With {.Multithreaded = True}
Private batchResults As BarcodeResults = BarcodeReader.Read(ListOfDocuments, options)
控制台输出显示在不同的 PDF 页面中发现了多个条形码
对于特定页面范围或高级PDF处理,请使用BarcodeReaderOptions:
// Read only specific pages to improve performance
PdfBarcodeReaderOptions pdfOptions = new PdfBarcodeReaderOptions
{
// Scan pages 1-5 only
PageNumbers = new[] { 1, 2, 3, 4, 5 },
// PDF-specific settings
DPI = 300 // Higher DPI for better accuracy
};
BarcodeResults results = BarcodeReader.ReadPdf("document.pdf", pdfOptions);// Read only specific pages to improve performance
PdfBarcodeReaderOptions pdfOptions = new PdfBarcodeReaderOptions
{
// Scan pages 1-5 only
PageNumbers = new[] { 1, 2, 3, 4, 5 },
// PDF-specific settings
DPI = 300 // Higher DPI for better accuracy
};
BarcodeResults results = BarcodeReader.ReadPdf("document.pdf", pdfOptions);' Read only specific pages to improve performance
Dim pdfOptions As New PdfBarcodeReaderOptions() With {
' Scan pages 1-5 only
.PageNumbers = New Integer() { 1, 2, 3, 4, 5 },
' PDF-specific settings
.DPI = 300 ' Higher DPI for better accuracy
}
Dim results As BarcodeResults = BarcodeReader.ReadPdf("document.pdf", pdfOptions)' Read only specific pages to improve performance
Dim pdfOptions As New PdfBarcodeReaderOptions() With {
' Scan pages 1-5 only
.PageNumbers = New Integer() { 1, 2, 3, 4, 5 },
' PDF-specific settings
.DPI = 300 ' Higher DPI for better accuracy
}
Dim results As BarcodeResults = BarcodeReader.ReadPdf("document.pdf", pdfOptions)如何处理多帧TIFF图像?
多帧 TIFF 文件(常见于文档扫描和传真系统)与 PDF 文件一样,都得到了全面的支持。
一个多帧 TIFF 文件,不同帧上带有条形码。
using IronBarCode;
using System;
// TIFF files are processed similarly to regular images
// Each frame is scanned automatically
BarcodeResults multiFrameResults = BarcodeReader.Read("Multiframe.tiff");
foreach (var result in multiFrameResults)
{
// Access frame-specific information
int frameNumber = result.PageNumber; // Frame number in TIFF
string barcodeValue = result.Text;
Console.WriteLine($"Frame {frameNumber}: {barcodeValue}");
// Save individual barcode images if needed
result.BarcodeImage?.SaveAs($"barcode_frame_{frameNumber}.png");
}using IronBarCode;
using System;
// TIFF files are processed similarly to regular images
// Each frame is scanned automatically
BarcodeResults multiFrameResults = BarcodeReader.Read("Multiframe.tiff");
foreach (var result in multiFrameResults)
{
// Access frame-specific information
int frameNumber = result.PageNumber; // Frame number in TIFF
string barcodeValue = result.Text;
Console.WriteLine($"Frame {frameNumber}: {barcodeValue}");
// Save individual barcode images if needed
result.BarcodeImage?.SaveAs($"barcode_frame_{frameNumber}.png");
}Imports IronBarCode
Imports System
' TIFF files are processed similarly to regular images
' Each frame is scanned automatically
Dim multiFrameResults As BarcodeResults = BarcodeReader.Read("Multiframe.tiff")
For Each result In multiFrameResults
' Access frame-specific information
Dim frameNumber As Integer = result.PageNumber ' Frame number in TIFF
Dim barcodeValue As String = result.Text
Console.WriteLine($"Frame {frameNumber}: {barcodeValue}")
' Save individual barcode images if needed
result.BarcodeImage?.SaveAs($"barcode_frame_{frameNumber}.png")
NextImports IronBarCode
Imports System
' TIFF files are processed similarly to regular images
' Each frame is scanned automatically
Dim multiFrameResults As BarcodeResults = BarcodeReader.Read("Multiframe.tiff")
For Each result In multiFrameResults
' Access frame-specific information
Dim frameNumber As Integer = result.PageNumber ' Frame number in TIFF
Dim barcodeValue As String = result.Text
Console.WriteLine($"Frame {frameNumber}: {barcodeValue}")
' Save individual barcode images if needed
result.BarcodeImage?.SaveAs($"barcode_frame_{frameNumber}.png")
Next同样的BarcodeReaderOptions适用于TIFF处理,包括图像滤镜和旋转设置。 有关详细的TIFF处理场景,请参阅我们的图像处理教程。
我能否利用多线程来加快处理速度?
并行处理能极大地提高多文档处理效率。 IronBarcode 会自动利用可用的 CPU 核心,以实现最佳性能。
using IronBarCode;
// List of documents to process - mix of formats supported
var documentBatch = new[]
{
"invoice1.pdf",
"shipping_label.png",
"inventory_sheet.tiff",
"product_catalog.pdf"
};
// Configure for batch processing
BarcodeReaderOptions batchOptions = new BarcodeReaderOptions
{
// Enable parallel processing across documents
Multithreaded = true,
// Limit threads if needed (0 = use all cores)
MaxParallelThreads = Environment.ProcessorCount,
// Apply consistent settings to all documents
Speed = ReadingSpeed.Balanced,
ExpectBarcodeTypes = BarcodeEncoding.All
};
// Process each document, tracking the source path externally
foreach (var document in documentBatch)
{
BarcodeResults results = BarcodeReader.Read(document, batchOptions);
Console.WriteLine($"\nDocument: {document}");
foreach (var barcode in results)
{
Console.WriteLine($" - {barcode.BarcodeType}: {barcode.Text}");
}
}using IronBarCode;
// List of documents to process - mix of formats supported
var documentBatch = new[]
{
"invoice1.pdf",
"shipping_label.png",
"inventory_sheet.tiff",
"product_catalog.pdf"
};
// Configure for batch processing
BarcodeReaderOptions batchOptions = new BarcodeReaderOptions
{
// Enable parallel processing across documents
Multithreaded = true,
// Limit threads if needed (0 = use all cores)
MaxParallelThreads = Environment.ProcessorCount,
// Apply consistent settings to all documents
Speed = ReadingSpeed.Balanced,
ExpectBarcodeTypes = BarcodeEncoding.All
};
// Process each document, tracking the source path externally
foreach (var document in documentBatch)
{
BarcodeResults results = BarcodeReader.Read(document, batchOptions);
Console.WriteLine($"\nDocument: {document}");
foreach (var barcode in results)
{
Console.WriteLine($" - {barcode.BarcodeType}: {barcode.Text}");
}
}Imports IronBarCode
' List of documents to process - mix of formats supported
Dim documentBatch = New String() {"invoice1.pdf", "shipping_label.png", "inventory_sheet.tiff", "product_catalog.pdf"}
' Configure for batch processing
Dim batchOptions As New BarcodeReaderOptions With {
.Multithreaded = True,
' Limit threads if needed (0 = use all cores)
.MaxParallelThreads = Environment.ProcessorCount,
' Apply consistent settings to all documents
.Speed = ReadingSpeed.Balanced,
.ExpectBarcodeTypes = BarcodeEncoding.All
}
' Process each document, tracking the source path externally
For Each document In documentBatch
Dim results As BarcodeResults = BarcodeReader.Read(document, batchOptions)
Console.WriteLine(vbLf & "Document: " & document)
For Each barcode In results
Console.WriteLine($" - {barcode.BarcodeType}: {barcode.Text}")
Next
NextImports IronBarCode
' List of documents to process - mix of formats supported
Dim documentBatch = New String() {"invoice1.pdf", "shipping_label.png", "inventory_sheet.tiff", "product_catalog.pdf"}
' Configure for batch processing
Dim batchOptions As New BarcodeReaderOptions With {
.Multithreaded = True,
' Limit threads if needed (0 = use all cores)
.MaxParallelThreads = Environment.ProcessorCount,
' Apply consistent settings to all documents
.Speed = ReadingSpeed.Balanced,
.ExpectBarcodeTypes = BarcodeEncoding.All
}
' Process each document, tracking the source path externally
For Each document In documentBatch
Dim results As BarcodeResults = BarcodeReader.Read(document, batchOptions)
Console.WriteLine(vbLf & "Document: " & document)
For Each barcode In results
Console.WriteLine($" - {barcode.BarcodeType}: {barcode.Text}")
Next
Next这种并行方法可以同时处理文档,减少多核系统上的总扫描时间。 对于企业级条码处理,请探索我们的性能优化指南。
总结
IronBarcode 将复杂的条形码扫描转化为简洁的 C# 代码。无论构建库存系统、文档处理程序还是移动应用程序,该库都能处理清晰的数字条形码和难以识别的实拍图像。
主要功能涵盖:
- 用一行代码从图像中读取条形码
- 针对损坏或旋转条形码的高级选项
- 全面扫描 PDF 和 TIFF 文档
- 高性能多线程批处理
- 支持所有主要条码格式
进一步阅读
利用以下资源扩展您的条形码处理能力:
- 条码生成教程 - 创建自定义条码
- QR码指南 - 专业的QR码功能
BarcodeReader类参考 - 完整的 API 文档- 故障排除指南 - 常见问题和解决方案
源代码下载
请自行运行以下示例:
准备好在您的应用程序中实现条形码扫描了吗? 开始您的免费试用,今天就将专业的条码读取功能添加到您的.NET项目中。
常见问题
什么是IronBarcode?
IronBarcode 是一个 .NET 库,通过提供易于使用的功能和高级特性来处理各种图像质量级别,从而简化了在 C# 中读取条形码和 QR 码的过程。
如何使用 IronBarcode 在 C# 中读取条形码?
您可以通过一行代码在 C# 中使用 IronBarcode 读取条形码:IronBarCode.BarcodeReader.Read("path/to/barcode.png");。此方法可以有效检测条形码类型并提供编码信息。
IronBarcode 能否从单个文件中读取多个条形码?
可以,IronBarcode 支持从单个图像或文档中读取多个条形码,包括 PDF 和多帧 TIFF 文件,通过自动检测和提取每个条形码。
IronBarcode 提供了哪些功能来处理破损的条形码?
IronBarcode 提供了高级功能,如自动旋转、使用 AdaptiveThresholdFilter 的图像过滤,以及使用机器学习从不完美的图像中解码条形码,例如光线不足或角度偏斜的图像。
如何在我的 .NET 项目中安装 IronBarcode?
您可以通过 NuGet 包管理器或下载 DLL 文件轻松安装 IronBarcode。建议使用 NuGet 进行自动依赖项管理和更新。
IronBarcode 支持从 PDF 文档中扫描条形码吗?
是的,IronBarcode 高效扫描 PDF 文档中所有页面的条形码,并支持多线程以提高处理速度。
IronBarcode 可以处理多帧 TIFF 图像吗?
可以,IronBarcode 可以处理多帧 TIFF 图像,自动扫描每一帧的条形码并提供综合结果。
IronBarcode 如何优化条形码扫描性能?
IronBarcode 通过多线程功能优化性能,允许使用所有可用的 CPU 内核跨多个文档和图像进行条形码扫描,从而减少处理时间。
IronBarcode 支持哪些条码格式?
IronBarcode 支持多种条形码格式,包括 Code128、Code39、QR 码、Data Matrix 码等,允许在各种场景中灵活应用。
有没有办法提高 IronBarcode 的条形码读取准确性?
可以通过配置 BarcodeReaderOptions 以使用特定图像过滤器、启用自动旋转,并设置 ExpectBarcodeTypes 属性以限制扫描的条形码类型,来提高 IronBarcode 的准确性。
Jacob Mellor是Iron Software的首席技术官,也是C# PDF技术的先锋工程师。作为Iron Software核心代码库的原始开发者,自公司成立以来,他就一直在塑造公司的产品架构,与首席执行官Cameron Rimington一起将其转型为拥有50多人、服务于NASA、特斯拉和全球政府机构的公司。