如何使用IronOCR在C#中讀取MICR支票
人工處理支票速度慢,也容易出錯。IronOCR 透過專用引擎準確讀取 MICR(磁性墨水字元辨識)資料列,讓您自動擷取銀行路由號碼、帳號和其他重要資料。
快速入門:以 OCR 讀取支票影像中的 MICR 資料使用 IronOCR,就能快速讀取 MICR 資料列。將 Language 設為 MICR,指定 MICR 文字所在的矩形區域,再執行 Read(),即可取得 result.Text 字串。開發人員只需少量設定,就能可靠地擷取財務資料。
-
1使用 IronOCR 安裝 NuGet Package Manager
-
2複製並運行這段程式碼。
var input = new IronOcr.OcrInput(); input.LoadImage("micr.png", new IronSoftware.Drawing.Rectangle(125, 240, 310, 15)); string micrText = new IronOcr.IronTesseract { Language = IronOcr.OcrLanguage.MICR }.Read(input).Text;Dim input As New IronOcr.OcrInput() input.LoadImage("micr.png", New IronSoftware.Drawing.Rectangle(125, 240, 310, 15)) Dim micrText As String = New IronOcr.IronTesseract With {.Language = IronOcr.OcrLanguage.MICR}.Read(input).TextDim input As New IronOcr.OcrInput() input.LoadImage("micr.png", New IronSoftware.Drawing.Rectangle(125, 240, 310, 15)) Dim micrText As String = New IronOcr.IronTesseract With {.Language = IronOcr.OcrLanguage.MICR}.Read(input).Text -
3部署到您的生產環境進行測試
立即在您的項目中開始使用 IronOCR,並使用 免費試用
基本工作流程(5 個步驟)
- 下載讀取MICR支票的C#程式庫
- 建立 OCR 引擎執行個體
- 將
Language設為 MICR - 使用
Read方法從支票樣本影像中擷取資料 - 存取OcrResult屬性以查看和操作擷取的資料
如何從支票影像中讀取MICR資料?
使用 IronOCR 讀取 MICR 資料列時,請先將 IronTesseract 執行個體的 Language 屬性設為 OcrLanguage.Micr。接著在 OcrInput 中以矩形框指定 MICR 資料列的位置,讓引擎讀取正確的區域。
先指定矩形框的 x、y 座標及高度和寬度,再於呼叫 Load 方法時將矩形框當作第二個參數傳入。呼叫 Read 方法後,IronOCR 只會處理指定區域。搭配 MICR 語言設定,即可準確擷取相關財務資訊。
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);
}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 UsingImports 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 為最低。由於 E-13B 採用標準化的字型設計,MICR 的辨識信心度通常較高。請參閱如何追蹤 OCR 結果的信心度。
區塊、段落、行和單字: 辨識結果的文字階層。在 MICR 中通常只有一行,包含多個以符號分隔的文字片段。
條碼資料:處理MICR時,IronOCR可以同時偵測支票上的任何條碼或 QR Code。
如何驗證正確的OCR區域?
若要確認 MICR 資料列的座標是否正確,可以將定義好的 ContentArea 視覺化。做法是在輸入影像上畫出矩形,再用 StampCropRectangleAndSaveAs 另存新檔。這樣有助於除錯和微調座標,以達到最佳效能。
您可以用 MS Paint 等簡單的影像編輯器取得 Rectangle 的座標。開啟支票影像,將滑鼠游標分別移到 MICR 資料列的左上角和右下角,記下像素座標 (x,y)。接著計算矩形的屬性 (x1, y1, width, height),其中 width = x2-x1,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);
}
});// 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)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 是磁性墨水字符識別的縮寫,是支票處理中使用的技術。它使用特殊的磁性墨水和獨特的字體(E-13B)來對支票上的詳情進行編碼,如路由號和帳戶號。IronOCR 可以準確讀取這些詳情來自動化提取過程。
如何設置 IronOCR 以從支票中讀取 MICR 行?
要使用 IronOCR 讀取 MICR 行,您需要將 IronTesseract 的 Language 屬性設置為 OcrLanguage.MICR。使用 OcrInput 類加載包含 MICR 文本的圖像,指定 MICR 行的矩形區域,並使用 Read() 方法提取文本。
MICR 行包含什麼類型的信息呢?
MICR 行通常包括支票號、路由號和帳戶號。路由號標識金融機構,帳戶號指定客戶的帳戶,支票號是每張支票的唯一編號。
IronOCR 能夠處理支票上 MICR 行位置的變化嗎?
是的,IronOCR 允許您指定要讀取的精確矩形區域,這樣可以處理不同支票格式中 MICR 行位置的變化。您還可以實施動態區域檢測以獲得額外的靈活性。
如何提高低質量支票掃描的 OCR 準確性?
對於低質量的掃描,您可以應用 IronOCR 提供的圖像預處理過濾器,以在提取之前提高 MICR 行的可讀性。這些過濾器可以顯著提高 OCR 的準確性。
使用 IronOCR 進行支票處理有什麼好處?
IronOCR 自動讀取 MICR 行,減少了手動數據輸入錯誤並加快了支票處理工作流程。由於其專門的 MICR 引擎,即使是輕微損壞的支票也能提供高準確性。
IronOCR 是否支持多線程處理多個支票?
是的,IronOCR 支持多線程,允許您同時處理多個支票。此功能可以顯著提高性能,尤其是在高容量環境中。
IronOCR 還提供哪些其他金融文件處理能力?
除了 MICR 讀取外,IronOCR 還可以從掃描的發票中提取數據,處理身份證件以滿足 KYC 合規,並讀取金融報表中的表格,提供全面的金融文件自動化支持。
如何使用 IronOCR 驗證提取的 MICR 數據?
IronOCR 提供了一個 OcrResult 對象,包括提取的文本、置信度等數據,如單詞和行。您還可以在輸入圖像上可視化目標的 OCR 區域以確保正確性。
如果 IronOCR 錯誤識別了 MICR 行中的字符,該怎麼辦?
確保 MICR 語言包是最新的並正確安裝。IronOCR 中的特殊 MICR 語言配置專為識別 MICR 行中使用的 E-13B 字體而設計。
Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。