IRONSOFTWAREHOME
影片

C# 如何在 Windows 上安裝 Tesseract OCR

Kannaopat Udonpant
Kannapat Udonpant
Updated: 2026年6月20日

本指南指導.NET開發人員將Google Cloud Vision替換為IronOCR作為即插即用的內部OCR引擎。它涵蓋憑證移除、Protobuf註釋解析替換、批量註釋簡化、多頁文件處理——這四個結構性更改佔據了遷移工作的主要部分。

為什麼從Google Cloud Vision OCR遷移

遷移的決定幾乎總是從兩個認識中的一個開始:合規審核阻止雲文件傳輸,或是管理GCP憑證、GCS儲存桶、異步輪詢和每圖片計費的操作範圍變得比OCR本身更昂貴。

服務帳戶JSON密鑰生命週期。 您的每一次應用部署——開發機、CI/CD管道、預發佈伺服器、生產伺服器、Docker容器、Kubernetes Pod ——都需要同一個服務帳戶JSON密鑰文件。該文件包含一個RSA私鑰。 它絕不能進入源程式碼控制,必須按計劃輪換,必須受文件系統權限保護,並且在輪換發生時必須同時在所有環境中更新。 一個妥協的密鑰將授予API存取權限,直到在GCP控制台手動撤銷。 IronOCR用一次性設置的應用程式啟動時的單個授權密鑰字串替換了整個操作表面。

按請求計費在規模上。 每1,000張影像1.50美元,每PDF頁面0.0015美元,這使得在開發中成本是隱性的,但在生產中卻痛苦難忍。 一個每月處理200,000頁面的文件加工管道,僅API費用就需300美元每月,還不算GCS儲存費和出口成本。 那300美元每月無限期重複。 IronOCR的永久授權將OCR從計量的運營開支轉變為固定的資本項目,在第二年或第三年運行費用為零。

PDF的GCS異步管道。 Google Cloud Vision不接受PDF作為直接API輸入。 完整管道需要第二個NuGet包(Google.Cloud.Storage.V1)、一個已佈建的GCS儲存桶、一次非同步上傳、一次AsyncBatchAnnotateFilesAsync調用、一個輪詢迴圈、來自GCS的JSON輸出解析,以及一個清理步驟。 該管道在提取任何文字之前涵蓋50多行程式碼。 IronOCR以三行同步無外部依賴閱讀PDF。

Protobuf符號串聯。 DOCUMENT_TEXT_DETECTION回應將文字儲存在頁面、塊、段落、單詞和符號的Protobuf階層中的符號級別。 閱讀段落文字需要迭代五個巢狀迴圈並調用.SelectMany(w => w.Symbols).Select(s => s.Text)。 IronOCR將段落文字作為paragraph.Text即型別化字串屬性返回。

每分鐘1,800次請求的預設配額。 超出預設配額的批量工作負載會收到StatusCode.ResourceExhausted回應,從而每次超過限制時使管道停滯60秒。 增加配額需要GCP控制台請求並獲得Google的批准。 IronOCR在本地以可用CPU核心的速度處理——無需管理配額,不需尋求准許,也不用為速率限制編寫重試邏輯。

無線下或隔離支持。 Google Cloud Vision需要與Google的端點之間的互聯網連接。 無法使用隔離網路、受分類的資料中心和工業控制系統,無論其建築複雜程度如何。 IronOCR運行在初始授權驗證後毫無出站網路連接的情況下。

根本問題

在運行OCR程式碼的第一行之前,Google Cloud Vision需要磁碟上的JSON密鑰文件:

// Google Cloud Vision: JSON key file deployed to every server before this line works
// GOOGLE_APPLICATION_CREDENTIALS="/etc/secrets/service-account.json" must be set
// Key contains RSA private key — rotate manually, revoke if compromised
_client = ImageAnnotatorClient.Create();
var image = Image.FromFile("document.jpg");
var response = _client.DetectText(image);   // document leaves your infrastructure
string text = response[0].Description;
C#

IronOCR以一個字串啟動,並完全在本地機器上運行:

// IronOCR: one string at startup, no key files, no environment variables
IronOcr.License.LicenseKey = "YOUR-LICENSE-KEY";
string text = new IronTesseract().Read("document.jpg").Text;  // local, no cloud
C#

IronOCR與Google Cloud Vision OCR:功能比較

下表直接映射了在為遷移建立商業案例的團隊的特性。

