x86應用程式中的OcrInternals部署錯誤

Curtis Chau
Curtis Chau
已更新: 2026年6月24日

IronTesseract.ReadScreenShot() 通過 IronOCR 的 AdvancedScan 管道運行,該管道僅在 Windows x64 處理過程中受支持。 從 x86 應用程式調用時會因 OcrInternals 部署錯誤而失敗,即使已安裝 IronOcr.Extensions.AdvancedScan 套件。

Error while reading a screenshot, Error while deploying OcrInternals for IronOcr:
'Unable to locate 'OcrInternals' in
...\bin\Debug\runtimes\win-x86\native,
...\bin\Debug\runtimes\win.6.2-x86\native,
...\bin\Debug\runtimes\win.6-x86\native,
...\bin\Debug\,
...
nor in an embedded resource.'
Please install the NuGet Package 'IronOcr.Extension.AdvancedScan' when using IronOcr on Windows.
[Issue Code IRONOCR-OCRINTERNALS-DEPLOYMENT-ERROR-WIN]
Text

失敗顯示在 ReadScreenShot() 調用本身:

var ocr = new IronOcr.IronTesseract();
using (var input = new IronOcr.OcrInput())
{
    input.LoadImage("Step_1-5.jpg");
    var result = ocr.ReadScreenShot(input);
    Console.WriteLine(result.Text);
}

ReadScreenShot() 所依賴的 AdvancedScan 本地元件不支持在 x86 過程中使用。 安裝 IronOcr.Extensions.AdvancedScan 是必要的,但這並不會改變主機過程的位寬,因此該調用仍然無法在 x86 下運行。

謹慎: 安裝 AdvancedScan 並不會使 ReadScreenShot() 在 x86 處理程序中運作。 呼叫它的處理程序必須以 x64 執行。

解決方案

選擇 1: 直接目標 x64

最直接的修復措施是將專案的平台目標切換到x64。在Visual Studio中:

  1. 右鍵點擊專案,然後選擇屬性。
  2. 打開建立選項卡。
  3. 將平台目標設置為x64。
  4. 取消選中首選32位。
  5. 重建並運行。

當主機過程以 x64 運行時,ReadScreenShot() 在受支持的環境中執行。

選擇 2: 保持應用程式x86並調用x64輔助進程

當主應用程式必須保持x86時,只將OCR操作移入小型x64輔助進程並從現有應用程式調用。結構如下:

MainWinForms.x86
  - .NET Framework Windows Forms app
  - Platform target: x86
  - Does not run ReadScreenShot() directly
  - Calls the x64 helper process
OcrHelper.x64
  - .NET Framework Console app
  - Platform target: x64
  - References IronOCR
  - References IronOcr.Extensions.AdvancedScan
  - Runs Ocr.ReadScreenShot()
  - Returns the OCR result to the main app
Text

x86應用程式仍然不受影響,而AdvancedScan在它受支持的地方運行。

從x86應用程式調用輔助項

用 ProcessStartInfo 啟動輔助程式並讀取其輸出:

using System;
using System.Diagnostics;
using System.IO;
public static class OcrHelperClient
{
    public static string ReadScreenshotWithHelper(string imagePath)
    {
        string helperExePath = Path.Combine(
            AppDomain.CurrentDomain.BaseDirectory,
            "OcrHelper.x64",
            "OcrHelper.x64.exe"
        );
        if (!File.Exists(helperExePath))
        {
            throw new FileNotFoundException("The OCR helper executable was not found.", helperExePath);
        }
        var startInfo = new ProcessStartInfo
        {
            FileName = helperExePath,
            Arguments = "\"" + imagePath + "\"",
            UseShellExecute = false,
            RedirectStandardOutput = true,
            RedirectStandardError = true,
            CreateNoWindow = true
        };
        using (var process = new Process())
        {
            process.StartInfo = startInfo;
            process.Start();
            string output = process.StandardOutput.ReadToEnd();
            string error = process.StandardError.ReadToEnd();
            process.WaitForExit();
            if (process.ExitCode != 0)
            {
                throw new Exception("OCR helper failed: " + error);
            }
            return output;
        }
    }
}

重定向 StandardOutput 和 StandardError 讓調用者捕獲識別的文本並顯示輔助程式退出代碼的任何失敗。

string imagePath = @"C:\Images\Step_1-5.jpg";
string text = OcrHelperClient.ReadScreenshotWithHelper(imagePath);
Console.WriteLine(text);

建立x64輔助程序

將輔助程式構建為引用 IronOcr 和 IronOcr.Extensions.AdvancedScan 的 x64 控制台應用程式。 它從第一個參數讀取圖像路徑,運行 OCR,並將結果寫入 stdout:

using System;
using System.IO;
using IronOcr;
namespace OcrHelper.x64
{
    internal static class Program
    {
        private static int Main(string[] args)
        {
            try
            {
                if (args.Length == 0)
                {
                    Console.Error.WriteLine("Missing image path argument.");
                    return 1;
                }
                string imagePath = args[0];
                if (!File.Exists(imagePath))
                {
                    Console.Error.WriteLine("Image file was not found: " + imagePath);
                    return 2;
                }
                string licenseKey = Environment.GetEnvironmentVariable("IRONOCR_LICENSE_KEY");
                if (!string.IsNullOrWhiteSpace(licenseKey))
                {
                    License.LicenseKey = licenseKey;
                }
                var ocr = new IronTesseract();
                using (var input = new OcrInput())
                {
                    input.LoadImage(imagePath);
                    var result = ocr.ReadScreenShot(input);
                    Console.WriteLine(result.Text);
                }
                return 0;
            }
            catch (Exception ex)
            {
                Console.Error.WriteLine(ex.ToString());
                return 99;
            }
        }
    }
}

不同的退出代碼 (1, 2, 99) 使調用的應用程式能夠區分缺少的參數、缺少的文件或意外的例外情況。

製作使用說明

範例中為簡單起見使用 stdout。 對於生產,選擇適合您的建築的通信方法。 選擇包括:

  • 標準輸出和標準錯誤。
  • 臨時JSON文件。
  • 命名管道。
  • 本地HTTP端點。
  • 託管x64 OCR操作的Windows服務。

對於小規模或偶爾呼叫:按需啟動輔助程序通常沒問題。對於大容量工作:長期運行的x64輔助服務比每個請求生成一個進程更高效。

調試提示

當輔助方法不正常工作時,請通過這些檢查進行:

  • 確認主應用程式確實需要保持 x86,並且 Ocr.Read() 不足以應對截圖場景。
  • 驗證當直接從 x64 過程運行時 ReadScreenShot() 成功。
  • 使用平台目標:x64構建輔助項目,並確保 x86 應用程式永遠不會自行調用 ReadScreenShot()。
  • 在 x64 輔助項目中安裝 IronOcr.Extensions.AdvancedScan。
  • 確認傳遞給輔助程序的圖像路徑對輔助進程可達。
  • 在程式碼、應用程式配置或 IRONOCR_LICENSE_KEY 環境變量中配置 IronOCR 授權密鑰。

發布輔助程式時,請複製整個構建輸出,而不僅是 .exe。 輸出文件夾必須包含所有引用的程式集和構建生成的本機運行時文件,否則輔助程序將遇到相同的部署錯誤。

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

閱讀更多

準備好開始了嗎?

NuGet 下載次數 6,411,693版本:2026.11剛剛發布