開始使用 C# 和 VB.NET 進行 OCR

This article was translated from English: Does it need improvement?
Translated
View the article in English

IronOCR 是一個 C# 軟體庫,可讓 .NET 平台軟體開發人員識別和讀取影像和 PDF 文件中的文字。 這是一個純 .NET OCR 函式庫,使用已知最先進的 Tesseract 引擎。

安裝

使用 NuGet 套件管理員安裝

在 Visual Studio 中或使用 NuGet 套件管理器在命令列中安裝 IronOcr。 在 Visual Studio 中,使用下列命令導覽至控制台:

  • 工具 ->
  • NuGet 套件管理器 ->
  • 軟體套件管理器控制台
Install-Package IronOcr

請造訪NuGet 查看 IronOcr ,以了解更多關於版本更新和安裝的資訊。

還有其他適用於不同平台的IronOCR NuGet 套件

IronOcr.Extensions.AdvancedScan for Linux and macOS

此軟體包適用於使用 Linux 和 Mac 且希望使用 IronOcr 高級功能的使用者。

故障排除

透過此次軟體包的更新,IronOCR 將 OpenCV 依賴項合併到軟體包中,從而簡化了軟體包。如果目前匯入 OpenCV 相依性的開發人員遇到下列錯誤,則會出現此問題。

The type of namespace name `OpenCvSharp` could not be found(are you missing a using directive or an assembly reference)

您可以安全地移除 OpenCV 命名空間,問題即可解決。

下載 IronOCR .ZIP 文件

您也可以選擇透過 .ZIP 檔案下載 IronOCR。 點擊即可直接下載 DLL 檔案。 下載完 .zip 檔案後:

.NET Framework 4.0+ 安裝說明:

將 net40 資料夾中的 IronOcr.dll 檔案新增至您的專案中 然後加入程式集引用到:

  • 系統配置
  • 系統繪圖
  • 系統.Web

適用於 .NET Standard、.NET Core 2.0+ 和 .NET 5 的說明

將 netstandard2.0 資料夾中的 IronOcr.dll 檔案新增至您的專案中 然後加入 NuGet 套件引用:

  • 系統繪圖.Common 4.7 或更高版本

下載 IronOCR 安裝程式(僅限 Windows 系統)

另一個選擇是下載我們的 IronOCR 安裝程序,它將安裝 IronOCR 開箱即用所需的所有資源。 請注意,此選項僅適用於 Windows 系統。 要下載安裝程序,請點擊這裡。 下載完 .zip 檔案後:

.NET Framework 4.0+ 安裝說明:

將 net40 資料夾中的 IronOcr.dll 檔案新增至您的專案中 然後加入程式集引用到:

  • 系統配置
  • 系統繪圖
  • 系統.Web

適用於 .NET Standard、.NET Core 2.0+ 和 .NET 5 的說明

將 netstandard2.0 資料夾中的 IronOcr.dll 檔案新增至您的專案中 然後加入 NuGet 套件引用:

  • 系統繪圖.Common 4.7 或更高版本

為什麼選擇 IronOCR?

IronOCR 是一個易於安裝、功能齊全且文件完善的 .NET 軟體庫。

選擇 IronOCR,無需使用任何外部網路服務、無需支付持續費用或透過網路發送機密文件,即可實現99.8% 以上的 OCR 準確率

為什麼 C# 開發人員選擇 IronOCR 而不是原生 Tesseract:

  • 可以安裝為單一 DLL 檔案或 NuGet 檔案。
  • 包裝盒內含 Tesseract 5、4 和 3 引擎。
  • 準確率高達99.8% ,遠超普通 Tesseract。
  • 極速和多線程
  • 相容於 MVC、WebApp、桌面、控制台和伺服器應用程式
  • 無需處理任何可執行檔或 C++ 程式碼
  • 完全支援 PDF OCR
  • 對幾乎任何影像檔案或 PDF 檔案執行 OCR 識別
  • 完全支援 .NET Core、Standard 和 Framework
  • 支援部署在 Windows、Mac、Linux、Azure、Docker、Lambda 和 AWS 上
  • 讀取條碼與二維碼
  • 將 OCR 結果匯出為 XHTML
  • 將 OCR 資訊匯出為可搜尋的 PDF 文檔
  • 多線程支持
  • 125 種國際語言,全部透過 NuGet 或 OcrData 檔案進行管理
  • 提取圖像、座標、統計資料和字體。 不僅僅是文字。
  • 可用於在商業和專有應用程式中重新分發 Tesseract OCR。