功能Google Cloud Vision OCRIronOCR
處理地點谷歌雲(遠程)本地(在地)
身份驗證服務帳戶JSON密鑰+環境變數授權密鑰字串
PDF 輸入上傳到GCS+異步APIinput.LoadPdf()直接
受密碼保護的 PDF不支持LoadPdf(path, Password: "...")
多頁TIFF輸入有限input.LoadImageFrames()
可搜尋的 PDF 輸出不可用result.SaveAsSearchablePdf()
結構化資料存取Protobuf: 頁面>塊>段落>單詞>符號result.Words(型別化.NET物件)
段落文字屬性否——需要符號串聯paragraph.Text直接屬性
置信分數每符號(需要迴圈)result.Confidence, word.Confidence
自動圖像預處理無(由機器學習處理)糾偏,去噪,對比度,二值化,銳化
基於區域的OCR無本地裁剪OcrInput
條碼讀取獨立的API功能ocr.Configuration.ReadBarCodes = true
速率限制每分鐘1,800次請求的預設值無(CPU限制)
離線 / 空隙不是
按文件費用每1,000張圖片1.50美元; 每PDF頁面0.0015美元無(永久許可)
支持的語言~50125+
FedRAMP授權未授權不適用(本地)
HIPAA遵從路徑需要商業夥伴協議無第三方資料處理
.NET Framework 支持.NET Standard 2.0+.NET Framework 4.6.2+ and .NET 5/6/7/8/9
需要 NuGet 套件Google.Cloud.Storage.V1IronOcr
定價模型按請求計量計費永久($999 Lite / 1,499美元 Plus / 2,999美元 Professional / 5,999美元Unlimited)

快速入門:Google Cloud Vision OCR到IronOCR的遷移

步驟1:替換NuGet包

移除Google Cloud包:

dotnet remove package Google.Cloud.Vision.V1
dotnet remove package Google.Cloud.Storage.V1
SHELL

NuGet包頁面安裝IronOCR:

dotnet add package IronOcr

步驟2:更新命名空間

將Google Cloud命名空間替換為IronOCR命名空間:

// Before (Google Cloud Vision)
using Google.Cloud.Vision.V1;
using Google.Cloud.Storage.V1;
using Google.Protobuf;
using Grpc.Core;

// After (IronOCR)
using IronOcr;
C#

步驟3:初始化許可證

在建立任何IronTesseract實例之前,於應用程式啟動時新增許可初始化一次:

IronOcr.License.LicenseKey = "YOUR-LICENSE-KEY";

在生產中,從環境變數或秘密管理器中讀取密鑰:

IronOcr.License.LicenseKey = Environment.GetEnvironmentVariable("IRONOCR_LICENSE")
    ?? throw new InvalidOperationException("IRONOCR_LICENSE environment variable not set.");
C#

程式碼遷移範例

消除服務帳戶憑證配置

Google Cloud Vision客戶端的初始化看起來是一行程式碼,但需要大量的先決基礎設施。 加入專案的每個開發者、每個部署環境和每個CI/CD管道都需要完整的憑證配置,才能在構造函式完成時不會拋出。

Google Cloud Vision方法:

using Google.Cloud.Vision.V1;

// Prerequisites before this class can be instantiated:
// 1. GCP project created and Vision API enabled in GCP Console
// 2. Service account created with roles/cloudvision.user IAM role
// 3. JSON key file downloaded to every server that runs this code
// 4. GOOGLE_APPLICATION_CREDENTIALS env var pointing to the JSON file
// 5. JSON file excluded from source control via .gitignore
// 6. Key rotation schedule established (recommended: 90 days)
// 7. Separate credentials per environment (dev/staging/prod)

public class DocumentOcrService
{
    private readonly ImageAnnotatorClient _client;
    private readonly string _projectId;

    public DocumentOcrService(string projectId)
    {
        _projectId = projectId;
        // Throws RpcException(StatusCode.PermissionDenied) if any prerequisite is missing
        _client = ImageAnnotatorClient.Create();
    }

    public string ReadDocument(string imagePath)
    {
        var image = Image.FromFile(imagePath);
        var response = _client.DetectText(image);
        return response.Count > 0 ? response[0].Description : string.Empty;
    }
}
C#

IronOCR方法:

using IronOcr;

// Prerequisites: set the license key once at app startup
// No JSON files, no environment variables beyond the key, no GCP Console configuration
// No key rotation, no IAM roles, no per-environment credential sets

public class DocumentOcrService
{
    private readonly IronTesseract _ocr;

    public DocumentOcrService()
    {
        // IronTesseract is ready immediately — no external validation required
        _ocr = new IronTesseract();
    }

    public string ReadDocument(string imagePath)
    {
        return _ocr.Read(imagePath).Text;
    }
}
C#

操作差異是具體的:Google Cloud Vision在運行時產生五種類別的RpcException——PermissionDeniedResourceExhaustedUnavailableDeadlineExceededUnauthenticated——每一種代表不同的基礎設施故障模式。 IronOCR的故障模式是IOException(找不到檔案或檔案被鎖定)和OcrException(處理失敗)。 查看IronTesseract設置指南以獲取配置選項,以及IronOCR產品頁面以瞭解授權詳情。

用多種功能型別替換批註請求

Google Cloud Vision支持將多個圖像批量到一個DOCUMENT_TEXT_DETECTION結果,或者在提交多個圖像以最小化往返開銷時使用這種模式。 Protobuf的回應需要通過索引將每個AnnotateImageResponse匹配回其原始請求。

