# 如何使用IronOCR在C#中閱讀MICR支票
手動處理支票既緩慢又容易出錯。 [IronOCR](https://ironsoftware.com/csharp/ocr/features/)通過專門的引擎簡化了這個工作流程,該引擎能準確讀取MICR(磁性墨水字元識別)線,讓您能夠自動擷取路由號碼、帳號和其他重要資料。
*as-heading:2(快速入門:OCR從支票圖像中讀取MICR)*
使用IronOCR快速抓取MICR線,只需將`result.Text`字串。 對於希望以最少設置獲得可靠金融資料提取的開發者來說,這是完美的選擇。
```cs
:title=Read MICR in One Line with IronOCR
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;
```
<div class="hsg-featured-snippet">
<h3>最小化工作流程 (5 步)</h3>
<ol>
<li><a class="js-modal-open" data-modal-id="trial-license-after-download" href="https://nuget.org/packages/IronOcr">下載C#程式庫以閱讀MICR支票</a></li>
<li>實例化OCR引擎</li>
<li>將<code>Language</code>設置為MICR</li>
<li>使用<code>Read</code>方法從樣本支票圖像中提取資料</li>
<li>存取<strong>OcrResult</strong>屬性以查看和操作提取的資料</li>
</ol>
</div>
## 如何從支票圖像中讀取MICR資料?
使用IronOCR讀取MICR線條非常簡單直觀。首先,將`OcrLanguage.Micr`。 為確保引擎讀取正確的區域,通過在`OcrInput`上設置矩形邊界來指定MICR線位置。
這可以通過選擇x和y座標,以及邊界框矩形的高和寬來實現,然後在調用`Load`方法時將矩形作為第二個參數傳遞。 調用`Read`方法後,僅處理此已定義區域。 MICR語言設定和特定區域的結合保證了IronOCR準確地提取相關金融資訊。
MICR技術使用特殊的磁性墨水和獨特的字體(北美的E-13B),包含精確的14個字元:數字0-9和四個特殊符號。 這些符號包括過渡符號(⑆),用於路由號碼邊界標記; 票據符號(⑈),用於將帳號與其他資料分隔開; 金額符號(⑊),用於編碼金額; 以及短線符號(⑉),用作分隔符。 這種墨水的磁性特性即使在支票被折疊、蓋章或輕微損壞時也能可靠地被讀取,使得MICR非常適合於高量支票處理。
### MICR支票長什麼樣?

### MICR線包含什麼資訊?
**支票號碼**:這個號碼唯一標識了賬戶持有人支票簿中的特定支票。 它作為跟踪個別付款和維護交易記錄的明確參考。 在自動處理系統中,支票號碼有助於防止重複處理並協助對賬程式。
**路由號碼**:這個九位數字程式碼,通過⑆過渡符號封閉,標識持有帳戶的金融機構。 這是結算所用於將支票轉發到正確銀行付款的第一條資訊。 路由號碼遵循特定格式:前四位數字標識聯邦儲備路由符號,接下來的四位標識機構,最後一位是核對碼,用於驗證。
**帳號**:這能標識將從中提取資金的特定客戶帳戶。 其長度可因不同銀行而異,通常範圍在10至12位數字。 銀行可能在帳號結構中包含內部程式碼或分支標識符。
### 我需要什麼程式碼來提取MICR資料?
```csharp
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);
}
```
程式碼展示了[MICR支票處理](https://ironsoftware.com/csharp/ocr/examples/read-micr-cheque/)的完整工作流程。 在運行此程式碼之前,確保您已通過NuGet安裝MICR語言包。 [OcrInput類別](https://ironsoftware.com/csharp/ocr/object-reference/api/IronOcr.OcrInput.html)提供強大的載入和預處理影像的方法,而`Rectangle`參數允許精確定位MICR線位置。
### 應該期望什麼結果?
<div class="content-img-align-center">
<div class="center-image-wrapper" style="width=50%">
<img src="/static-assets/ocr/how-to/read-micr-cheque/micr-output.png" alt="除錯控制台顯示MICR解析結果,提取的過渡、路由和帳號來自支票" class="img-responsive add-shadow" />
</div>
</div>
上面的輸出顯示了從MICR支票中獲得的三個部分:過渡號碼、路由號碼和帳號。 注意MICR特殊符號在輸出中的表示 - 這是正常行為,因為這些符號有特定的Unicode表示,可能在控制台輸出中顯示不同。
#### MICR OCR結果
`OcrResult`物件提供了掃描的詳細資訊:
**文字**:從`OcrInput`中提取的文字。 這包括來自MICR線的所有字元和符號,保持其原始順序。
**信心**:表示每個字元平均的統計準確度信心,1為最高,0為最低。由於標準化字體設計,MICR讀取的信心水平通常高於0.9。 了解更多關於[OCR結果中的信心跟踪](https://ironsoftware.com/csharp/ocr/how-to/tesseract-result-confidence/)。
**塊、段落、行和單詞**:已識別文字的分層結構,對於MICR通常由一行多個由符號分隔的單詞段組成。
**條碼資料**:在處理MICR的同時,IronOCR還可以檢測支票上出現的任何[條碼或QR碼](https://ironsoftware.com/csharp/ocr/examples/csharp-ocr-barcodes/)。
### 我如何驗證正確的OCR區域?
為了確保您選擇了MICR線的正確座標,您可以可視化您定義的**ContentArea**。 一個簡單的方法是將矩形畫在輸入影像上並保存為新文件`StampCropRectangleAndSaveAs`。 這有助於您除錯和微調坐標以獲得最佳性能。
要找到您的`Rectangle`的座標,使用像MS Paint這樣的簡單影像編輯器。 打開您的支票圖像,將滑鼠懸停在MICR線的左上角和右下角,並注意`(x,y)`像素座標。 然後您可以計算矩形的性質:`height = y2-y1`。
要了解更多高級區域選擇技術,請探索[OCR影像區域目標](https://ironsoftware.com/csharp/ocr/how-to/ocr-region-of-an-image/)和[PDF上的內容區域](https://ironsoftware.com/csharp/ocr/troubleshooting/crop-regions-rectangles/)。
這是畫完指定邊界框後,我們的範例支票的輸出圖像。
#### 輸出
<div class="content-img-align-center">
<div class="center-image-wrapper" style="width=50%">
<img src="/static-assets/ocr/how-to/read-micr-cheque/micr-cropped.png" alt="檢查顯示路由號碼、帳號和支票號碼的MICR線,以紅色框突出顯示" class="img-responsive add-shadow" />
</div>
</div>
淡藍色矩形確認我們已正確隔離MICR線以進行處理。
### 效能優化提示
在處理多張支票時,考慮實施[多執行緒以提高效能](https://ironsoftware.com/csharp/ocr/examples/csharp-tesseract-multithreading-for-speed/)。 IronOCR有效地處理並發操作:
```csharp
// 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);
}
});
```
### 常見故障排除場景
**低質量掃描**:如果您處理的是質量不佳的支票影像,請應用[影像預處理過濾器](https://ironsoftware.com/csharp/ocr/examples/ocr-low-quality-scans-tesseract/)以提高可讀性。 MICR字體的鮮明形狀即使在退化的影像中也能幫助保持準確性。
**字元識別不正確**:確保已安裝最新的MICR語言包。 專門的[MICR語言配置](https://ironsoftware.com/csharp/ocr/troubleshooting/financial-language-pack/)專門訓練用於E-13B字體識別。
**MICR線位置多變**:不同銀行可能會將MICR線放置在略有不同的位置。 考慮實施動態區域檢測,或者維護一個不同支票格式座標的配置文件。
### 相關金融文件處理
IronOCR的功能不僅限於支票處理。 探索這些相關功能以實現全面的金融文件自動化:
- [從掃描的發票中提取資料](https://ironsoftware.com/csharp/ocr/examples/read-scanned-document/)
- [處理身份文件](https://ironsoftware.com/csharp/ocr/examples/read-passport/)以達到KYC合規性要求
- [讀取財務報表中的表格](https://ironsoftware.com/csharp/ocr/examples/read-table-in-document/)
通過掌握IronOCR的MICR閱讀,您已經邁出了邁向全面自動化金融文件處理的第一步,大大減少了手動輸入錯誤並顯著加快工作流程。
using IronOcr;using IronSoftware.Drawing;using System;// Create a new instance of IronTesseract for performing OCR operationsIronTesseract ocr = new IronTesseract();// Set the OCR language to MICR to recognize magnetic ink characters// Must have MICR (IronOcr.Languages.MICR) installed beforehandocr.Language = OcrLanguage.MICR;// Specify the file path of the input image containing MICR textusing (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 consoleConsole.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);
}
ImportsIronOcrImportsIronSoftware.DrawingImportsSystem' Create a new instance of IronTesseract for performing OCR operationsDim ocr As New IronTesseract()' Set the OCR language to MICR to recognize magnetic ink characters' Must have MICR (IronOcr.Languages.MICR) installed beforehandocr.Language = OcrLanguage.MICR' Specify the file path of the input image containing MICR textUsing 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 consoleConsole.WriteLine(result.Text) ' Transit number is the first 7 characters of the MICR string Dim transitNum AsString = result.Text.Substring(0, 7) ' Routing number starts from the 8th character and is 11 characters long Dim routingNum AsString = result.Text.Substring(7, 11) ' Account number starts from the 22nd character to the end of the string Dim accountNum AsString = result.Text.Substring(22)EndUsing
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
// Process multiple cheques in parallelvar 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);
}
});
ImportsSystem.IOImportsSystem.Threading.TasksImportsIronOcr' Process multiple cheques in parallelDim chequeFiles = Directory.GetFiles("cheques/", "*.png")Parallel.ForEach(chequeFiles, Sub(file)Using ocr As New IronTesseractWith {.Language = OcrLanguage.MICR}Using input As New OcrInput() input.LoadImage(file, micrRegion) Dim result = ocr.Read(input)ProcessMicrData(result.Text)EndUsingEndUsingEnd 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)
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.