OCR 工具 如何使用 Tesseract 從圖片中取得文字 Kannapat Udonpant 更新:2025年7月2日 下載 IronOCR NuGet 下載 DLL 下載 Windows 安裝程式 開始免費試用 法學碩士副本 法學碩士副本 將頁面複製為 Markdown 格式,用於 LLMs 在 ChatGPT 中打開 請向 ChatGPT 諮詢此頁面 在雙子座打開 請向 Gemini 詢問此頁面 在 Grok 中打開 向 Grok 詢問此頁面 打開困惑 向 Perplexity 詢問有關此頁面的信息 分享 在 Facebook 上分享 分享到 X(Twitter) 在 LinkedIn 上分享 複製連結 電子郵件文章 利用 IronOCR 和 Tesseract 等庫,開發人員可以使用高級演算法和機器學習技術從圖像和掃描文件中提取文字資訊。 本教學將向讀者展示如何使用 Tesseract 庫從圖像中提取文本,最後介紹 IronOCR 的獨特方法。 1. 使用 Tesseract 進行 OCR 1.1 安裝 Tesseract 使用 NuGet 套件管理器控制台,輸入以下命令: Install-Package Tesseract 或透過 NuGet 套件管理員下載該套件。 如何實現 OCR 文字識別,圖 1:在 NuGet 套件管理器中安裝 Tesseract 套件 在 NuGet 套件管理器中安裝Tesseract套件 安裝 NuGet 套件後,必須手動將語言檔案安裝並儲存到專案資料夾中。 這可以被視為該特定庫的一個缺陷。 請造訪以下網站下載語言檔案。 下載完成後,解壓縮文件,並將"tessdata"資料夾新增至專案的偵錯資料夾。 1.2. 使用 Tesseract(快速入門) 可以使用以下原始程式碼對給定影像進行OCR識別: using Tesseract; class Program { static void Main() { // Initialize Tesseract engine with English language data using var ocrEngine = new TesseractEngine(@"tessdata", "eng", EngineMode.Default); // Load the image to be processed using var img = Pix.LoadFromFile("Demo.png"); // Process the image to extract text using var res = ocrEngine.Process(img); // Output the recognized text Console.WriteLine(res.GetText()); Console.ReadKey(); } } using Tesseract; class Program { static void Main() { // Initialize Tesseract engine with English language data using var ocrEngine = new TesseractEngine(@"tessdata", "eng", EngineMode.Default); // Load the image to be processed using var img = Pix.LoadFromFile("Demo.png"); // Process the image to extract text using var res = ocrEngine.Process(img); // Output the recognized text Console.WriteLine(res.GetText()); Console.ReadKey(); } } Imports Tesseract Friend Class Program Shared Sub Main() ' Initialize Tesseract engine with English language data Dim ocrEngine = New TesseractEngine("tessdata", "eng", EngineMode.Default) ' Load the image to be processed Dim img = Pix.LoadFromFile("Demo.png") ' Process the image to extract text Dim res = ocrEngine.Process(img) ' Output the recognized text Console.WriteLine(res.GetText()) Console.ReadKey() End Sub End Class $vbLabelText $csharpLabel 首先,必須建立一個TesseractEngine對象,並將語言資料載入到引擎中。 然後藉助Pix.LoadFromFile載入所需的映像檔。 將圖像傳遞給TesseractEngine ,使用Process方法提取文字。 使用GetText方法取得識別出的文本,並將其列印到控制台。 如何進行OCR文本識別,圖2:從圖像中提取的文本 從圖像中提取的文本 1.3 超立方體的考慮因素 Tesseract 從 3.00 版本開始支援輸出文字格式、OCR 位置資料和頁面佈局分析。 Tesseract 可在 Windows、Linux 和 MacOS 上運行,但由於開發支援有限,目前已證實其主要在 Windows 和 Ubuntu 上按預期運行。 Tesseract 可以區分等寬字體和比例字體。 利用 OCRopus 等前端,Tesseract 非常適合用作後端,並可用於更具挑戰性的 OCR 作業,例如佈局分析。 Tesseract 的一些不足之處: 最新版本並未設計為可在 Windows 系統上編譯。 Tesseract 的 C# API 封裝器維護頻率很低,而且比 Tesseract 的新版本落後數年。 要了解有關 C# 中 Tesseract 的更多信息,請訪問Tesseract 教程。 2. 使用 IronOCR 進行 OCR 識別 2.1. 安裝 IronOCR 在 NuGet 套件管理器控制台中輸入以下命令: Install-Package IronOcr 或者,您也可以透過 NuGet 套件管理器安裝 IronOCR 庫,以及其他語言的附加套件,這些套件使用起來既簡單又方便。 如何取得 OCR 文字辨識功能,圖 3:透過 NuGet 套件管理器安裝 IronOcr 和語言套件 透過 NuGet 套件管理器安裝 IronOcr 和語言套件 2.2. 使用 IronOCR 以下是識別給定圖像中文字的範例程式碼: using IronOcr; class Program { static void Main() { // Create an IronTesseract instance with predefined settings var ocr = new IronTesseract() { Language = OcrLanguage.EnglishBest, Configuration = { TesseractVersion = TesseractVersion.Tesseract5 } }; // Create an OcrInput instance for image processing using var input = new OcrInput(); // Load the image to be processed input.AddImage("Demo.png"); // Process the image and extract text var result = ocr.Read(input); // Output the recognized text Console.WriteLine(result.Text); Console.ReadKey(); } } using IronOcr; class Program { static void Main() { // Create an IronTesseract instance with predefined settings var ocr = new IronTesseract() { Language = OcrLanguage.EnglishBest, Configuration = { TesseractVersion = TesseractVersion.Tesseract5 } }; // Create an OcrInput instance for image processing using var input = new OcrInput(); // Load the image to be processed input.AddImage("Demo.png"); // Process the image and extract text var result = ocr.Read(input); // Output the recognized text Console.WriteLine(result.Text); Console.ReadKey(); } } Imports IronOcr Friend Class Program Shared Sub Main() ' Create an IronTesseract instance with predefined settings Dim ocr = New IronTesseract() With { .Language = OcrLanguage.EnglishBest, .Configuration = { TesseractVersion = TesseractVersion.Tesseract5 } } ' Create an OcrInput instance for image processing Dim input = New OcrInput() ' Load the image to be processed input.AddImage("Demo.png") ' Process the image and extract text Dim result = ocr.Read(input) ' Output the recognized text Console.WriteLine(result.Text) Console.ReadKey() End Sub End Class $vbLabelText $csharpLabel 此程式碼初始化一個IronTesseract對象,設定語言和 Tesseract 版本。 然後建立一個OcrInput對象,使用AddImage方法載入圖片檔。 IronTesseract的Read方法處理圖像並提取文本,然後將文本列印到控制台。 如何進行 OCR 文字識別,圖 4:使用 IronOCR 庫提取的文字輸出 使用 IronOCR 庫提取文字輸出 2.3 鐵氧濃度比 (IronOCR) 考量 IronOCR 是 Tesseract 函式庫的擴展,引入了更高的穩定性和更高的準確性。 IronOCR 可以讀取PDF和照片中的文字內容。 它還可以讀取 20 多種不同類型的條碼和二維碼。 輸出可以呈現為純文字、結構化資料、條碼或二維碼。 該圖書館認可全球 125 種語言。 IronOCR 可靈活地在所有 .NET 環境(控制台、Web、桌面等)中運行,並且還支援最新的行動框架,如 Mono、Xamarin、 Azure和MAUI 。 IronOCR 提供免費試用版,且開發版的價格更低。 了解更多許可資訊。 有關 IronOCR 的詳細教程,請參閱本文,以了解如何在 C# 中從圖像中讀取文字。 Kannapat Udonpant 立即與工程團隊聊天 軟體工程師 在成為軟體工程師之前,Kannapat 完成了日本北海道大學的環境資源博士學位。在攻讀學位期間,Kannapat 也成為生物製造工程系車輛機器人實驗室的成員。2022 年,他利用自己的 C# 技能加入 Iron Software 的工程團隊,主要負責 IronPDF 的開發。Kannapat 非常重視他的工作,因為他可以直接向撰寫 IronPDF 使用的大部分程式碼的開發者學習。除了同儕學習之外,Kannapat 也很享受在 Iron Software 工作的社交生活。不寫程式碼或文件時,Kannapat 通常會用 PS5 玩遊戲或重看《最後的我們》。 相關文章 更新2025年6月22日 Power Automate OCR(開發者教程) 此光學字元識別技術應用於文件數位化、自動 PDF 資料擷取與輸入、發票處理,以及使掃描的 PDF 可供搜尋。 閱讀更多 更新2025年6月22日 Easyocr vs Tesseract (OCR 功能比較) 常用的 OCR 工具和函式庫,如 EasyOCR、Tesseract OCR、Keras-OCR 和 IronOCR,通常會用來將此功能整合到現代應用程式中。 閱讀更多 更新2025年6月22日 如何將圖片轉換為文字 在目前的數位時代,將圖像內容轉換成容易閱讀、可編輯、可搜尋的文字 閱讀更多 OCR C# 開放原始碼(開發者名單)最佳 OCR API(更新列表比較)
更新2025年6月22日 Easyocr vs Tesseract (OCR 功能比較) 常用的 OCR 工具和函式庫,如 EasyOCR、Tesseract OCR、Keras-OCR 和 IronOCR,通常會用來將此功能整合到現代應用程式中。 閱讀更多