Google Cloud Vision方法:

using Google.Cloud.Vision.V1;
using System.Collections.Generic;

public class BatchAnnotationService
{
    private readonly ImageAnnotatorClient _client;

    public BatchAnnotationService()
    {
        _client = ImageAnnotatorClient.Create();
    }

    public List<string> BatchAnnotateImages(string[] imagePaths)
    {
        // Build one request per image with TEXT_DETECTION feature
        var requests = imagePaths.Select(path => new AnnotateImageRequest
        {
            Image = Image.FromFile(path),
            Features =
            {
                new Feature { Type = Feature.Types.Type.TextDetection },
                new Feature { Type = Feature.Types.Type.DocumentTextDetection }
            }
        }).ToList();

        // Single round-trip for all images in the batch
        var batchResponse = _client.BatchAnnotateImages(requests);

        // Match responses back to requests by index
        var results = new List<string>();
        for (int i = 0; i < batchResponse.Responses.Count; i++)
        {
            var response = batchResponse.Responses[i];
            if (response.Error != null)
            {
                // Per-item error in batch — must handle individually
                results.Add($"Error on {imagePaths[i]}: {response.Error.Message}");
                continue;
            }

            // Prefer DOCUMENT_TEXT_DETECTION full text if available
            var fullText = response.FullTextAnnotation?.Text
                ?? response.TextAnnotations.FirstOrDefault()?.Description
                ?? string.Empty;
            results.Add(fullText);
        }

        return results;
    }
}
C#

IronOCR方法:

using IronOcr;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Threading.Tasks;

public class BatchAnnotationService
{
    public List<string> BatchAnnotateImages(string[] imagePaths)
    {
        var results = new ConcurrentDictionary<int, string>();

        // Parallel processing — no batch size limit, no network round-trips
        Parallel.For(0, imagePaths.Length, i =>
        {
            var ocr = new IronTesseract();   // thread-safe: one instance per thread
            results[i] = ocr.Read(imagePaths[i]).Text;
        });

        // Reconstruct in original order
        return Enumerable.Range(0, imagePaths.Length)
            .Select(i => results[i])
            .ToList();
    }

    public OcrResult BatchAsDocument(string[] imagePaths)
    {
        // Load all images into a single OcrInput for combined document output
        using var input = new OcrInput();
        foreach (var path in imagePaths)
            input.LoadImage(path);

        return new IronTesseract().Read(input);
    }
}
C#

IronOCR在CPU核心上並行運行批處理,沒有網路開銷。 沒有BatchSize上限,沒有回應索引匹配,並且沒有網路或憑證失敗的逐項錯誤處理。 對於將多個圖像組合成單一邏輯文件的工作負載——例如個別JPEG格式的掃描多頁表單——result.Pages對應到每個輸入圖像進行索引。 多執行緒範例顯示了並行處理的性能基準。

遷移Protobuf單詞層次註釋提取

Google Cloud Vision的單詞層次邊界框和置信度資料需要遍歷完整的Protobuf架構:頁面,然后是塊,然后是段落,然后是單詞,然后是符號。 提取單詞文字需要將每個單詞的符號串聯起來——.Text屬性。 邊界框坐標儲存為Vertices列表而不是離散的X、Y、寬度、高度字段。

Google Cloud Vision方法:

using Google.Cloud.Vision.V1;
using System.Collections.Generic;

public record WordAnnotation(string Text, int X, int Y, int Width, int Height, float Confidence);

public class WordLevelExtractor
{
    private readonly ImageAnnotatorClient _client;

    public WordLevelExtractor()
    {
        _client = ImageAnnotatorClient.Create();
    }

    public List<WordAnnotation> ExtractWordAnnotations(string imagePath)
    {
        var image = Image.FromFile(imagePath);
        var annotation = _client.DetectDocumentText(image);

        var words = new List<WordAnnotation>();

        // Navigate: Pages -> Blocks -> Paragraphs -> Words
        foreach (var page in annotation.Pages)
        {
            foreach (var block in page.Blocks)
            {
                foreach (var paragraph in block.Paragraphs)
                {
                    foreach (var word in paragraph.Words)
                    {
                        // Word.Text does not exist — must concatenate Symbols
                        var text = string.Concat(word.Symbols.Select(s => s.Text));

                        // BoundingPoly has Vertices, not X/Y/Width/Height
                        var vertices = word.BoundingBox.Vertices;
                        int x = vertices[0].X;
                        int y = vertices[0].Y;
                        int width = vertices.Count > 1 ? vertices[1].X - vertices[0].X : 0;
                        int height = vertices.Count > 2 ? vertices[2].Y - vertices[0].Y : 0;

                        words.Add(new WordAnnotation(text, x, y, width, height, word.Confidence));
                    }
                }
            }
        }

        return words;
    }
}
C#

IronOCR方法:

using IronOcr;
using System.Collections.Generic;

public record WordAnnotation(string Text, int X, int Y, int Width, int Height, double Confidence);