IronOCR 在處理真實世界的影像和不完美的文件(例如照片或低解析度掃描件,這些文件可能有數位雜訊或瑕疵)時表現出色。

其他適用於 .NET 平台的免費 OCR函式庫,例如其他 .NET Tesseract API 和 Web 服務,在這些實際用例中表現並不理想。

使用 Tesseract 5 進行 OCR - 開始使用 C# 編碼

下面的程式碼範例展示了使用 C# 或 VB .NET 從圖像中讀取文字是多麼容易。

一句話

:path=/static-assets/ocr/content-code-examples/get-started/get-started-1.cs
string Text = new IronTesseract().Read(@"img\Screenshot.png").Text;
Dim Text As String = (New IronTesseract()).Read("img\Screenshot.png").Text
$vbLabelText   $csharpLabel

可配置的 Hello World

:path=/static-assets/ocr/content-code-examples/get-started/get-started-2.cs
using IronOcr;

IronTesseract ocr = new IronTesseract();
using OcrInput input = new OcrInput();

// Add multiple images
input.LoadImage("images/sample.jpeg");

OcrResult result = ocr.Read(input);
Console.WriteLine(result.Text);
Imports IronOcr

Private ocr As New IronTesseract()
Private OcrInput As using

' Add multiple images
input.LoadImage("images/sample.jpeg")

Dim result As OcrResult = ocr.Read(input)
Console.WriteLine(result.Text)
$vbLabelText   $csharpLabel

C# PDF OCR

同樣的方法也可用於從任何 PDF 文件中提取文字。

:path=/static-assets/ocr/content-code-examples/get-started/get-started-3.cs
using IronOcr;

IronTesseract ocr = new IronTesseract();
using OcrInput input = new OcrInput();

// We can also select specific PDF page numbers to OCR
input.LoadPdf("example.pdf", Password: "password");

OcrResult result = ocr.Read(input);
Console.WriteLine(result.Text);

// 1 page for every page of the PDF
Console.WriteLine($"{result.Pages.Length} Pages");
Imports IronOcr

Private ocr As New IronTesseract()
Private OcrInput As using

' We can also select specific PDF page numbers to OCR
input.LoadPdf("example.pdf", Password:= "password")

Dim result As OcrResult = ocr.Read(input)
Console.WriteLine(result.Text)

' 1 page for every page of the PDF
Console.WriteLine($"{result.Pages.Length} Pages")
$vbLabelText   $csharpLabel

多頁TIFF檔案的OCR識別

:path=/static-assets/ocr/content-code-examples/get-started/get-started-4.cs
using IronOcr;

IronTesseract ocr = new IronTesseract();
using OcrInput input = new OcrInput();
var pageindices = new int[] { 1, 2 };
input.LoadImageFrames("multi-frame.tiff", pageindices);
OcrResult result = ocr.Read(input);
Console.WriteLine(result.Text);
Imports IronOcr

Private ocr As New IronTesseract()
Private OcrInput As using
Private pageindices = New Integer() { 1, 2 }
input.LoadImageFrames("multi-frame.tiff", pageindices)
Dim result As OcrResult = ocr.Read(input)
Console.WriteLine(result.Text)
$vbLabelText   $csharpLabel

條碼和二維碼

IronOCR 的一個獨特之處在於,它在掃描文字的同時,還可以讀取文件中的條碼和二維碼。 OcrResult.OcrBarcode類別的實例為開發人員提供有關每個掃描條碼的詳細資訊。

