開始使用 C# 和 VB.NET 的 OCR
IronOCR 是一個 C# 軟體庫,允許 .NET 平台的軟體開發人員從圖像和 PDF 文件中識別和讀取文本。這是一個純 .NET OCR 庫,使用當前已知的最先進的 Tesseract 引擎。
安裝
使用 NuGet 套件管理器安裝
使用 NuGet 套件管理器在 Visual Studio 或命令行中安裝 IronOCR。在 Visual Studio 中,導航到控制台:
- 工具 ->
- NuGet 套件管理器 ->
- 套件管理器控制台
Install-Package IronOcr
並查看 NuGet上的IronOCR 了解有關版本更新和安裝的更多信息。
有其他適用於不同平台的 IronOCR NuGet 套件:
- Windows: https://www.nuget.org/packages/IronOcr
- Linux: https://www.nuget.org/packages/IronOcr.Linux
- MacOs: https://www.nuget.org/packages/IronOcr.MacOs (ARM): https://www.nuget.org/packages/IronOcr.MacOs.ARM
下載 IronOCR .ZIP
您也可以選擇通過 .ZIP 文件下載 IronOCR。點擊 直接下載 DLL. 一旦你下載了.zip:
.NET Framework 4.0+ 安裝說明:
- 將 net40 資料夾中的 IronOcr.dll 包含到您的專案中
然後新增以下 Assembly 參考:
System.Configuration
System.Drawing
- System.Web
.NET Standard & .NET Core 2.0+ & .NET 5 的使用说明
- 將 netstandard2.0 資料夾中的 IronOcr.dll 包含到您的專案中
然後新增一個 NuGet 套件參考到:
* System.Drawing.Common 4.7 或更高版本
下載 IronOCR 安裝程式 (僅限 Windows)
另一個選項是下載我們的 IronOCR 安裝程式,該程式將安裝 IronOCR 運行所需的所有資源。請記住這個選項僅適用于 Windows 系統。要下載安裝程式,請 點擊這裡. 一旦你下載了.zip:
.NET Framework 4.0+ 安裝說明:
- 將 net40 資料夾中的 IronOcr.dll 包含到您的專案中
然後新增以下 Assembly 參考:
System.Configuration
System.Drawing
- System.Web
.NET Standard & .NET Core 2.0+ & .NET 5 的使用说明
- 將 netstandard2.0 資料夾中的 IronOcr.dll 包含到您的專案中
然後新增一個 NuGet 套件參考到:
* System.Drawing.Common 4.7 或更高版本
為什麼選擇 IronOCR?
IronOCR 是一個易於安裝、完整且有詳盡文檔的 .NET 軟體庫。
選擇 IronOCR,無需使用任何外部網絡服務、持續費用或在互聯網上發送機密文件,即可實現 99.8% 以上的 OCR 精準度。
為什麼 C# 開發者選擇 IronOCR 而不是 Vanilla Tesseract:
- 安裝為單個 DLL 或 NuGet
- 包括 Tesseract 5、4 和 3 引擎的即時支持
- 精確度 99.8%,顯著超出普通 Tesseract
- 極速且支持多執行緒
- MVC、WebApp、桌面應用程序、控制台和服務器應用程序兼容
- 無需使用 Exes 或 C++ 代碼
- 完整的 PDF OCR 支持
- 幾乎能對任何圖像文件或 PDF 進行 OCR
- 完整支持 .NET Core,標準和框架
- 可部署於 Windows、Mac、Linux、Azure、Docker、Lambda、AWS
- 讀取條形碼和 QR 碼
- 將 OCR 導出為 XHTML
- 將 OCR 導出為可檢索的 PDF 文件
- 支持多執行緒
- 支持 125 種國際語言,全部通過 NuGet 或 OcrData 文件管理
- 提取圖像、坐標、統計信息和字體,不僅僅是文本
- 可用于在商業和專有應用程序中重新分發 Tesseract OCR
在處理真實圖像和有瑕疵的文件(如照片或低分辨率掃描件,可能存在數字噪聲或缺陷)時,IronOCR 表現尤為出色。
其他 免費OCR 適用於 .NET 平台的函式庫,例如其他 .NET Tesseract APIs 和網頁服務,在這些實際使用情況下表現不佳。
使用 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
可配置的 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)
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")
多頁 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)
條碼和 QR
IronOCR 的一個獨特功能是它可以在掃描文本時從文件中讀取條碼和 QR 碼。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
圖片特定區域的光學字符識別 (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)
OCR 針對低品質掃描
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)
將 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")
將 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")
將 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")
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)
OCR 圖片過濾器列表
為提升 OCR 效能而內建於 IronOCR 的輸入過濾器包括:
- OcrInput.Rotate( 雙精度)** - 將圖像順時針旋轉指定度數。若要逆時針旋轉,請使用負數。
- OcrInput.Binarize() - 此影像濾鏡將每個像素變成黑色或白色,沒有過渡色。可能在文字與背景對比度非常低的情況下改善OCR性能。
- OcrInput.ToGrayScale() - 此影像濾鏡將每個像素轉換為灰階陰影。不太可能提高OCR的準確性,但可能提高速度
- OcrInput.對比() - 自動增加對比度。這個濾鏡通常能提高在低對比度掃描中的OCR速度和準確性。
- OcrInput.DeNoise() - 除去數字噪音。此過濾器應僅在預期有噪音的地方使用。
- OcrInput.Invert() - 反轉每個顏色。例如,白色變成黑色:黑色變成白色。
- OcrInput.Dilate() - 高級形態學。膨脹在圖像中的物體邊界上增加像素。與腐蝕相反
- OcrInput.Erode() - 進階形態學。Erosion去除物體邊界上的像素,相反於Dilate
- OcrInput.Deskew() - 將圖片旋轉到正確的方向和正交。這對於OCR非常有用,因為Tesseract對於傾斜掃描的容忍度可能低至5度。
- OcrInput.EnhanceResolution - 增強低質量圖片的解析度。這個濾鏡通常不需要使用,因為 OcrInput.MinimumDPI 和 OcrInput.TargetDPI 會自動捕捉並解決低解析度的輸入。
- EnhanceResolution 是一個設置,會自動檢測低解析度的圖片 (低於275 dpi的) 並自動放大圖像,然後銳化所有文字,以便 OCR 庫可以完美地讀取這些文字。儘管此操作本身耗時,但通常會減少圖像上進行 OCR 操作的總時間。
- 語言 IronOCR 支援 22 種國際語言包,可以使用語言設定來選擇一種或多種語言應用於 OCR 操作。
- 策略 IronOCR 支援兩種策略。我們可以選擇快速且不太準確的文檔掃描,或者使用某些人工智能模型的高級策略,通過查看單詞在句子中的統計關係來自動提高 OCR 文本的準確性。
- 色彩空間 是一種設定,我們可以選擇在灰度或彩色模式下進行 OCR。一般來說,灰度是最佳選擇。但是,有時當存在相似色調但顏色差異很大的文本或背景時,使用全彩色空間會提供更好的結果。
- 檢測深色背景上的白色文本。一般情況下,所有 OCR 庫都期望看到白色背景上的黑色文本。此設定允許 IronOCR 自動檢測負片或帶有白色文本的深色頁面並讀取它們。
- 輸入圖像類型。此設定允許開發人員引導 OCR 庫,告知它是查看完整文檔還是查看片段(例如截圖)。
- 旋轉和拉直 是一種高級設定,允許 IronOCR 獨特地識別和讀取不僅旋轉的文檔,還可能是帶有透視效果的文檔,如文本文檔的照片。
- 讀取條碼 是一項實用功能,允許 IronOCR 在讀取文本時自動讀取頁面上的條碼和 QR 碼,而不會額外增加大量的時間負擔。
- 色深。此設定確定 OCR 庫將使用多少位元每像素來確定顏色的深度。更高的色深可能會提高 OCR 質量,但也會增加完成 OCR 操作所需的時間。
125 種語言包
IronOCR 通過語言包支援125 種國際語言,這些語言包以 DLL 文件的形式分發,可以 從本網站下載或來自 NuGet 套件管理器語言包括德語、法語、英語、中文、日語和更多。 專門的語言包存在於護照機讀區、磁墨字符檢查、財務數據、車牌等許多方面。 您也可以使用任何tesseract的".traineddata"文件——包括您自己創建的文件。
語言範例
:path=/static-assets/ocr/content-code-examples/get-started/get-started-12.cs
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");
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
多語言範例
同時使用多種語言進行 OCR 也是可以的。這可以真正幫助獲取 Unicode 文件中的英文元數據和網址。
:path=/static-assets/ocr/content-code-examples/get-started/get-started-13.cs
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");
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
詳細的 OCR 結果對象
IronOCR 每次 OCR 操作都返回一個 OCR 結果對象。通常,開發人員只使用該對象的文字屬性來獲取從圖像掃描的文字。然而,OCR 結果 DOM 遠不止這些。
:path=/static-assets/ocr/content-code-examples/get-started/get-started-14.cs
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
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
性能
IronOCR 開箱即用,無需性能調整或大量修改輸入圖像。
速度極快:IronOCR.2020+ 比以前的版本快最多10倍,錯誤率降低超過 250%。
了解更多
若要瞭解更多有關在C#、VB、F#或任何其他 .NET 語言中的OCR,請 閱讀我們的社群教程,提供了 IronOCR 的實際應用範例,並展示了如何充分利用這個函式庫的細微之處。
完整的 .NET開發者的API參考 也可用。