public class WordLevelExtractor
{
    private readonly IronTesseract _ocr;

    public WordLevelExtractor()
    {
        _ocr = new IronTesseract();
    }

    public List<WordAnnotation> ExtractWordAnnotations(string imagePath)
    {
        var result = _ocr.Read(imagePath);

        // Words are a flat collection — no hierarchy traversal, no symbol concatenation
        return result.Words.Select(w => new WordAnnotation(
            Text:       w.Text,         // direct string property
            X:          w.X,
            Y:          w.Y,
            Width:      w.Width,
            Height:     w.Height,
            Confidence: w.Confidence    // double, no conversion needed
        )).ToList();
    }
}
C#

Google Cloud Vision版的符號串聯迴圈不是一種設計選擇——而是Protobuf模式所要求的。 Word.Text不是回應對像中的屬性。 每個使用單詞層次API的團隊都會編寫等效的迴圈。IronOCR的Text, X, Y, Width, Confidence作為一等屬性。 讀取結果指南記錄了每個粒度級別提供的完整屬性集。

多頁TIFF處理到可搜索PDF輸出

Google Cloud Vision將多頁TIFF文件視為一系列連續的圖像,每個圖像都需要單獨的API調用。 沒有接受TIFF並為所有幀返回結構化輸出的單一API調用。 從Google Cloud Vision結果生成可搜尋的PDF需要一個單獨的PDF生成庫——API僅返回文字。

Google Cloud Vision方法:

using Google.Cloud.Vision.V1;
using System.Collections.Generic;
using System.Drawing;          // for TIFF frame extraction
using System.Drawing.Imaging;

public class TiffProcessingService
{
    private readonly ImageAnnotatorClient _client;

    public TiffProcessingService()
    {
        _client = ImageAnnotatorClient.Create();
    }

    public List<string> ProcessMultiPageTiff(string tiffPath)
    {
        var pageTexts = new List<string>();

        // Load TIFF and extract frames manually using System.Drawing
        using var tiff = System.Drawing.Image.FromFile(tiffPath);
        var frameDimension = new FrameDimension(tiff.FrameDimensionsList[0]);
        int frameCount = tiff.GetFrameCount(frameDimension);

        for (int i = 0; i < frameCount; i++)
        {
            tiff.SelectActiveFrame(frameDimension, i);

            // Save each frame to a temp file — Vision API does not accept TIFF frames directly
            var tempPath = Path.Combine(Path.GetTempPath(), $"tiff-frame-{i}.jpg");
            tiff.Save(tempPath, ImageFormat.Jpeg);

            // One API call per frame — each call = one unit of quota
            var visionImage = Google.Cloud.Vision.V1.Image.FromFile(tempPath);
            var response = _client.DetectText(visionImage);
            pageTexts.Add(response.FirstOrDefault()?.Description ?? string.Empty);

            File.Delete(tempPath);
        }

        // Producing a searchable PDF requires a separate library (e.g., iTextSharp)
        // Google Cloud Vision has no PDF output capability
        return pageTexts;
    }
}
C#

IronOCR方法:

using IronOcr;

public class TiffProcessingService
{
    private readonly IronTesseract _ocr;

    public TiffProcessingService()
    {
        _ocr = new IronTesseract();
    }

    public string ProcessMultiPageTiff(string tiffPath)
    {
        using var input = new OcrInput();
        // LoadImageFrames handles all TIFF frames in one call — no temp files, no frame loop
        input.LoadImageFrames(tiffPath);

        var result = _ocr.Read(input);
        return result.Text;
    }

    public void ProcessMultiPageTiffToSearchablePdf(string tiffPath, string outputPdfPath)
    {
        using var input = new OcrInput();
        input.LoadImageFrames(tiffPath);

        var result = _ocr.Read(input);

        // Google Cloud Vision has no equivalent — this single call produces a searchable PDF
        result.SaveAsSearchablePdf(outputPdfPath);
    }

    public void ProcessLowQualityTiff(string tiffPath, string outputPdfPath)
    {
        using var input = new OcrInput();
        input.LoadImageFrames(tiffPath);

        // Preprocessing before OCR improves accuracy on degraded scans
        input.Deskew();
        input.DeNoise();
        input.Contrast();

        var result = _ocr.Read(input);
        result.SaveAsSearchablePdf(outputPdfPath);
    }
}
C#

Google Cloud Vision版本需要System.Drawing進行幀提取,將臨時JPEG文件寫入磁盤,每幀調用一個API(按幀數成比例消耗配額),以及一個單獨的PDF庫來輸出純文字以外的任何內容。 IronOCR通過SaveAsSearchablePdf生成可搜尋的PDF輸出。 對於掃描存檔的TIFF文件,ProcessLowQualityTiff中的預處理管道在一個步驟中解決了最常見的質量問題。 查看TIFF和GIF輸入以及可搜尋PDF輸出以獲取完整API。

具有進度跟踪的速率限制批量遷移