:path=/static-assets/ocr/content-code-examples/get-started/get-started-5.cs
using IronOcr;

IronTesseract ocr = new IronTesseract();
ocr.Configuration.ReadBarCodes = true;

using OcrInput input = new OcrInput();
input.LoadImage("img/Barcode.png");

OcrResult Result = ocr.Read(input);
foreach (var Barcode in Result.Barcodes)
{
    // type and location properties also exposed
    Console.WriteLine(Barcode.Value);
}
Imports IronOcr

Private ocr As New IronTesseract()
ocr.Configuration.ReadBarCodes = True

Using input As New OcrInput()
	input.LoadImage("img/Barcode.png")
	
	Dim Result As OcrResult = ocr.Read(input)
	For Each Barcode In Result.Barcodes
		' type and location properties also exposed
		Console.WriteLine(Barcode.Value)
	Next Barcode
End Using
$vbLabelText   $csharpLabel

對影像特定區域進行OCR識別

IronOCR 的所有掃描和讀取方法都允許我們精確指定要從頁面的哪個部分讀取文字。 這在處理標準化表格時非常有用,可以節省大量時間並提高效率。

要使用裁切區域,我們需要新增對System.Drawing系統引用,以便我們可以使用System.Drawing.Rectangle物件。

:path=/static-assets/ocr/content-code-examples/get-started/get-started-6.cs
using IronOcr;

IronTesseract ocr = new IronTesseract();
using OcrInput input = new OcrInput();

// Dimensions are in pixel
var contentArea = new System.Drawing.Rectangle() { X = 215, Y = 1250, Height = 280, Width = 1335 };

input.LoadImage("document.png", contentArea);

OcrResult result = ocr.Read(input);
Console.WriteLine(result.Text);
Imports IronOcr

Private ocr As New IronTesseract()
Private OcrInput As using

' Dimensions are in pixel
Private contentArea = New System.Drawing.Rectangle() With {
	.X = 215,
	.Y = 1250,
	.Height = 280,
	.Width = 1335
}

input.LoadImage("document.png", contentArea)

Dim result As OcrResult = ocr.Read(input)
Console.WriteLine(result.Text)
$vbLabelText   $csharpLabel

低品質掃描的光學字元識別

IronOCR OcrInput類別可以修復普通 Tesseract 無法讀取的掃描件。

:path=/static-assets/ocr/content-code-examples/get-started/get-started-7.cs
using IronOcr;

IronTesseract ocr = new IronTesseract();
using OcrInput input = new OcrInput();
var pageindices = new int[] { 1, 2 };
input.LoadImageFrames(@"img\Potter.tiff", pageindices);

// fixes digital noise and poor scanning
input.DeNoise();

// fixes rotation and perspective
input.Deskew();

OcrResult result = ocr.Read(input);
Console.WriteLine(result.Text);
Imports IronOcr

Private ocr As New IronTesseract()
Private OcrInput As using
Private pageindices = New Integer() { 1, 2 }
input.LoadImageFrames("img\Potter.tiff", pageindices)

' fixes digital noise and poor scanning
input.DeNoise()

' fixes rotation and perspective
input.Deskew()

Dim result As OcrResult = ocr.Read(input)
Console.WriteLine(result.Text)
$vbLabelText   $csharpLabel

將 OCR 結果匯出為可搜尋 PDF

:path=/static-assets/ocr/content-code-examples/get-started/get-started-8.cs
using IronOcr;

IronTesseract ocr = new IronTesseract();
using OcrInput input = new OcrInput();
input.Title = "Quarterly Report";
input.LoadImage("image1.jpeg");
input.LoadImage("image2.png");
var pageindices = new int[] { 1, 2 };
input.LoadImageFrames("image3.gif", pageindices);

OcrResult result = ocr.Read(input);
result.SaveAsSearchablePdf("searchable.pdf");
Imports IronOcr

