如何使用 C# 中的 OCR 从收据中提取数据
IronOCR 提供了一个强大的 C# 库,使用先进的 OCR 技术从收据图像中提取文本,支持 125 种语言和内置图像预处理功能,可实现自动费用跟踪和数据分析。
收据和自动化
收据在当今快节奏的世界中至关重要。 无论你是购买食品杂货还是外出就餐,收据都能帮助你追踪支出并进行预算。 与此同时,商店使用收据扫描仪分析销售数据,通过数据提取技术帮助他们预测需求和管理库存。
然而,收据可能难以辨认,计算结果也并不总是清晰明了。 手动输入预算数据既繁琐又容易出错,尤其是在项目很多的情况下。 丢失收据可能会让你的每月超支变成一个谜。 传统纸质收据经常存在打印质量差、油墨褪色和热敏纸老化等问题,因此OCR 图像优化对于准确提取至关重要。
为了解决这个问题,预算和财务应用程序采用了OCR (光学字符识别)技术。 通过将收据扫描成数字格式,OCR 可以最大限度地减少错误,自动输入数据,跟踪支出,并揭示购买模式。 现代OCR 解决方案可处理各种收据格式,从传统的销售点打印输出到具有条形码和二维码读取功能的数字收据。
OCR利用机器学习技术从图像中识别和提取文本。 该过程包括图像预处理、字符分割、模式识别和验证。 然而,OCR 并非完美无缺——模糊或污迹会导致错误。 先进系统利用计算机视觉技术来提高准确率。 选择一个可靠的、能够高效处理和优化读取的 OCR 库对于成功实现文档自动化至关重要。
为什么我应该选择 IronOCR 进行收据处理?
IronOCR是一个基于定制Tesseract OCR 引擎的 C# 库。与标准 Tesseract 不同,IronOCR 包含了Tesseract 5 的优化和专为 .NET 开发人员设计的功能。 它之所以脱颖而出,是因为它具备以下特点:
1.跨平台兼容性:可与 .NET 8、7、6、5 和 Framework 4.6.2+ 配合使用。 可在 Windows、macOS、Azure 和 Linux 系统上运行。 可无缝部署到Docker 、 AWS Lambda和Azure Functions 。
2.灵活性和可扩展性:支持 JPG、PNG 和 GIF 格式。 与 System.Drawing 对象集成。 处理多页 TIFF和PDF 流。 支持 多线程,适用于高吞吐量场景。
3.易用性和支持:文档齐全,API 功能强大,并提供 24/5 支持。 提供简单的单行操作和详细的配置选项。 包含全面的故障排除指南。
4.多语言功能:支持125 种国际语言。 能够有效识别产品名称和价格。 支持每个文档使用多种语言。 支持自定义训练数据文件。
如何在我的应用程序中实现收据OCR?
我需要什么许可证才能开始?
使用 IronOCR 之前,您需要一个许可证密钥。 点击这里获取免费试用。 许可选项包括 Lite、Plus 和 Professional 三种级别,分别适用于不同的团队规模和部署场景。 请参阅有关应用许可证密钥的文档。
// Replace the license key variable with the trial key you obtained
IronOcr.License.LicenseKey = "REPLACE-WITH-YOUR-KEY";// Replace the license key variable with the trial key you obtained
IronOcr.License.LicenseKey = "REPLACE-WITH-YOUR-KEY";对于 Web 应用程序,请在 Web.config 中设置许可证密钥以进行集中配置。 随着您的业务发展,许可系统支持扩展和升级。
如何使用 IronOCR 读取超市收据?
让我们探索如何在应用程序中使用 IronOCR,该应用程序可以使用智能手机扫描超市收据,提取产品名称和价格,并根据购买情况奖励忠诚度积分。 这包括图像采集、预处理、OCR执行以及使用结果置信度评分进行数据验证。
典型的收据图片是什么样的?
这是一张超市收据示例,其中包含占位符文本(Lorem ipsum),以及商品、价格、小计(107.60 美元)和付款详情——展示了典型的收据布局,包括抬头、商品列表、总计和条形码。
常见的收据问题包括热敏纸质量、字体不统一、版面拥挤以及折叠或受潮造成的损坏。 IronOCR 的预处理通过图像质量校正和颜色校正技术来处理这些问题。
我需要编写哪些 C# 代码来提取收据数据?
using IronOcr;
class ReceiptScanner
{
static void Main()
{
// Set the license key for IronOCR
IronOcr.License.LicenseKey = "YOUR-KEY";
// Instantiate OCR engine with optimal settings for receipts
var ocr = new IronTesseract();
// Configure for receipt-specific text
ocr.Configuration.WhiteListCharacters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789.$,- ";
ocr.Configuration.BlackListCharacters = "~`@#%^*_+={}[]|\\:;\"'<>?";
using var inputPhoto = new OcrInput();
inputPhoto.LoadImage("supermarketexample.jpg");
// Apply preprocessing for better accuracy
inputPhoto.DeNoise();
inputPhoto.ToGrayScale();
inputPhoto.Contrast(1.2);
// Perform OCR on the loaded image
OcrResult result = ocr.Read(inputPhoto);
// Output the text extracted from the receipt
string text = result.Text;
Console.WriteLine(text);
// Extract specific data using OcrResult features
foreach (var line in result.Lines)
{
if (line.Text.Contains("TOTAL"))
{
Console.WriteLine($"Total Found: {line.Text}");
}
}
}
}using IronOcr;
class ReceiptScanner
{
static void Main()
{
// Set the license key for IronOCR
IronOcr.License.LicenseKey = "YOUR-KEY";
// Instantiate OCR engine with optimal settings for receipts
var ocr = new IronTesseract();
// Configure for receipt-specific text
ocr.Configuration.WhiteListCharacters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789.$,- ";
ocr.Configuration.BlackListCharacters = "~`@#%^*_+={}[]|\\:;\"'<>?";
using var inputPhoto = new OcrInput();
inputPhoto.LoadImage("supermarketexample.jpg");
// Apply preprocessing for better accuracy
inputPhoto.DeNoise();
inputPhoto.ToGrayScale();
inputPhoto.Contrast(1.2);
// Perform OCR on the loaded image
OcrResult result = ocr.Read(inputPhoto);
// Output the text extracted from the receipt
string text = result.Text;
Console.WriteLine(text);
// Extract specific data using OcrResult features
foreach (var line in result.Lines)
{
if (line.Text.Contains("TOTAL"))
{
Console.WriteLine($"Total Found: {line.Text}");
}
}
}
}这段代码演示了:
- 导入 IronOcr 库。
- 使用配置选项实例化 OCR 引擎(
IronTesseract)。 - 创建一个新的OcrInput来加载收据图像。
- 应用预处理以提高准确度。
- 使用
Read方法提取文本。 - 使用OcrResult 类处理结构化数据的结果。
如何验证提取数据的准确性?
为确保一致性,检查提取数据的置信度。 IronOCR提供多层次的全面置信度指标:
OcrResult result = ocr.Read(inputPhoto);
string text = result.Text;
Console.WriteLine(text);
Console.WriteLine($"Overall Confidence: {result.Confidence}%");
// Check confidence for individual elements
foreach (var word in result.Words)
{
if (word.Confidence < 80)
{
Console.WriteLine($"Low confidence word: '{word.Text}' ({word.Confidence}%)");
}
}
// Validate numeric values
foreach (var block in result.Blocks)
{
if (block.Text.Contains("$"))
{
Console.WriteLine($"Price detected: {block.Text} (Confidence: {block.Confidence}%)");
}
}OcrResult result = ocr.Read(inputPhoto);
string text = result.Text;
Console.WriteLine(text);
Console.WriteLine($"Overall Confidence: {result.Confidence}%");
// Check confidence for individual elements
foreach (var word in result.Words)
{
if (word.Confidence < 80)
{
Console.WriteLine($"Low confidence word: '{word.Text}' ({word.Confidence}%)");
}
}
// Validate numeric values
foreach (var block in result.Blocks)
{
if (block.Text.Contains("$"))
{
Console.WriteLine($"Price detected: {block.Text} (Confidence: {block.Confidence}%)");
}
}Confidence属性衡量统计准确度,范围从 0(低)到 100(高)。 利用这些置信水平来确定如何处理数据。 对于生产系统,实施进度跟踪以监控 OCR 操作。
如何通过图像预处理提高OCR准确率?
处理图像前,请使用以下方法预处理图像,以获得更好的处理效果:
using var inputPhoto = new OcrInput();
inputPhoto.LoadImage("receipt.jpg");
// Basic preprocessing
inputPhoto.DeNoise(); // Removes noise from the image
inputPhoto.ToGrayScale(); // Converts image to grayscale
inputPhoto.Contrast(1.5); // Enhance contrast for faded receipts
inputPhoto.Sharpen(); // Improve text clarity
// Advanced preprocessing for challenging receipts
inputPhoto.Rotate(2.5); // Correct slight rotation
inputPhoto.Deskew(); // Automatically straighten receipt
inputPhoto.Scale(200); // Upscale low-resolution images
// Handle specific receipt issues
if (receiptIsDamaged)
{
inputPhoto.Dilate(); // Thicken thin text
inputPhoto.Erode(); // Reduce text bleeding
}
// For colored or patterned backgrounds
inputPhoto.Binarize(); // Convert to pure black and white
inputPhoto.Invert(); // Handle white text on dark backgroundusing var inputPhoto = new OcrInput();
inputPhoto.LoadImage("receipt.jpg");
// Basic preprocessing
inputPhoto.DeNoise(); // Removes noise from the image
inputPhoto.ToGrayScale(); // Converts image to grayscale
inputPhoto.Contrast(1.5); // Enhance contrast for faded receipts
inputPhoto.Sharpen(); // Improve text clarity
// Advanced preprocessing for challenging receipts
inputPhoto.Rotate(2.5); // Correct slight rotation
inputPhoto.Deskew(); // Automatically straighten receipt
inputPhoto.Scale(200); // Upscale low-resolution images
// Handle specific receipt issues
if (receiptIsDamaged)
{
inputPhoto.Dilate(); // Thicken thin text
inputPhoto.Erode(); // Reduce text bleeding
}
// For colored or patterned backgrounds
inputPhoto.Binarize(); // Convert to pure black and white
inputPhoto.Invert(); // Handle white text on dark background使用 IronOCR 进行收据处理的主要优势是什么?
收据 OCR 技术可帮助企业和个人进行预算编制、防欺诈和自动数据收集。 IronOCR 具有准确性高、速度快、易于与现有平台集成等优点,是收据扫描解决方案的理想选择。
主要优势包括:
2.导出灵活性:将收据转换为可搜索的 PDF或hOCR HTML ,以便集成到 Web 中。
3.企业功能:部署到Azure 、 Docker和Linux 服务器以实现可扩展性。
常见问题解答
OCR 技术如何用于自动化处理超市收据?
OCR 技术可以通过将扫描的收据转换为数字数据来自动化处理超市收据。使用 IronOCR,可以自动读取收据和提取文本,从而减少人工数据输入的需求并最大程度地减少人为错误。
IronOCR 在处理超市收据方面提供哪些优势?
IronOCR 在处理超市收据方面提供了多个优势,包括跨平台兼容性、支持多种图像格式、一个易于集成的强大 API,以及处理多达 125 种语言的能力,使其非常适合处理国际收据。
如何将 IronOCR 集成到 C# 应用程序中以读取超市收据?
要将 IronOCR 集成到 C# 应用程序中,需要获取许可证密钥,导入 IronOcr 库,并使用 IronTesseract 引擎读取和提取超市收据图像中的文本。
哪些预处理技术可以提高收据扫描中的 OCR 准确性?
IronOCR 提供了如 DeNoise 和 ToGrayScale 的预处理技术来提高 OCR 准确性。这些技术帮助去除图像噪点并将图像转换为灰度,提高了从收据中提取文本的能力。
为什么 OCR 中的置信度测试很重要?如何应用?
在 IronOCR 中,置信度测试很重要,因为它测量提取数据的准确性,值范围从 0 (低)到 1 (高)。它帮助用户评估 OCR 结果的可靠性,并指导数据处理决策。
IronOCR 能处理多语言的超市收据吗?
是的,IronOCR 支持多达 125 种语言的 OCR 处理,能够有效处理多语言的超市收据。
是否有试用版供对 IronOCR 感兴趣的开发者使用?
是的,IronOCR 提供免费试用版供开发人员使用,让他们可以在购买之前探索其功能和能力。
IronOCR 支持哪些平台进行收据扫描?
IronOCR 兼容 .NET 平台,包括 .NET 8, 7, 6, 5 和框架 4.6.2 及以上版本,并支持在 Windows、macOS、Azure 和 Linux 环境中运行。
是什么使 IronOCR 适合集成收据扫描到应用程序中?
IronOCR 适合集成收据扫描到应用程序中,因为其高精度、易用性、跨平台支持以及能够顺利处理各种输入格式和语言。