在生產規模上,Google Cloud Vision的每分鐘預設配額1800次請求需要限制或者使用指數回退的重試邏輯。 在一次夜間作業中處理5000份文件將多次超出配額。 每次超過配額都會強制等待管道60秒。 IronOCR沒有速率限制——管道僅受限於CPU核心和可用執行緒。

Google Cloud Vision方法:

using Google.Cloud.Vision.V1;
using Grpc.Core;
using System.Collections.Generic;

public class ThrottledBatchProcessor
{
    private readonly ImageAnnotatorClient _client;
    private const int MaxRequestsPerMinute = 1800;
    private const int RetryDelayMs = 60_000;

    public ThrottledBatchProcessor()
    {
        _client = ImageAnnotatorClient.Create();
    }

    public async Task<Dictionary<string, string>> ProcessWithThrottlingAsync(
        string[] imagePaths,
        IProgress<(int completed, int total)> progress)
    {
        var results = new Dictionary<string, string>();
        int completed = 0;

        foreach (var path in imagePaths)
        {
            bool succeeded = false;
            while (!succeeded)
            {
                try
                {
                    var image = Google.Cloud.Vision.V1.Image.FromFile(path);
                    var response = _client.DetectText(image);
                    results[path] = response.FirstOrDefault()?.Description ?? string.Empty;
                    succeeded = true;
                }
                catch (RpcException ex) when (ex.StatusCode == StatusCode.ResourceExhausted)
                {
                    // Rate limit exceeded — wait and retry
                    await Task.Delay(RetryDelayMs);
                }
            }

            progress.Report((++completed, imagePaths.Length));
        }

        return results;
    }
}
C#

IronOCR方法:

using IronOcr;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;

public class BatchProcessor
{
    public Dictionary<string, string> ProcessBatch(
        string[] imagePaths,
        IProgress<(int completed, int total)> progress)
    {
        var results = new ConcurrentDictionary<string, string>();
        int completed = 0;

        // No rate limits — full parallelism, no retry logic needed
        Parallel.ForEach(imagePaths, imagePath =>
        {
            var ocr = new IronTesseract();
            results[imagePath] = ocr.Read(imagePath).Text;
            progress.Report((Interlocked.Increment(ref completed), imagePaths.Length));
        });

        return new Dictionary<string, string>(results);
    }

    public void ProcessBatchToSearchablePdfs(
        string[] imagePaths,
        string outputDirectory)
    {
        Parallel.ForEach(imagePaths, imagePath =>
        {
            var ocr = new IronTesseract();
            var result = ocr.Read(imagePath);

            var outputPath = Path.Combine(
                outputDirectory,
                Path.GetFileNameWithoutExtension(imagePath) + "-searchable.pdf");

            result.SaveAsSearchablePdf(outputPath);
        });
    }
}
C#

Google Cloud Vision版本是順序處理的,因為並行請求會增加暴露於速率限制。 每個ResourceExhausted例外都會導致完整的60秒停頓。 碰到配額10次的5000份文件批量會導致10分鐘的閑置等待。 IronOCR版本在所有可用核心上並行化而無需等待。 對於長時間運行的批量,進度跟踪API提供內建的進度回調,無需手動Interlocked.Increment接線。 對於批次掃描中的圖像質量問題,圖像質量修正指南涵蓋可以在每個Read調用之前新增的預處理管道。

Google Cloud Vision OCR API 到 IronOCR 映射參考

Google Cloud VisionIronOCR注意事項
ImageAnnotatorClient.Create()new IronTesseract()客戶端初始化; 不用憑證文件
Image.FromFile(path)input.LoadImage(path)IronTesseract上可用的直接路徑讀取
_client.DetectText(image)_ocr.Read(path).TextTEXT_DETECTION等價
_client.DetectDocumentText(image)_ocr.Read(path)DOCUMENT_TEXT_DETECTION等價; 模式是自動的
response[0].Descriptionresult.Text完整文件文字
TextAnnotationOcrResult頂層結果容器
annotation.Textresult.Text完整文字字串
annotation.Pages[i]result.Pages[i]逐頁存取
page.Blocks[i].Paragraphs[j]result.Paragraphs[i]IronOCR將段落暴露為平面集合
paragraph.Words.SelectMany(w => w.Symbols).Select(s => s.Text)paragraph.Text直接字串屬性; 無需符號迭代
word.BoundingBox.Verticesword.X, word.Y, word.Width, word.Height離散int屬性而不是頂點列表
word.Confidenceword.Confidence每個單詞的置信度分數
page.Confidenceresult.Confidence整體結果置信度
Feature.Types.Type.DocumentTextDetection自動IronOCR自動選擇處理模式
BatchAnnotateImagesRequestnew IronTesseract()本地並行處理; 無批量大小上限
_client.BatchAnnotateImages(requests)OcrInput對多圖像輸入的單次調用
AsyncBatchAnnotateFilesAsync()input.LoadPdf(); _ocr.Read(input)PDF處理是同步的; 不需要GCS
StorageClient.Create()不需要無GCS依賴性
storageClient.UploadObjectAsync()不需要PDF可直接從本地路徑或流載入
operation.PollUntilCompletedAsync()不需要處理是同步的
RpcException (StatusCode.ResourceExhausted)不適用無速率限制
RpcException (StatusCode.PermissionDenied)不適用無運行時身份驗證
GOOGLE_APPLICATION_CREDENTIALS環境變數IronOcr.License.LicenseKey字串分配,而不是文件路徑