Private ocr As New IronTesseract()
Private OcrInput As using
input.Title = "Quarterly Report"
input.LoadImage("image1.jpeg")
input.LoadImage("image2.png")
Dim pageindices = New Integer() { 1, 2 }
input.LoadImageFrames("image3.gif", pageindices)

Dim result As OcrResult = ocr.Read(input)
result.SaveAsSearchablePdf("searchable.pdf")
$vbLabelText   $csharpLabel

TIFF 轉可搜尋 PDF

:path=/static-assets/ocr/content-code-examples/get-started/get-started-9.cs
using IronOcr;

IronTesseract ocr = new IronTesseract();
using OcrInput input = new OcrInput();
var pageindices = new int[] { 1, 2 };
input.LoadImageFrames("example.tiff", pageindices);
ocr.Read(input).SaveAsSearchablePdf("searchable.pdf");
Imports IronOcr

Private ocr As New IronTesseract()
Private OcrInput As using
Private pageindices = New Integer() { 1, 2 }
input.LoadImageFrames("example.tiff", pageindices)
ocr.Read(input).SaveAsSearchablePdf("searchable.pdf")
$vbLabelText   $csharpLabel

將 OCR 結果匯出為 HTML

:path=/static-assets/ocr/content-code-examples/get-started/get-started-10.cs
using IronOcr;

IronTesseract ocr = new IronTesseract();
using OcrInput input = new OcrInput();
input.Title = "Html Title";
input.LoadImage("image1.jpeg");

OcrResult Result = ocr.Read(input);
Result.SaveAsHocrFile("results.html");
Imports IronOcr

Private ocr As New IronTesseract()
Private OcrInput As using
input.Title = "Html Title"
input.LoadImage("image1.jpeg")

Dim Result As OcrResult = ocr.Read(input)
Result.SaveAsHocrFile("results.html")
$vbLabelText   $csharpLabel

OCR影像增強濾鏡

IronOCR 為OcrInput物件提供獨特的過濾器,以提高 OCR 性能。

影像增強程式碼範例

:path=/static-assets/ocr/content-code-examples/get-started/get-started-11.cs
using IronOcr;

IronTesseract ocr = new IronTesseract();
using OcrInput input = new OcrInput();
input.LoadImage("LowQuality.jpeg");

// fixes digital noise and poor scanning
input.DeNoise();

// fixes rotation and perspective
input.Deskew();

OcrResult result = ocr.Read(input);
Console.WriteLine(result.Text);
Imports IronOcr

Private ocr As New IronTesseract()
Private OcrInput As using
input.LoadImage("LowQuality.jpeg")

' fixes digital noise and poor scanning
input.DeNoise()

' fixes rotation and perspective
input.Deskew()

Dim result As OcrResult = ocr.Read(input)
Console.WriteLine(result.Text)
$vbLabelText   $csharpLabel

OCR影像濾鏡列表

