如何使用 C# 中的 IronOCR 阅读 MICR 支票
人工处理支票既慢又容易出错。 IronOCR通过专门的引擎简化了这一工作流程,该引擎可准确读取 MICR(磁性墨水字符识别)线,让您自动提取路由号码、帐号和其他关键数据。
快速开始:OCR 从支票图像读取 MICR使用IronOCR快速读取MICR行——只需将result.Text字符串。 非常适合希望以最少的设置提取可靠财务数据的开发人员。
-
1Install IronOCR with NuGet Package Manager
-
2复制并运行这段代码。
string micrText = new IronOcr.IronTesseract { Language = IronOcr.OcrLanguage.MICR }.Read(new IronOcr.OcrInput().LoadImage("micr.png", new System.Drawing.Rectangle(125, 240, 310, 15))).Text;C# -
3部署到您的生产环境中进行测试
通过免费试用立即在您的项目中开始使用IronOCR
最小工作流程(5 个步骤)
- 下载用于读取 MICR 支票的 C# 库
- 实例化 OCR 引擎
- 将
Language设置设为 MICR - 使用
Read方法从样本支票图像中提取数据 - 访问OcrResult属性以查看和操作提取的数据
如何从支票图像中读取 MICR 数据?
使用IronOCR读取MICR行简单而直观。首先将OcrLanguage.Micr。 为确保引擎读取正确的区域,请通过在OcrInput上设置矩形边界来指定MICR行的位置。
这是通过选择x和y坐标以及边界框矩形的高度和宽度来实现的,然后在调用Load方法时将矩形作为第二个参数传递。 然后调用Read方法仅处理此定义的区域。 MICR 语言设置与特定区域的这种结合保证了 IronOCR 能够准确提取相关的财务信息。
MICR 技术使用特殊的磁性墨水和独特的字体(北美为 E-13B),其中包含 14 个字符:数字 0-9 和四个特殊符号。 这些符号包括中转符号 (⑆),用于标记路由编号的边界; on-us 符号 (⑈),用于将帐号与其他数据分开; 金额符号 (⑊),用于编码金额; 以及作为分隔符的破折号 (⑉)。 这种油墨的磁性使其即使在支票折叠、盖章或轻微损坏的情况下也能可靠读取,因此 MICR 是大批量支票处理的理想选择。
MICR 支票看起来像什么?

MICR 行包含哪些信息?
支票号码:此号码用于唯一标识账户持有人支票簿中的特定支票。 它为追踪个人付款和维护交易记录提供了清晰的参考依据。 在自动处理系统中,支票号码有助于防止重复处理并协助对账程序。
路由号码:这个九位数的代码,用⑆符号括起来,用于识别持有该账户的金融机构。 这是清算机构用来将支票发送到正确银行进行付款的第一条信息。 路由号码遵循特定的格式:前四位数字表示美联储路由符号,后四位数字表示机构,最后一位数字是校验和,用于验证。
账号:用于识别将从中提取资金的特定客户账户。 其长度因银行而异,通常在 10 到 12 位数之间。 银行可能会在账号结构中包含内部代码或分支机构标识符。
提取 MICR 数据需要哪些代码?
using IronOcr;
using IronSoftware.Drawing;
using System;
// Create a new instance of IronTesseract for performing OCR operations
IronTesseract ocr = new IronTesseract();
// Set the OCR language to MICR to recognize magnetic ink characters
// Must have MICR (IronOcr.Languages.MICR) installed beforehand
ocr.Language = OcrLanguage.MICR;
// Specify the file path of the input image containing MICR text
using (var input = new OcrInput())
{
// Specify the MICR of the image to focus on for OCR (coordinates in pixels)
var contentArea = new Rectangle(x: 215, y: 482, width: 520, height: 20);
input.LoadImage("micr.png", contentArea);
// Optional: Save the cropped area for verification
input.StampCropRectangleAndSaveAs(contentArea, Color.Aqua, "cropped.png");
// Run the OCR engine to read the MICR text from the input image
var result = ocr.Read(input);
// Output the recognized text to the console
Console.WriteLine(result.Text);
// Transit number is the first 7 characters of the MICR string
string transitNum = result.Text.Substring(0, 7);
// Routing number starts from the 8th character and is 11 characters long
string routingNum = result.Text.Substring(7, 11);
// Account number starts from the 22nd character to the end of the string
string accountNum = result.Text.Substring(22);
}Imports IronOcr
Imports IronSoftware.Drawing
Imports System
' Create a new instance of IronTesseract for performing OCR operations
Dim ocr As New IronTesseract()
' Set the OCR language to MICR to recognize magnetic ink characters
' Must have MICR (IronOcr.Languages.MICR) installed beforehand
ocr.Language = OcrLanguage.MICR
' Specify the file path of the input image containing MICR text
Using input As New OcrInput()
' Specify the MICR of the image to focus on for OCR (coordinates in pixels)
Dim contentArea As New Rectangle(x:=215, y:=482, width:=520, height:=20)
input.LoadImage("micr.png", contentArea)
' Optional: Save the cropped area for verification
input.StampCropRectangleAndSaveAs(contentArea, Color.Aqua, "cropped.png")
' Run the OCR engine to read the MICR text from the input image
Dim result = ocr.Read(input)
' Output the recognized text to the console
Console.WriteLine(result.Text)
' Transit number is the first 7 characters of the MICR string
Dim transitNum As String = result.Text.Substring(0, 7)
' Routing number starts from the 8th character and is 11 characters long
Dim routingNum As String = result.Text.Substring(7, 11)
' Account number starts from the 22nd character to the end of the string
Dim accountNum As String = result.Text.Substring(22)
End Using代码演示了MICR 支票处理的完整工作流程。 在运行此代码之前,请确保您已通过 NuGet 安装了 MICR 语言包。 OcrInput类提供了强大的方法,用于加载和预处理图像,而Rectangle参数允许精确定位MICR行位置。
我应该期待什么样的结果?