常見的遷移問題與解決方案

問題1:在沒有GOOGLE_APPLICATION_CREDENTIALS時構造函式會拋出

**Google Cloud Vision:**如果未設置環境變數或指向無效的文件,StatusCode.PermissionDenied拋出。這一故障發生在啟動時,而不是在第一次API調用時,這意味著如果任何一個環境中缺少憑證,整個應用程式就無法初始化。

**解決方案:**在刪除Google Cloud Vision包後,從您的環境配置、CI/CD管道秘密、Kubernetes秘密和Docker Compose文件中刪除所有對GOOGLE_APPLICATION_CREDENTIALS的引用。 用單個IRONOCR_LICENSE環境變數替代:

// Remove this from every deployment environment:
// GOOGLE_APPLICATION_CREDENTIALS=/path/to/service-account.json

// Add this once at application startup:
IronOcr.License.LicenseKey = Environment.GetEnvironmentVariable("IRONOCR_LICENSE")
    ?? throw new InvalidOperationException("IRONOCR_LICENSE environment variable is required.");
C#

問題2:在命名空間移除後,Protobuf符號串聯程式碼失效

**Google Cloud Vision:**當使用Google.Cloud.Vision.V1命名空間移除後將產生編譯錯誤。 這些調用分散在消耗API響應的任何輔助類或服務類中。

**解決方案:**在程式碼庫中搜索所有w.Symbols模式,並將它們替換為IronOCR結果物件的直接屬性存取。 讀取結果指導指南涵蓋了OcrResult.Word上可用的每一個屬性:

# Find all Protobuf symbol concatenation patterns
grep -rn "\.Symbols\." --include="*.cs" .
grep -rn "SelectMany.*Symbols" --include="*.cs" .
grep -rn "w\.Symbols\.Select" --include="*.cs" .
SHELL

替換每一處:

// Before: symbol concatenation required by Protobuf schema
var text = string.Join("", paragraph.Words.SelectMany(w => w.Symbols).Select(s => s.Text));

// After: direct property on OcrResult.Paragraph
var text = paragraph.Text;
C#

問題3:在移除Storage.V1後PDF處理程式碼無法編譯

**Google Cloud Vision:**刪除PollUntilCompletedAsync的程式碼將無法編譯。這些程式碼可能會跨多個服務類,通常代表最大的單一塊變更。

**解決方案:**刪除整個GCS管道。用IronOCR三行等價碼替換50多行的異步方法。 對於保持異步簽名以維持調用相容性的程式碼,用Task.Run包裝:

// Delete: StorageClient, GCS upload, AsyncBatchAnnotateFilesAsync,
//         PollUntilCompletedAsync, output download, DeleteObjectAsync

// Replace with:
public async Task<string> ProcessPdfAsync(string pdfPath)
{
    return await Task.Run(() =>
    {
        using var input = new OcrInput();
        input.LoadPdf(pdfPath);
        return new IronTesseract().Read(input).Text;
    });
}
C#

對於新程式碼,使用本機異步OCR支持,而不是Task.Run包裝器。 PDF輸入指導涵蓋了頁面範圍選擇和受密碼保護的PDF載入。

問題4:速率限制重試邏輯不再需要

**Google Cloud Vision:**任何捕獲StatusCode.ResourceExhausted並實施等待和重試模式的程式碼都是寫來處理每分鐘1,800次請求配額。 這種重試邏輯可能嵌入在中間件、管道步驟或批處理迴圈中。

**解決方案:**刪除所有與配額錯誤相關的重試邏輯。 IronOCR在本地處理,無需外部配額。 錯誤處理合同從五個RpcException案例更改為兩個:

// Remove: all RpcException handlers for ResourceExhausted, PermissionDenied,
//         Unavailable, DeadlineExceeded, Unauthenticated

// IronOCR error surface:
try
{
    var result = new IronTesseract().Read(imagePath);
    if (result.Confidence < 50)
        input.DeNoise(); // add preprocessing for low-confidence results
    return result.Text;
}
catch (IOException ex)
{
    // File not found or locked
    throw new InvalidOperationException($"Cannot read: {imagePath}", ex);
}
catch (IronOcr.Exceptions.OcrException ex)
{
    // Processing failure — not a transient network error
    throw new InvalidOperationException($"OCR failed: {ex.Message}", ex);
}
C#

問題5:多頁TIFF需要幀提取迴圈