IronOCR 內建的用於增強 OCR 性能的輸入過濾器包括:

  • OcrInput.Rotate(double degrees) - 將影像順時針旋轉指定角度。逆時針旋轉請使用負數。
  • OcrInput.Binarize() - 此濾鏡將每個像素轉換為黑色或白色,沒有中間色,可能會提高低對比度影像的 OCR 效能。
  • OcrInput.ToGrayScale() - 將每個像素轉換為灰階陰影。 它可能不會提高準確率,但可以提高速度。
  • OcrInput.Contrast() - 自動增加對比度,通常可以提高低對比度掃描的速度和準確性。
  • OcrInput.DeNoise() - 去除數位噪聲,僅在預期會有噪音時建議使用。
  • OcrInput.Invert() - 反轉所有顏色(白色變為黑色,反之亦然)。
  • OcrInput.Dilate() - 推進形態學,向物件邊界添加像素,與侵蝕相反。
  • OcrInput.Erode() - 推進形態學,從物件邊界移除像素,與膨脹相反。
  • OcrInput.Deskew() - 旋轉影像以使其方向正確。 由於 Tesseract 的傾斜容差有限,因此這種方法很有用。
  • OcrInput.EnhanceResolution - 提高低品質影像的解析度。 此設定通常用於自動管理低 DPI 輸入。
  • EnhanceResolution可偵測低解析度影像(低於 275 dpi),將其放大,並銳化文字以獲得更好的 OCR 效果。 雖然耗時,但通常可以減少 OCR 的整體操作時間。
  • Language - 支援從 22 種國際語言包中進行選擇。
  • Strategy - 允許根據字詞的統計關係,在快速、準確度較低或更高級(使用 AI 提高準確度)的策略之間進行選擇。
  • ColorSpace - 選擇以灰階或彩色進行 OCR; 雖然在某些對比度場景下彩色顯示效果可能更好,但灰階顯示通常是最佳選擇。
  • DetectWhiteTextOnDarkBackgrounds - 調整負片影像,自動偵測並讀取深色背景上的白色文字。
  • InputImageType - 指導 OCR 函式庫,指定它是在處理整個文件還是片段。
  • RotateAndStraighten - 允許 IronOCR 正確處理旋轉或受透視變形影響的文件。
  • ReadBarcodes - 無需額外花費大量時間,即可自動讀取條碼和二維碼,同時進行文字掃描。
  • ColorDepth - 確定 OCR 過程中顏色深度的每個像素位數。 更深的加工深度可以提高質量,但也會延長加工時間。

125 種語言包

IronOCR 透過語言包支援125 種國際語言,這些語言套件以 DLL 的形式分發,可從此網站NuGet 套件管理器下載。

語言包括德語、法語、英語、中文、日語等。 針對 MRZ、MICR 檢查、金融數據、車牌等,有專門的語言包。此外,還可以使用自訂的 Tesseract".traineddata"檔案。

語言範例

// Reference to the path of the source file that demonstrates setting language packs for OCR
:path=/static-assets/ocr/content-code-examples/get-started/get-started-12.cs
// Reference to the path of the source file that demonstrates setting language packs for OCR
using IronOcr;

// PM> Install IronOcr.Languages.Arabic
IronTesseract ocr = new IronTesseract();
ocr.Language = OcrLanguage.Arabic;

using OcrInput input = new OcrInput();

var pageindices = new int[] { 1, 2 };
input.LoadImageFrames("img/arabic.gif", pageindices);
// Add image filters if needed
// In this case, even thought input is very low quality
// IronTesseract can read what conventional Tesseract cannot.
OcrResult result = ocr.Read(input);
// Console can't print Arabic on Windows easily.
// Let's save to disk instead.
result.SaveAsTextFile("arabic.txt");
' Reference to the path of the source file that demonstrates setting language packs for OCR
Imports IronOcr

' PM> Install IronOcr.Languages.Arabic
Private ocr As New IronTesseract()
ocr.Language = OcrLanguage.Arabic

Using input As New OcrInput()
	
	Dim pageindices = New Integer() { 1, 2 }
	input.LoadImageFrames("img/arabic.gif", pageindices)
	' Add image filters if needed
	' In this case, even thought input is very low quality
	' IronTesseract can read what conventional Tesseract cannot.
	Dim result As OcrResult = ocr.Read(input)
	' Console can't print Arabic on Windows easily.
	' Let's save to disk instead.
	result.SaveAsTextFile("arabic.txt")
End Using
$vbLabelText   $csharpLabel

多語言範例

同時使用多種語言進行OCR辨識也是可行的。這可以增強對Unicode文件中英文元資料和URL的OCR辨識效果。

// Reference to the path of the source file that demonstrates multi-language OCR
:path=/static-assets/ocr/content-code-examples/get-started/get-started-13.cs
// Reference to the path of the source file that demonstrates multi-language OCR
using IronOcr;

// PM> Install IronOcr.Languages.ChineseSimplified
IronTesseract ocr = new IronTesseract();
ocr.Language = OcrLanguage.ChineseSimplified;

// We can add any number of languages
ocr.AddSecondaryLanguage(OcrLanguage.English);