上面的输出显示了从 MICR 支票中获得的三个部分:过境号码、路由号码和账号。 请注意特殊 MICR 符号在输出中的表示方式--这是正常现象,因为这些符号有特定的 Unicode 表示法,在控制台输出中可能会有不同的显示方式。
MICR OCR 结果
OcrResult对象提供扫描的详细信息:
文本:来自OcrInput的提取文本。 这包括 MICR 行中的所有字符和符号,并保持其原始顺序。
置信度:表示平均每个字符的统计准确性置信度,1 为最高,0 为最低。对于 MICR 读取,由于采用标准化字体设计,置信度通常高于 0.9。 了解有关跟踪 OCR 结果的更多信息。
块、段、行和单词:识别文本的层次结构,对于 MICR 而言,通常由单行和由符号分隔的多个单词段组成。
条形码数据:在处理 MICR 时,IronOCR 可同时检测支票上存在的任何 条形码或 QR 码。
如何验证 OCR 区域的正确性?
为了确保您已为 MICR 线选择了正确的坐标,您可以可视化您定义的ContentArea 。 一种简单的方法是在输入图像上绘制矩形并用StampCropRectangleAndSaveAs保存为新文件。 这有助于您调试和微调坐标,以获得最佳性能。
要查找您的Rectangle的坐标,请使用简单的图像编辑器,如MS Paint。 打开您的支票图像,将鼠标悬停在MICR行的左上角和右下角,并记下(x,y)像素坐标。 然后您可以计算矩形的属性:height = y2-y1。
有关更高级的区域选择技术,请浏览 OCR 区域定位 和 PDF 内容区域。
这是在示例支票上绘制指定边界框后的输出图像。
输出

