IronOCR 通过在配置中设置 ReadBarCodes = true 读取条形码和二维码。 该单一设置可从 PDF 和图像中自动提取条形码值,同时还能识别常规文本,支持 20 多种条形码格式,包括 QR 码、Code 128 码和数据矩阵。
快速开始:立即从PDF读取条形码
只需一个设置即可启用条形码检测,并使用 IronOCR 扫描 PDF。 下面的代码展示了如何打开条形码读取、处理 PDF 和检索解码值。
1Install IronOCR with NuGet Package Manager
PM > Install-Package IronOcr
Install-Package IronOcr
2复制并运行这段代码。
var result = new IronOcr.IronTesseract() { Configuration = new IronOcr.TesseractConfiguration { ReadBarCodes = true } }.Read(new IronOcr.OcrPdfInput("document.pdf"));foreach(var bc in result.Barcodes) Console.WriteLine(bc.Value);
var result = new IronOcr.IronTesseract() { Configuration = new IronOcr.TesseractConfiguration { ReadBarCodes = true } }.Read(new IronOcr.OcrPdfInput("document.pdf"));
foreach(var bc in result.Barcodes) Console.WriteLine(bc.Value);
创建一个 IronTesseract 对象来进行读取。 设置 ReadBarCodes 属性为 true 以启用条形码检测。 使用OcrPdfInput 构造函数导入 PDF 文档。 使用 Read 方法对导入的 PDF 执行 OCR。
下面是一个使用该 PDF 文档的示例:
using IronOcr;using System;// Instantiate IronTesseractIronTesseract ocrTesseract = new IronTesseract();// Enable barcode readingocrTesseract.Configuration.ReadBarCodes = true;// Add PDFusing var imageInput = new OcrPdfInput("pdfWithBarcodes.pdf");// Perform OCROcrResult ocrResult = ocrTesseract.Read(imageInput);// Output detected barcodes and text valuesConsole.WriteLine("Extracted text:");Console.WriteLine(ocrResult.Text);Console.WriteLine("Extracted barcodes:");foreach (var barcode in ocrResult.Barcodes){Console.WriteLine(barcode.Value);}
using IronOcr;
using System;
// Instantiate IronTesseract
IronTesseract ocrTesseract = new IronTesseract();
// Enable barcode reading
ocrTesseract.Configuration.ReadBarCodes = true;
// Add PDF
using var imageInput = new OcrPdfInput("pdfWithBarcodes.pdf");
// Perform OCR
OcrResult ocrResult = ocrTesseract.Read(imageInput);
// Output detected barcodes and text values
Console.WriteLine("Extracted text:");
Console.WriteLine(ocrResult.Text);
Console.WriteLine("Extracted barcodes:");
foreach (var barcode in ocrResult.Barcodes)
{
Console.WriteLine(barcode.Value);
}
ImportsIronOcrImportsSystem' Instantiate IronTesseractPrivate ocrTesseract As New IronTesseract()' Enable barcode readingocrTesseract.Configuration.ReadBarCodes = True' Add PDFDim imageInput = New OcrPdfInput("pdfWithBarcodes.pdf")' Perform OCRDim ocrResult AsOcrResult = ocrTesseract.Read(imageInput)' Output detected barcodes and text valuesConsole.WriteLine("Extracted text:")Console.WriteLine(ocrResult.Text)Console.WriteLine("Extracted barcodes:")For Each barcode In ocrResult.BarcodesConsole.WriteLine(barcode.Value)Next barcode
Imports IronOcr
Imports System
' Instantiate IronTesseract
Private ocrTesseract As New IronTesseract()
' Enable barcode reading
ocrTesseract.Configuration.ReadBarCodes = True
' Add PDF
Dim imageInput = New OcrPdfInput("pdfWithBarcodes.pdf")
' Perform OCR
Dim ocrResult As OcrResult = ocrTesseract.Read(imageInput)
' Output detected barcodes and text values
Console.WriteLine("Extracted text:")
Console.WriteLine(ocrResult.Text)
Console.WriteLine("Extracted barcodes:")
For Each barcode In ocrResult.Barcodes
Console.WriteLine(barcode.Value)
Next barcode
像读取条形码一样,将 ReadBarCodes 属性设置为 true。 除文件路径外,无需更改其他代码。 使用二维码处理此 PDF 文档:
using IronOcr;using System;// Instantiate IronTesseractIronTesseract ocrTesseract = new IronTesseract();// Enable barcode readingocrTesseract.Configuration.ReadBarCodes = true;// Add PDFusing var imageInput = new OcrPdfInput("pdfWithQrCodes.pdf");// Perform OCROcrResult ocrResult = ocrTesseract.Read(imageInput);// Output detected barcodes and text valuesConsole.WriteLine("Extracted text:");Console.WriteLine(ocrResult.Text);Console.WriteLine("Extracted barcodes:");foreach (var barcode in ocrResult.Barcodes){Console.WriteLine(barcode.Value);}
using IronOcr;
using System;
// Instantiate IronTesseract
IronTesseract ocrTesseract = new IronTesseract();
// Enable barcode reading
ocrTesseract.Configuration.ReadBarCodes = true;
// Add PDF
using var imageInput = new OcrPdfInput("pdfWithQrCodes.pdf");
// Perform OCR
OcrResult ocrResult = ocrTesseract.Read(imageInput);
// Output detected barcodes and text values
Console.WriteLine("Extracted text:");
Console.WriteLine(ocrResult.Text);
Console.WriteLine("Extracted barcodes:");
foreach (var barcode in ocrResult.Barcodes)
{
Console.WriteLine(barcode.Value);
}
ImportsIronOcrImportsSystem' Instantiate IronTesseractPrivate ocrTesseract As New IronTesseract()' Enable barcode readingocrTesseract.Configuration.ReadBarCodes = True' Add PDFDim imageInput = New OcrPdfInput("pdfWithQrCodes.pdf")' Perform OCRDim ocrResult AsOcrResult = ocrTesseract.Read(imageInput)' Output detected barcodes and text valuesConsole.WriteLine("Extracted text:")Console.WriteLine(ocrResult.Text)Console.WriteLine("Extracted barcodes:")For Each barcode In ocrResult.BarcodesConsole.WriteLine(barcode.Value)Next barcode
Imports IronOcr
Imports System
' Instantiate IronTesseract
Private ocrTesseract As New IronTesseract()
' Enable barcode reading
ocrTesseract.Configuration.ReadBarCodes = True
' Add PDF
Dim imageInput = New OcrPdfInput("pdfWithQrCodes.pdf")
' Perform OCR
Dim ocrResult As OcrResult = ocrTesseract.Read(imageInput)
' Output detected barcodes and text values
Console.WriteLine("Extracted text:")
Console.WriteLine(ocrResult.Text)
Console.WriteLine("Extracted barcodes:")
For Each barcode In ocrResult.Barcodes
Console.WriteLine(barcode.Value)
Next barcode
// Apply filters to improve QR code readabilityocrTesseract.Configuration.ReadBarCodes = true;var input = new OcrImageInput("qr-code-scan.jpg");input.DeNoise();input.Sharpen();input.EnhanceResolution();var result = ocrTesseract.Read(input);
// Apply filters to improve QR code readability
ocrTesseract.Configuration.ReadBarCodes = true;
var input = new OcrImageInput("qr-code-scan.jpg");
input.DeNoise();
input.Sharpen();
input.EnhanceResolution();
var result = ocrTesseract.Read(input);
' Apply filters to improve QR code readabilityocrTesseract.Configuration.ReadBarCodes = TrueDim input As New OcrImageInput("qr-code-scan.jpg")input.DeNoise()input.Sharpen()input.EnhanceResolution()Dim result = ocrTesseract.Read(input)
' Apply filters to improve QR code readability
ocrTesseract.Configuration.ReadBarCodes = True
Dim input As New OcrImageInput("qr-code-scan.jpg")
input.DeNoise()
input.Sharpen()
input.EnhanceResolution()
Dim result = ocrTesseract.Read(input)
// Enhanced QR code reading with preprocessingvar ocrTesseract = new IronTesseract();ocrTesseract.Configuration.ReadBarCodes = true;// Configure for better QR detectionvar input = new OcrImageInput("document-with-qr.pdf");input.TargetDPI = 300; // Ensure sufficient resolutioninput.Binarize(); // Convert to black and whiteinput.DeNoise(); // Remove image artifactsvar result = ocrTesseract.Read(input);
// Enhanced QR code reading with preprocessing
var ocrTesseract = new IronTesseract();
ocrTesseract.Configuration.ReadBarCodes = true;
// Configure for better QR detection
var input = new OcrImageInput("document-with-qr.pdf");
input.TargetDPI = 300; // Ensure sufficient resolution
input.Binarize(); // Convert to black and white
input.DeNoise(); // Remove image artifacts
var result = ocrTesseract.Read(input);
ImportsIronTesseract' Enhanced QR code reading with preprocessingDim ocrTesseract = New IronTesseract()ocrTesseract.Configuration.ReadBarCodes = True' Configure for better QR detectionDim input = New OcrImageInput("document-with-qr.pdf")input.TargetDPI = 300 ' Ensure sufficient resolutioninput.Binarize() ' Convert to black and whiteinput.DeNoise() ' Remove image artifactsDim result = ocrTesseract.Read(input)
Imports IronTesseract
' Enhanced QR code reading with preprocessing
Dim ocrTesseract = New IronTesseract()
ocrTesseract.Configuration.ReadBarCodes = True
' Configure for better QR detection
Dim input = New OcrImageInput("document-with-qr.pdf")
input.TargetDPI = 300 ' Ensure sufficient resolution
input.Binarize() ' Convert to black and white
input.DeNoise() ' Remove image artifacts
Dim result = ocrTesseract.Read(input)
// Process multiple documents in parallelvar documents = new[] { "doc1.pdf", "doc2.pdf", "doc3.pdf" };var results = documents.AsParallel().Select(doc =>{ var tesseract = new IronTesseract(); tesseract.Configuration.ReadBarCodes = true; return tesseract.Read(new OcrPdfInput(doc));}).ToList();
// Process multiple documents in parallel
var documents = new[] { "doc1.pdf", "doc2.pdf", "doc3.pdf" };
var results = documents.AsParallel().Select(doc =>
{
var tesseract = new IronTesseract();
tesseract.Configuration.ReadBarCodes = true;
return tesseract.Read(new OcrPdfInput(doc));
}).ToList();
ImportsIronTesseract' Process multiple documents in parallelDim documents = {"doc1.pdf", "doc2.pdf", "doc3.pdf"}Dim results = documents.AsParallel().Select(Function(doc) Dim tesseract = New IronTesseract() tesseract.Configuration.ReadBarCodes = True Return tesseract.Read(New OcrPdfInput(doc))End Function).ToList()
Imports IronTesseract
' Process multiple documents in parallel
Dim documents = {"doc1.pdf", "doc2.pdf", "doc3.pdf"}
Dim results = documents.AsParallel().Select(Function(doc)
Dim tesseract = New IronTesseract()
tesseract.Configuration.ReadBarCodes = True
Return tesseract.Read(New OcrPdfInput(doc))
End Function).ToList()
// Start an async read that can be aborted if it runs too longOcrReadTask ocrReadTask = ocrTesseract.ReadAsync(ocrInput);// Cancel if processing takes longer than 5 minutesif (!ocrReadTask.Wait(TimeSpan.FromMinutes(5))){ ocrReadTask.Cancel();}
// Start an async read that can be aborted if it runs too long
OcrReadTask ocrReadTask = ocrTesseract.ReadAsync(ocrInput);
// Cancel if processing takes longer than 5 minutes
if (!ocrReadTask.Wait(TimeSpan.FromMinutes(5)))
{
ocrReadTask.Cancel();
}
How can I improve the accuracy of QR code recognition with IronOCR?
Improve QR code recognition by ensuring high resolution, applying image corrections like de-noising and sharpening, and handling orientation issues.
What are common challenges when reading QR codes with OCR?
Common challenges include resolution issues, image quality problems, orientation errors, and interference from overlapping text or graphics.
Can IronOCR handle multi-page PDF documents with barcodes?
Yes, IronOCR can process multi-page PDF documents with barcodes by reading each page individually and outputting detected barcode values and formats.
What is the benefit of using IronOCR for PDF text extraction and barcode reading?
Using IronOCR allows for unified text and barcode extraction from PDF documents, reducing the need for separate libraries and processing steps for each type of data.
How can IronOCR's barcode capabilities integrate into business applications?
IronOCR can be used in various business applications such as inventory management, document archiving, invoice processing, and healthcare records by extracting and processing barcode information.