**Google Cloud Vision:**現有的TIFF處理程式碼可能使用System.Drawing.Image提取幀,將每個幀作為JPEG保存到臨時目錄,將每個JPEG作為單獨的API調用提交,並在之後刪除臨時文件。 這種模式每幀消耗一個配額單位,並可能在崩潰時留下孤立的臨時文件。

**解決方案:**用input.LoadImageFrames()替換幀提取迴圈和臨時文件管理。 整個System.Drawing幀迴圈被刪除:

// Remove: System.Drawing frame extraction, temp file writes, per-frame API calls

// Replace with:
using var input = new OcrInput();
input.LoadImageFrames(tiffPath);   // all frames, no temp files
var result = new IronTesseract().Read(input);
C#

查看TIFF和GIF輸入指南以獲取包括幀範圍選擇的多幀處理選項。

問題6:BoundingPoly頂點計算失效

**Google Cloud Vision:**從vertices[2].Y - vertices[0].Y。 遷移後,這些表達式在IronOCR中沒有等價物,因為Vertices不存在。

**解決方案:**用直接的int屬性替換頂點算術。 無需計算:

// Before: vertex index arithmetic
int x = word.BoundingBox.Vertices[0].X;
int y = word.BoundingBox.Vertices[0].Y;
int width  = word.BoundingBox.Vertices[1].X - word.BoundingBox.Vertices[0].X;
int height = word.BoundingBox.Vertices[2].Y - word.BoundingBox.Vertices[0].Y;

// After: direct properties
int x      = word.X;
int y      = word.Y;
int width  = word.Width;
int height = word.Height;
C#

Google Cloud Vision OCR 遷移檢查清單

遷移前

在進行任何更改之前,審核程式碼庫以識別所有Google Cloud Vision依賴性:

# Find all Google Cloud Vision namespace imports
grep -rn "using Google.Cloud.Vision" --include="*.cs" .
grep -rn "using Google.Cloud.Storage" --include="*.cs" .
grep -rn "using Grpc.Core" --include="*.cs" .

# Find ImageAnnotatorClient usage
grep -rn "ImageAnnotatorClient" --include="*.cs" .

# Find GCS pipeline code
grep -rn "StorageClient\|UploadObjectAsync\|DeleteObjectAsync" --include="*.cs" .
grep -rn "AsyncBatchAnnotateFilesAsync\|PollUntilCompleted" --include="*.cs" .

# Find Protobuf symbol concatenation
grep -rn "\.Symbols\." --include="*.cs" .
grep -rn "SelectMany.*Symbols" --include="*.cs" .

# Find BoundingPoly vertex calculations
grep -rn "BoundingPoly\|BoundingBox\.Vertices" --include="*.cs" .

# Find rate limit retry handlers
grep -rn "ResourceExhausted\|StatusCode\." --include="*.cs" .

# Find environment variable references
grep -rn "GOOGLE_APPLICATION_CREDENTIALS" .
SHELL

完成前的清單備註:

  • 列出為OCR輸入/輸出而建立的所有GCS儲存桶——在遷移後安排清理
  • 記錄服務帳戶電子郵件,以便在遷移後於GCP控制台中禁用它
  • 識別所有配置了GOOGLE_APPLICATION_CREDENTIALS的環境
  • 記下從配置中讀取GCP專案ID或儲存桶名稱的任何程式碼——在遷移後刪除

程式碼遷移

  1. 從所有專案中移除Google.Cloud.Vision.V1 NuGet包
  2. 從所有專案中移除Google.Cloud.Storage.V1 NuGet包
  3. 在所有進行OCR的專案中安裝IronOcr NuGet包
  4. 在應用程式啟動時新增IronOcr.License.LicenseKey初始化
  5. 將所有using Google.Cloud.Vision.V1導入替換為using IronOcr
  6. 替換所有using Google.Cloud.Storage.V1using Grpc.Core導入
  7. ImageAnnotatorClient.Create()替換為new IronTesseract()
  8. 刪除所有GCS管道方法(UploadObjectAsync、異步註釋、輪詢、下載、刪除)
  9. 替換所有PDF處理路徑的input.LoadPdf()(移除異步GCS協調)
  10. 用直接的.Text屬性存取替換所有Protobuf符號串聯迴圈
  11. BoundingPoly.Vertices索引計算替換為word.Xword.Yword.Widthword.Height
  12. 移除所有針對ResourceExhaustedPermissionDeniedUnavailableDeadlineExceededUnauthenticatedRpcException捕獲區塊
  13. input.LoadImageFrames()替換每幀的TIFF迴圈
  14. 將順序批量迴圈轉換為每執行緒IronTesseract實例
  15. 從所有環境配置、CI/CD管道、Docker Compose文件和Kubernetes秘密中移除GOOGLE_APPLICATION_CREDENTIALS