浅蓝色矩形表示我们已经正确分离出 MICR 线进行处理。
性能优化技巧
在处理多个检查时,请考虑实施多线程以提高性能。 IronOCR 可高效处理并发操作:
// Process multiple cheques in parallel
var chequeFiles = Directory.GetFiles("cheques/", "*.png");
Parallel.ForEach(chequeFiles, file =>
{
using (var ocr = new IronTesseract { Language = OcrLanguage.MICR })
using (var input = new OcrInput())
{
input.LoadImage(file, micrRegion);
var result = ocr.Read(input);
ProcessMicrData(result.Text);
}
});Imports System.IO
Imports System.Threading.Tasks
Imports IronOcr
' Process multiple cheques in parallel
Dim chequeFiles = Directory.GetFiles("cheques/", "*.png")
Parallel.ForEach(chequeFiles, Sub(file)
Using ocr As New IronTesseract With {.Language = OcrLanguage.MICR}
Using input As New OcrInput()
input.LoadImage(file, micrRegion)
Dim result = ocr.Read(input)
ProcessMicrData(result.Text)
End Using
End Using
End Sub)常见故障排除场景
低质量扫描:如果您使用的是质量较差的检查图像,请应用图像预处理过滤器以提高可读性。 MICR 字体的独特形状有助于在图像质量下降的情况下保持准确性。
字符识别错误:确保您安装了最新的 MICR 语言包。 专门的MICR 语言配置经过专门训练,可识别 E-13B 字体。
可变的 MICR 线位置:不同银行的 MICR 线位置可能略有不同。 考虑实施动态区域检测或维护一个包含不同检查格式坐标的配置文件。
相关财务文档处理
IronOCR 的功能不仅限于检查处理。 探索这些相关功能,实现全面的财务文档自动化:
- 从扫描发票中提取数据
- 处理身份证件以符合 KYC 合规性要求
- 阅读财务报表中的表格
通过 IronOCR 掌握 MICR 读取,您就迈出了实现全自动财务文档处理的第一步,减少了手动输入错误,大大加快了工作流程。
常见问题解答
什么是 MICR,为什么它对支票处理很重要?
MICR(磁性墨水字符识别)是一种使用特殊磁性墨水和独特字体对支票上的金融信息进行编码的技术。IronOCR 的专用 MICR 引擎可以准确读取这些编码数据,包括路由号码、账号和支票号码,将原本缓慢且容易出错的人工流程自动化。
如何配置 OCR 引擎以读取 MICR 文本?
要使用 IronOCR 阅读 MICR 文本,需要将 IronTesseract 实例的语言属性设置为 OcrLanguage.MICR。这样,引擎就会使用专门为读取支票上使用的 E-13B 字体而设计的 MICR 字符识别算法。
我能否明确指定在支票的哪个位置查找 MICR 数据?
是的,IronOCR 允许您通过在 OcrInput 上设置矩形边界来指定 MICR 线的准确位置。您可以通过选择 x 和 y 坐标以及边界框矩形的高度和宽度来定义,然后在调用加载方法时将其作为第二个参数传递。
MICR 编码中使用了哪些特殊字符?
MICR 共使用 14 个字符:数字 0-9 和四个特殊符号。其中包括用于路由号码边界的中转符号 (⑆)、用于分隔账号的 on-us 符号 (⑈)、用于编码金额的金额符号 (⑊),以及作为分隔符的破折号 (⑉)。IronOCR 可以识别所有这些 MICR 专用字符。
如何快速从支票图像中提取 MICR 数据?
using IronOCR,只需一行代码就能提取 MICR 数据。只需创建一个语言设置为 MICR 的 IronTesseract 实例,加载指定了 MICR 区域的图片,调用 Read(),然后访问 result.Text 属性,即可立即获得提取的 MICR 字符串。
可以从 MICR 行中提取哪些类型的财务信息?
IronOCR 可以提取 MICR 行中编码的所有关键财务数据,包括路由号(识别银行)、账号(识别特定账户)和支票号码(唯一识别单张支票)。这种自动提取可简化金融文档处理。
Does IronOCR support multithreading for processing multiple checks?
Yes, IronOCR supports multithreading, allowing you to process multiple checks simultaneously. This feature can significantly improve performance, especially in high-volume environments.
What other financial document processing capabilities does IronOCR offer?
Beyond MICR reading, IronOCR can extract data from scanned invoices, process identity documents for KYC compliance, and read tables in financial statements, providing comprehensive automation for financial documents.
How can I verify the extracted MICR data using IronOCR?
IronOCR provides an `OcrResult` object that includes the extracted text, confidence levels, and additional data like words and lines. You can also visualize the targeted OCR region on the input image to ensure correctness.
What should I do if IronOCR incorrectly recognizes characters in the MICR line?
Ensure that the MICR language pack is up to date and correctly installed. The specialized MICR language configuration in IronOCR is specifically designed for recognizing the E-13B font used in MICR lines.

Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。