using OcrInput input = new OcrInput();
input.LoadPdf("multi-language.pdf");
OcrResult result = ocr.Read(input);
result.SaveAsTextFile("results.txt");
' Reference to the path of the source file that demonstrates multi-language OCR
Imports IronOcr

' PM> Install IronOcr.Languages.ChineseSimplified
Private ocr As New IronTesseract()
ocr.Language = OcrLanguage.ChineseSimplified

' We can add any number of languages
ocr.AddSecondaryLanguage(OcrLanguage.English)

Using input As New OcrInput()
	input.LoadPdf("multi-language.pdf")
	Dim result As OcrResult = ocr.Read(input)
	result.SaveAsTextFile("results.txt")
End Using
$vbLabelText   $csharpLabel

詳細的OCR結果對象

IronOCR 為每次操作傳回一個 OCR 結果物件。 通常,開發人員會存取Text屬性來取得掃描的文字。 但是,結果物件包含更詳細的資訊。

// Reference to the path of the source file demonstrating detailed OCR result object usage
:path=/static-assets/ocr/content-code-examples/get-started/get-started-14.cs
// Reference to the path of the source file demonstrating detailed OCR result object usage
using IronOcr;

IronTesseract ocr = new IronTesseract();

// Must be set to true to read barcode
ocr.Configuration.ReadBarCodes = true;
using OcrInput input = new OcrInput();
var pageindices = new int[] { 1, 2 };
input.LoadImageFrames(@"img\sample.tiff", pageindices);

OcrResult result = ocr.Read(input);
var pages = result.Pages;
var words = pages[0].Words;
var barcodes = result.Barcodes;
// Explore here to find a massive, detailed API:
// - Pages, Blocks, Paraphaphs, Lines, Words, Chars
// - Image Export, Fonts Coordinates, Statistical Data, Tables
' Reference to the path of the source file demonstrating detailed OCR result object usage
Imports IronOcr

Private ocr As New IronTesseract()

' Must be set to true to read barcode
ocr.Configuration.ReadBarCodes = True
Using input As New OcrInput()
	Dim pageindices = New Integer() { 1, 2 }
	input.LoadImageFrames("img\sample.tiff", pageindices)
	
	Dim result As OcrResult = ocr.Read(input)
	Dim pages = result.Pages
	Dim words = pages(0).Words
	Dim barcodes = result.Barcodes
	' Explore here to find a massive, detailed API:
	' - Pages, Blocks, Paraphaphs, Lines, Words, Chars
	' - Image Export, Fonts Coordinates, Statistical Data, Tables
End Using
$vbLabelText   $csharpLabel

表現

IronOCR開箱即用,無需性能調優或影像修改。

速度飛快:IronOcr.2020+ 的速度比以前的版本快 10 倍,錯誤率降低了 250% 以上。

了解更多

要了解有關 C#、VB、F# 或任何其他 .NET 語言中的 OCR 的更多信息,請閱讀我們的社區教程,其中提供了使用 IronOCR 的實際示例,並展示了優化庫的細微差別。

我們也提供了針對.NET 開發人員的完整 API 參考文件

Curtis Chau
技術作家

Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。

除了開發之外,Curtis 對物聯網 (IoT) 有著濃厚的興趣,探索將硬體和軟體結合的創新方式。在閒暇時間,他喜愛遊戲並構建 Discord 機器人,結合科技與創意的樂趣。

審核人
Jeff Fritz
Jeffrey T. Fritz
首席程序经理 - .NET 社区团队
Jeff 也是 .NET 和 Visual Studio 团队的首席程序经理。他是 .NET Conf 虚拟会议系列的执行制作人,并主持“Fritz 和朋友”这一每周两次的开发者的直播节目,在节目上讨论技术并与观众一起编写代码。Jeff 撰写研讨会、主持演讲,并计划大型 Microsoft 开发者活动(包括 Microsoft Build、Microsoft Ignite、.NET Conf 和 Microsoft MVP Summit)的内容。
準備好開始了嗎?
Nuget 下載 5,167,857 | Version: 2025.11 剛發表