遷移後

  • 驗證錯誤處理程式碼中不再引用RpcExceptionGoogleApiException型別
  • 確認所有部署環境配置中都沒有GOOGLE_APPLICATION_CREDENTIALS
  • 在生產中使用的相同樣本文件集上運行OCR管道並比較文字輸出質量
  • 在之前由GCS異步管道處理的文件上測試PDF處理並確認文字輸出相同
  • 使用input.LoadPdf(path, Password: "...")測試受密碼保護的PDF——之前不支持
  • 使用input.LoadImageFrames()測試多頁TIFF處理並驗證所有幀都已處理
  • 在代表性樣本上運行批量處理器並確認輸出質量與先前結果相同
  • 確認result.Confidence值在您的文件集內的可接受範圍內
  • 使用result.SaveAsSearchablePdf()驗證可搜尋PDF輸出,之前需要單獨的PDF庫
  • 在沒有外部網路連接的環境中運行應用程式並確認OCR正常工作

遷移至IronOCR的主要好處

憑證表面減少至零文件。 遷移後,不再有JSON密鑰文件,無GCS儲存桶配置,無IAM角色,無服務帳戶,也無GOOGLE_APPLICATION_CREDENTIALS環境變數在您的基礎設施中。 整個憑證表面是一個包含許可密鑰字串的環境變數。 在Google Cloud Vision中是必須定期操作的密鑰輪轉不再是一個適用的概念。 對於在多個地區或雲供應商中運行的團隊,部署配置複雜性的減少是即時的。

無需外部依賴的PDF和TIFF處理。 GCS異步管道和System.Drawing TIFF frame迴圈被完全刪除。 input.LoadPdf()input.LoadImageFrames()是替代品——都同步,都是本地,都從調用到結果僅三行。 受密碼保護的PDF,這在Google Cloud Vision中是不可能的,現在隻需一個額外的參數即可工作。 PDF OCR指南TIFF輸入指南涵蓋了完整的輸入API。

在CPU速度下的批量處理。 移除每分鐘的1800次配額和強制60秒重試等待這意味著之前受速率限制的批量作業現在以可用處理器核心的速度運行。 一台擁有16個核心的機器能夠同時處理16個文件而無需外部批準。 Parallel.ForEach模式與每執行緒的IronTesseract實例共同是受限順序迴圈的直接替代。速度優化指南涵蓋了引擎配置選擇,這些選擇可以調整特定文件型別的吞吐量。

沒有Protobuf的結構化資料。 每個OcrResultTextConfidencePagesParagraphsLinesWordsCharacters公開為型別化.NET屬性,沒有Protobuf命名空間依賴,沒有符號串聯,也沒有用於邊界框的頂點算術。 需要20行巢狀迴圈來提取段落文字的程式碼已減少到result.Paragraphs.Select(p => p.Text)。 需要單詞級定位以進行文件佈局分析的用例,word.Xword.Yword.Widthword.Height可直接使用。 OCR結果特徵頁記錄了結果模型中的每個屬性。

內建的可搜索PDF輸出。 Google Cloud Vision僅返回文字——生產可搜索PDF需要單獨的PDF生成庫,這增加了另一個NuGet依賴,另一個API需要學習,並需要評估更多的授權。 IronOCR的result.SaveAsSearchablePdf(outputPath)從任何OCR結果中生產出完全可搜索的PDF。對於文件存檔和法律發現流水線,這消除了整個依賴性。 可搜索PDF範例完整展示了這一模式。

為受監管行業提供資料主權。 使用IronOCR處理的文件從未離開伺服器。 對於HIPAA涵蓋的健康記錄、ITAR控管的技術資料、CMMC範圍的國防承包商材料、律師-客戶特權法律文件和PCI-DSS範圍內的財務記錄,本地架構完全將第三方資料處理器類別從合規範圍中移除。 沒有商業夥伴協議需要協商,沒有資料保護協議需要執行,也不存在需要審查的Google資料保留政策。 IronOCR文件中心涵蓋了在Docker、Linux、Azure和AWS環境中適用資料駐留要求的部署配置。

請注意: Google Cloud Vision、Tesseract和iText是各自所有者的註冊商標。 本網站未獲Google或iText Group的認可或贊助。所有產品名稱、標誌和品牌均為其各自所有者的財產。 比較僅供資訊用途,並反映撰寫時獲得的公開資訊。

相關文章

Key in blue circle

立即免費取得 30 天試用金鑰

Your trial license will be sent to your email address

無任何限制。100% 解鎖。無需信用卡。

bullet_checked無需信用卡或建立帳號無任何限制。100% 解鎖。無需信用卡。
  • Logo Aetna
  • Logo NASA
  • Logo GE
  • Logo Porsche
  • Logo USDA
  • Logo Qatar
Join Millions of Engineers who’ve tried IronPDF
獲取您的無義務諮詢
填寫以下表格或發送電子郵件至sales@ironsoftware.com
您的詳細資訊將始終保密。
被全球數百萬工程師信任
Iron Software的客戶標誌
立即獲取您的30天試用金鑰
無需信用卡或帳戶建立