跳至頁尾內容
影片

如何在 C# 中修復圖片顏色以便閱讀

本指南指導.NET開發人員如何將OCR.space的REST API整合替換為IronOCR,它是一個作為單一NuGet包提供的原生.NET程式庫。 內容包括封包替換、命名空間清除,以及四種具體的程式碼遷移場景,專為REST到本地過渡而設:多部分上傳消除、base64編碼移除、OCR引擎選擇替換和結構化資料提取。 已閱讀階段1比較文章的開發人員會發現本指南著重於遷移本身的機械步驟,而非功能比較。

為什麼從OCR.space遷移

OCR.space滿足了特定需求:零成本的試驗,讓開發人員能在下午測試OCR而不需安裝任何東西。 問題是免費層是為原型設計的,而非生產使用。 一旦.NET應用程式轉向真實文件量、合規要求或團隊開發,每個OCR.space整合特性都對應用程式不利。

沒有NuGet包意味著沒有SDK和沒有IntelliSense。 OCR.space提供了一個REST端點和文件。 .NET整合──HTTP客戶端構建、請求序列化、回應反序列化、錯誤處理和重試邏輯──完全是開發人員的責任。 這不是一個小不便。 最低可行的客戶端在編寫第一個業務邏輯方法之前就需要80多行的基礎設施程式碼。 這段程式碼在每個.NET程式碼庫中的每個OCR.space整合中都一樣,並隨著時間的推移累積錯誤和維護負擔。

速率限制對生產應用程式施加了人為的上限。免費層限制每IP地址每分鐘60個請求和每天500個請求。 這兩個限制都是硬牆。 從午夜到次日午夜之間超過500個請求的應用程式會收到錯誤回應,直到計數器重置。 在共享辦公網路或共享CI/CD環境運行的生產系統可能在工作日結束前就耗盡日配額。

文件會在每次呼叫中離開您的基礎設施。 OCR.space沒有本地部署選項。 每個請求都會將文件──發票、醫療記錄、合同、身份文件──傳輸到OCR.space的雲伺服器。 禁止將敏感文件傳輸給第三方的HIPAA、GDPR和內部資料分類政策,無論合同控制如何,都使得OCR.space在架構上不相容。

免費層產生水印可搜尋的PDF。生成可搜尋的PDF輸出作為交付品的應用──文件存檔系統、合規平台、客戶面向的文件門戶──不能使用OCR.space的免費層。 水印嵌入在輸出的PDF中,沒有付費方案無法去除。

訂閱價格隨著量增長; OCR.space的PRO層每年$144,在第六年之前超過IronOCR的$999永久入門價格。 預計文件量將超過免費層門檻的團隊會面臨日漸增加的訂閱費用,對抗固定的永久許可。 $999 Lite授權涵蓋一位開發人員和一個部署地點,無每請求費用,不論任何量。 查看IronOCR 授權頁面了解層級詳情。

根本問題

OCR.space要求您構建完整的HTTP客戶端才能處理單個文件:

// OCR.space: 80+ lines of infrastructure before business logic
public class OcrSpaceApiClient : IDisposable
{
    private readonly HttpClient _httpClient;
    private readonly string _apiKey;
    private readonly SemaphoreSlim _rateLimiter; // You implement this

    public OcrSpaceApiClient(string apiKey)
    {
        _httpClient = new HttpClient();
        _httpClient.Timeout = TimeSpan.FromSeconds(120);
        _rateLimiter = new SemaphoreSlim(60, 60); // Free tier: 60/min
    }
    // ... 70+ more lines of HTTP plumbing follow
}
// OCR.space: 80+ lines of infrastructure before business logic
public class OcrSpaceApiClient : IDisposable
{
    private readonly HttpClient _httpClient;
    private readonly string _apiKey;
    private readonly SemaphoreSlim _rateLimiter; // You implement this

    public OcrSpaceApiClient(string apiKey)
    {
        _httpClient = new HttpClient();
        _httpClient.Timeout = TimeSpan.FromSeconds(120);
        _rateLimiter = new SemaphoreSlim(60, 60); // Free tier: 60/min
    }
    // ... 70+ more lines of HTTP plumbing follow
}
Imports System
Imports System.Net.Http
Imports System.Threading

' OCR.space: 80+ lines of infrastructure before business logic
Public Class OcrSpaceApiClient
    Implements IDisposable

    Private ReadOnly _httpClient As HttpClient
    Private ReadOnly _apiKey As String
    Private ReadOnly _rateLimiter As SemaphoreSlim ' You implement this

    Public Sub New(apiKey As String)
        _httpClient = New HttpClient()
        _httpClient.Timeout = TimeSpan.FromSeconds(120)
        _rateLimiter = New SemaphoreSlim(60, 60) ' Free tier: 60/min
    End Sub
    ' ... 70+ more lines of HTTP plumbing follow

    Public Sub Dispose() Implements IDisposable.Dispose
        _httpClient.Dispose()
        _rateLimiter.Dispose()
    End Sub
End Class
$vbLabelText   $csharpLabel

IronOCR是一個NuGet包。 整個客戶端已經寫好:

// IronOCR: no client to build, no rate limiter to manage
IronOcr.License.LicenseKey = "YOUR-LICENSE-KEY";
var result = new IronTesseract().Read("document.jpg");
Console.WriteLine(result.Text);
// IronOCR: no client to build, no rate limiter to manage
IronOcr.License.LicenseKey = "YOUR-LICENSE-KEY";
var result = new IronTesseract().Read("document.jpg");
Console.WriteLine(result.Text);
Imports IronOcr

' IronOCR: no client to build, no rate limiter to manage
IronOcr.License.LicenseKey = "YOUR-LICENSE-KEY"
Dim result = New IronTesseract().Read("document.jpg")
Console.WriteLine(result.Text)
$vbLabelText   $csharpLabel

IronOCRvs OCR.space: 功能比較

下表直接將OCR.space概念和限制映射到IronOCR等效功能。

功能 OCR.space IronOCR
NuGet套件 無——僅限REST API IronOcr — 原生 .NET
SDK / IntelliSense 無 — 手動JSON 全 — 型別化API
需要自訂模型 不是 不是
處理位置 OCR.space雲端伺服器 本地 — 內部處理
網路依賴 每次呼叫都需要 None
氣隙部署 不支持 全面支持
速率限制 60/分鐘,500/天(免費) None
檔案大小限制 5 MB(免費層) 只有可用記憶體
PDF輸入 是(有限,5 MB) 是 — 原生,無大小限制
可搜尋的PDF輸出 免費層的加水印PDF輸出 乾淨輸出,所有層級
自動前處理 伺服器端,無開發人員控制 糾偏,去噪,對比度,二值化,銳化
語言支持 ~25種語言 125+經由NuGet語言包
每個文件多語言 不支持 是 — OcrLanguage.French + OcrLanguage.German
結構化輸出(單詞,行) 只有純文字 頁面、段落、行、單詞帶座標
單詞等級信任分數 不可用 是 — word.Confidence
基於區域的OCR 不支持 是 — CropRectangle
條碼識別 不支持 是 — ReadBarCodes = true
可搜尋的PDF生成 水印(免費),乾淨(付費) 乾淨輸出 — 所有許可層級
HIPAA / GDPR相容性 風險 — 資料外傳 是 — 無外部資料傳輸
價格模型 每月訂閱 一次性永久
入門價格 $12/月($144/年) $999 一次性
.NET相容性 HttpClient — 任何 .NET .NET 4.6.2+、.NET 5/6/7/8/9
跨平台部署 需要對外部網路連接 Windows、Linux、macOS、Docker、Azure、AWS

快速開始:OCR.space到IronOCR遷移

步驟1:替換NuGet包

OCR.space沒有要解除安裝的NuGet包。 從專案移除所有OCR.space相關的基礎設施程式碼:SemaphoreSlim速率限制器,自訂結果模型,自訂例外型別。 IronOCR的NuGet包取代了所有這些。

IronOCR NuGet 頁面安裝IronOCR:

dotnet add package IronOcr

步驟2:更新命名空間

移除OCR.space HTTP和JSON名稱空間。 新增IronOCR名稱空間:

// Before (OCR.space — manually written infrastructure)
using System.Net.Http;
using System.Text.Json;
using System.Threading;

// After (IronOCR)
using IronOcr;
// Before (OCR.space — manually written infrastructure)
using System.Net.Http;
using System.Text.Json;
using System.Threading;

// After (IronOCR)
using IronOcr;
Imports System.Net.Http
Imports System.Text.Json
Imports System.Threading

Imports IronOcr
$vbLabelText   $csharpLabel

步驟3:初始化許可證

在應用程式啟動畫面新增授權初始化,而非每次請求:

// Program.cs or application startup
IronOcr.License.LicenseKey = "YOUR-LICENSE-KEY";
// Program.cs or application startup
IronOcr.License.LicenseKey = "YOUR-LICENSE-KEY";
Imports IronOcr

IronOcr.License.LicenseKey = "YOUR-LICENSE-KEY"
$vbLabelText   $csharpLabel

程式碼遷移範例

取代MultipartFormDataContent檔案上傳

OCR.space需要構建MultipartFormDataContent,其中包含檔案位元組和API金鑰,然後POST至雲端端點。 文件在每次呼叫時離開您的基礎設施。

OCR.space方法:

// MultipartFormDataContent: file upload to cloud on every request
public async Task<string> UploadAndExtract(string imagePath)
{
    using var content = new MultipartFormDataContent();
    var imageBytes = File.ReadAllBytes(imagePath);

    // Document is transmitted to OCR.space servers here
    content.Add(new ByteArrayContent(imageBytes), "file", Path.GetFileName(imagePath));
    content.Add(new StringContent(_apiKey), "apikey");
    content.Add(new StringContent("eng"), "language");
    content.Add(new StringContent("2"), "OCREngine"); // Select Engine 2

    var response = await _httpClient.PostAsync("https://api.ocr.space/parse/image", content);
    response.EnsureSuccessStatusCode();

    string json = await response.Content.ReadAsStringAsync();
    using var doc = JsonDocument.Parse(json);

    // Navigate JSON tree manually — no typed result
    return doc.RootElement
        .GetProperty("ParsedResults")[0]
        .GetProperty("ParsedText")
        .GetString() ?? string.Empty;
}
// MultipartFormDataContent: file upload to cloud on every request
public async Task<string> UploadAndExtract(string imagePath)
{
    using var content = new MultipartFormDataContent();
    var imageBytes = File.ReadAllBytes(imagePath);

    // Document is transmitted to OCR.space servers here
    content.Add(new ByteArrayContent(imageBytes), "file", Path.GetFileName(imagePath));
    content.Add(new StringContent(_apiKey), "apikey");
    content.Add(new StringContent("eng"), "language");
    content.Add(new StringContent("2"), "OCREngine"); // Select Engine 2

    var response = await _httpClient.PostAsync("https://api.ocr.space/parse/image", content);
    response.EnsureSuccessStatusCode();

    string json = await response.Content.ReadAsStringAsync();
    using var doc = JsonDocument.Parse(json);

    // Navigate JSON tree manually — no typed result
    return doc.RootElement
        .GetProperty("ParsedResults")[0]
        .GetProperty("ParsedText")
        .GetString() ?? string.Empty;
}
Imports System.IO
Imports System.Net.Http
Imports System.Text.Json
Imports System.Threading.Tasks

Public Class YourClassName
    Private _apiKey As String
    Private _httpClient As HttpClient

    Public Async Function UploadAndExtract(imagePath As String) As Task(Of String)
        Using content As New MultipartFormDataContent()
            Dim imageBytes = File.ReadAllBytes(imagePath)

            ' Document is transmitted to OCR.space servers here
            content.Add(New ByteArrayContent(imageBytes), "file", Path.GetFileName(imagePath))
            content.Add(New StringContent(_apiKey), "apikey")
            content.Add(New StringContent("eng"), "language")
            content.Add(New StringContent("2"), "OCREngine") ' Select Engine 2

            Dim response = Await _httpClient.PostAsync("https://api.ocr.space/parse/image", content)
            response.EnsureSuccessStatusCode()

            Dim json As String = Await response.Content.ReadAsStringAsync()
            Using doc = JsonDocument.Parse(json)
                ' Navigate JSON tree manually — no typed result
                Return doc.RootElement _
                    .GetProperty("ParsedResults")(0) _
                    .GetProperty("ParsedText") _
                    .GetString() OrElse String.Empty
            End Using
        End Using
    End Function
End Class
$vbLabelText   $csharpLabel

IronOCR方法:

// OcrInput replaces the entire upload + JSON pipeline
public string ExtractFromFile(string imagePath)
{
    using var input = new OcrInput();
    input.LoadImage(imagePath); // Stays local — no network call

    var ocr = new IronTesseract();
    var result = ocr.Read(input);

    return result.Text; // Typed property — no JSON navigation
}
// OcrInput replaces the entire upload + JSON pipeline
public string ExtractFromFile(string imagePath)
{
    using var input = new OcrInput();
    input.LoadImage(imagePath); // Stays local — no network call

    var ocr = new IronTesseract();
    var result = ocr.Read(input);

    return result.Text; // Typed property — no JSON navigation
}
Imports IronTesseract

Public Function ExtractFromFile(ByVal imagePath As String) As String
    Using input As New OcrInput()
        input.LoadImage(imagePath) ' Stays local — no network call

        Dim ocr As New IronTesseract()
        Dim result = ocr.Read(input)

        Return result.Text ' Typed property — no JSON navigation
    End Using
End Function
$vbLabelText   $csharpLabel

OcrInputMultipartFormDataContent 的本地替代品。 它通過一致的API接收文件路徑、位元組陣列、流和多頁TIFF。 HttpClient、API金鑰注入和JSON導航完全消失。 圖像輸入指南涵蓋每種支援的輸入格式。

消除Base64編碼

當OCR.space整合使用FormUrlEncodedContent中。 IronOCR直接接受原始位元組,無需編碼步驟。

OCR.space方法:

// base64Image parameter: read → encode → embed in form → POST → parse
public async Task<string> ExtractViaBase64(string imagePath)
{
    byte[] imageBytes = await File.ReadAllBytesAsync(imagePath);
    string base64Image = Convert.ToBase64String(imageBytes); // Mandatory encoding step

    // Embed as data URI — adds 33% overhead to payload size
    string mimeType = "image/png";
    string dataUri = $"data:{mimeType};base64,{base64Image}";

    var formContent = new FormUrlEncodedContent(new[]
    {
        new KeyValuePair<string, string>("apikey", _apiKey),
        new KeyValuePair<string, string>("base64Image", dataUri),
        new KeyValuePair<string, string>("language", "eng"),
        new KeyValuePair<string, string>("isOverlayRequired", "false")
    });

    var response = await _httpClient.PostAsync("https://api.ocr.space/parse/image", formContent);
    string json = await response.Content.ReadAsStringAsync();

    using var doc = JsonDocument.Parse(json);
    return doc.RootElement
        .GetProperty("ParsedResults")[0]
        .GetProperty("ParsedText")
        .GetString() ?? string.Empty;
}
// base64Image parameter: read → encode → embed in form → POST → parse
public async Task<string> ExtractViaBase64(string imagePath)
{
    byte[] imageBytes = await File.ReadAllBytesAsync(imagePath);
    string base64Image = Convert.ToBase64String(imageBytes); // Mandatory encoding step

    // Embed as data URI — adds 33% overhead to payload size
    string mimeType = "image/png";
    string dataUri = $"data:{mimeType};base64,{base64Image}";

    var formContent = new FormUrlEncodedContent(new[]
    {
        new KeyValuePair<string, string>("apikey", _apiKey),
        new KeyValuePair<string, string>("base64Image", dataUri),
        new KeyValuePair<string, string>("language", "eng"),
        new KeyValuePair<string, string>("isOverlayRequired", "false")
    });

    var response = await _httpClient.PostAsync("https://api.ocr.space/parse/image", formContent);
    string json = await response.Content.ReadAsStringAsync();

    using var doc = JsonDocument.Parse(json);
    return doc.RootElement
        .GetProperty("ParsedResults")[0]
        .GetProperty("ParsedText")
        .GetString() ?? string.Empty;
}
Imports System
Imports System.Collections.Generic
Imports System.IO
Imports System.Net.Http
Imports System.Text.Json
Imports System.Threading.Tasks

Public Class ImageProcessor
    Private _apiKey As String
    Private _httpClient As HttpClient

    Public Sub New(apiKey As String, httpClient As HttpClient)
        _apiKey = apiKey
        _httpClient = httpClient
    End Sub

    ' base64Image parameter: read → encode → embed in form → POST → parse
    Public Async Function ExtractViaBase64(imagePath As String) As Task(Of String)
        Dim imageBytes As Byte() = Await File.ReadAllBytesAsync(imagePath)
        Dim base64Image As String = Convert.ToBase64String(imageBytes) ' Mandatory encoding step

        ' Embed as data URI — adds 33% overhead to payload size
        Dim mimeType As String = "image/png"
        Dim dataUri As String = $"data:{mimeType};base64,{base64Image}"

        Dim formContent As New FormUrlEncodedContent(New KeyValuePair(Of String, String)() {
            New KeyValuePair(Of String, String)("apikey", _apiKey),
            New KeyValuePair(Of String, String)("base64Image", dataUri),
            New KeyValuePair(Of String, String)("language", "eng"),
            New KeyValuePair(Of String, String)("isOverlayRequired", "false")
        })

        Dim response As HttpResponseMessage = Await _httpClient.PostAsync("https://api.ocr.space/parse/image", formContent)
        Dim json As String = Await response.Content.ReadAsStringAsync()

        Using doc As JsonDocument = JsonDocument.Parse(json)
            Return doc.RootElement _
                .GetProperty("ParsedResults")(0) _
                .GetProperty("ParsedText") _
                .GetString() OrElse String.Empty
        End Using
    End Function
End Class
$vbLabelText   $csharpLabel

IronOCR方法:

// LoadImage(bytes): raw bytes accepted directly — no encoding
public string ExtractFromBytes(byte[] imageBytes)
{
    using var input = new OcrInput();
    input.LoadImage(imageBytes); //不是Base64, no data URI, no overhead

    var result = new IronTesseract().Read(input);
    return result.Text;
}
// LoadImage(bytes): raw bytes accepted directly — no encoding
public string ExtractFromBytes(byte[] imageBytes)
{
    using var input = new OcrInput();
    input.LoadImage(imageBytes); //不是Base64, no data URI, no overhead

    var result = new IronTesseract().Read(input);
    return result.Text;
}
Imports IronOcr

Public Function ExtractFromBytes(imageBytes As Byte()) As String
    Using input As New OcrInput()
        input.LoadImage(imageBytes) '不是Base64, no data URI, no overhead

        Dim result = New IronTesseract().Read(input)
        Return result.Text
    End Using
End Function
$vbLabelText   $csharpLabel

IronOCR中不存在Base64編碼步驟,因為沒有HTTP傳輸層。 原始位元組直接進入OcrInput.LoadImage()。 資料URI的開銷 — Base64編碼會將有效負載大小增加約33% — 也消失。 流輸入指南顯示了相同的Stream輸入模式,當位元組來自於上傳處理程式或記憶體緩衝而非文件時特別有用。

用圖像預處理替換OCR引擎選擇

OCR.space通過OCREngine表單參數公開兩個OCR引擎:引擎1在復雜佈局上速度快但準確性低; 引擎2在大多數文件型別上速度略慢但準確性更高。開發人員根據文件特徵在每次呼叫時選擇引擎。 IronOCR運行一個優化的Tesseract 5引擎,但提供明確的預處理過濾器,解決文件品質的根本原因,而不是在引擎模式之間切換。

OCR.space方法:

// OCREngine parameter: binary choice, no control over why accuracy differs
public async Task<string> ExtractWithEngineSelection(
    string imagePath,
    bool useHighAccuracyEngine = true)
{
    byte[] imageBytes = await File.ReadAllBytesAsync(imagePath);
    string base64 = Convert.ToBase64String(imageBytes);

    var formContent = new FormUrlEncodedContent(new[]
    {
        new KeyValuePair<string, string>("apikey", _apiKey),
        new KeyValuePair<string, string>("base64Image", $"data:image/png;base64,{base64}"),
        new KeyValuePair<string, string>("language", "eng"),
        // Engine 1 = faster, Engine 2 = higher accuracy — binary choice only
        new KeyValuePair<string, string>("OCREngine", useHighAccuracyEngine ? "2" : "1"),
        new KeyValuePair<string, string>("scale", "true"),
        new KeyValuePair<string, string>("detectOrientation", "true")
    });

    var response = await _httpClient.PostAsync("https://api.ocr.space/parse/image", formContent);
    string json = await response.Content.ReadAsStringAsync();

    using var doc = JsonDocument.Parse(json);
    return doc.RootElement
        .GetProperty("ParsedResults")[0]
        .GetProperty("ParsedText")
        .GetString() ?? string.Empty;
}
// OCREngine parameter: binary choice, no control over why accuracy differs
public async Task<string> ExtractWithEngineSelection(
    string imagePath,
    bool useHighAccuracyEngine = true)
{
    byte[] imageBytes = await File.ReadAllBytesAsync(imagePath);
    string base64 = Convert.ToBase64String(imageBytes);

    var formContent = new FormUrlEncodedContent(new[]
    {
        new KeyValuePair<string, string>("apikey", _apiKey),
        new KeyValuePair<string, string>("base64Image", $"data:image/png;base64,{base64}"),
        new KeyValuePair<string, string>("language", "eng"),
        // Engine 1 = faster, Engine 2 = higher accuracy — binary choice only
        new KeyValuePair<string, string>("OCREngine", useHighAccuracyEngine ? "2" : "1"),
        new KeyValuePair<string, string>("scale", "true"),
        new KeyValuePair<string, string>("detectOrientation", "true")
    });

    var response = await _httpClient.PostAsync("https://api.ocr.space/parse/image", formContent);
    string json = await response.Content.ReadAsStringAsync();

    using var doc = JsonDocument.Parse(json);
    return doc.RootElement
        .GetProperty("ParsedResults")[0]
        .GetProperty("ParsedText")
        .GetString() ?? string.Empty;
}
Imports System.IO
Imports System.Net.Http
Imports System.Text.Json
Imports System.Threading.Tasks

Public Class OCRService
    Private _apiKey As String
    Private _httpClient As HttpClient

    Public Sub New(apiKey As String, httpClient As HttpClient)
        _apiKey = apiKey
        _httpClient = httpClient
    End Sub

    ' OCREngine parameter: binary choice, no control over why accuracy differs
    Public Async Function ExtractWithEngineSelection(imagePath As String, Optional useHighAccuracyEngine As Boolean = True) As Task(Of String)
        Dim imageBytes As Byte() = Await File.ReadAllBytesAsync(imagePath)
        Dim base64 As String = Convert.ToBase64String(imageBytes)

        Dim formContent As New FormUrlEncodedContent(New KeyValuePair(Of String, String)() {
            New KeyValuePair(Of String, String)("apikey", _apiKey),
            New KeyValuePair(Of String, String)("base64Image", $"data:image/png;base64,{base64}"),
            New KeyValuePair(Of String, String)("language", "eng"),
            ' Engine 1 = faster, Engine 2 = higher accuracy — binary choice only
            New KeyValuePair(Of String, String)("OCREngine", If(useHighAccuracyEngine, "2", "1")),
            New KeyValuePair(Of String, String)("scale", "true"),
            New KeyValuePair(Of String, String)("detectOrientation", "true")
        })

        Dim response As HttpResponseMessage = Await _httpClient.PostAsync("https://api.ocr.space/parse/image", formContent)
        Dim json As String = Await response.Content.ReadAsStringAsync()

        Using doc As JsonDocument = JsonDocument.Parse(json)
            Return doc.RootElement _
                .GetProperty("ParsedResults")(0) _
                .GetProperty("ParsedText") _
                .GetString() OrElse String.Empty
        End Using
    End Function
End Class
$vbLabelText   $csharpLabel

IronOCR方法:

// Preprocessing pipeline: fix the document, not the engine selection
public string ExtractWithPreprocessing(string imagePath)
{
    using var input = new OcrInput();
    input.LoadImage(imagePath);

    // Apply filters that match the document's specific quality issues
    input.Deskew();         // Correct rotation — replaces detectOrientation
    input.DeNoise();        // Remove noise from fax/photocopier artifacts
    input.Contrast();       // Enhance contrast on low-quality scans
    input.Scale(200);       // Upscale small or low-DPI images

    var ocr = new IronTesseract();
    var result = ocr.Read(input);

    Console.WriteLine($"Confidence: {result.Confidence}%"); //不是equivalent in OCR.space
    return result.Text;
}
// Preprocessing pipeline: fix the document, not the engine selection
public string ExtractWithPreprocessing(string imagePath)
{
    using var input = new OcrInput();
    input.LoadImage(imagePath);

    // Apply filters that match the document's specific quality issues
    input.Deskew();         // Correct rotation — replaces detectOrientation
    input.DeNoise();        // Remove noise from fax/photocopier artifacts
    input.Contrast();       // Enhance contrast on low-quality scans
    input.Scale(200);       // Upscale small or low-DPI images

    var ocr = new IronTesseract();
    var result = ocr.Read(input);

    Console.WriteLine($"Confidence: {result.Confidence}%"); //不是equivalent in OCR.space
    return result.Text;
}
Imports IronOcr

Public Class PreprocessingPipeline
    ' Preprocessing pipeline: fix the document, not the engine selection
    Public Function ExtractWithPreprocessing(imagePath As String) As String
        Using input As New OcrInput()
            input.LoadImage(imagePath)

            ' Apply filters that match the document's specific quality issues
            input.Deskew()         ' Correct rotation — replaces detectOrientation
            input.DeNoise()        ' Remove noise from fax/photocopier artifacts
            input.Contrast()       ' Enhance contrast on low-quality scans
            input.Scale(200)       ' Upscale small or low-DPI images

            Dim ocr As New IronTesseract()
            Dim result = ocr.Read(input)

            Console.WriteLine($"Confidence: {result.Confidence}%") '不是equivalent in OCR.space
            Return result.Text
        End Using
    End Function
End Class
$vbLabelText   $csharpLabel

OCR.space的OCREngine參數是文件品質的代理 — 當引擎1在某份文件上失敗時,開發人員切換到引擎2,希望不同的算法能彌補。 IronOCR的預處理管道直接解決了品質問題:Contrast()從低對比度複印件中恢復文字。 結果的OCREngine切換無法提供。圖像品質修正指南過濾器嚮導文件化了每個過濾器對不同文件型別的影響。

多語言OCR無需逐呼叫語言切換

OCR.space接受每次API呼叫的一個language參數。 包含混合語言的文件需要為每種語言單獨呼叫,並手動合併結果。 IronOCR使用OcrLanguage值上一個讀取操作中同時處理多種語言。

OCR.space方法:

// OCR.space: one language per call — multi-language requires multiple requests
public async Task<string> ExtractMultiLanguage(string imagePath)
{
    // First pass: English
    string englishText = await ExtractWithLanguage(imagePath, "eng");

    // Second pass: French (consumes another rate-limit slot, another API call)
    string frenchText = await ExtractWithLanguage(imagePath, "fre");

    // Manually merge results — no way to know which text belongs to which language
    return $"{englishText}\n{frenchText}";
}

private async Task<string> ExtractWithLanguage(string imagePath, string langCode)
{
    byte[] imageBytes = await File.ReadAllBytesAsync(imagePath);
    string base64 = Convert.ToBase64String(imageBytes);

    var content = new FormUrlEncodedContent(new[]
    {
        new KeyValuePair<string, string>("apikey", _apiKey),
        new KeyValuePair<string, string>("base64Image", $"data:image/png;base64,{base64}"),
        new KeyValuePair<string, string>("language", langCode) // One language per call
    });

    var response = await _httpClient.PostAsync("https://api.ocr.space/parse/image", content);
    string json = await response.Content.ReadAsStringAsync();

    using var doc = JsonDocument.Parse(json);
    return doc.RootElement
        .GetProperty("ParsedResults")[0]
        .GetProperty("ParsedText")
        .GetString() ?? string.Empty;
}
// OCR.space: one language per call — multi-language requires multiple requests
public async Task<string> ExtractMultiLanguage(string imagePath)
{
    // First pass: English
    string englishText = await ExtractWithLanguage(imagePath, "eng");

    // Second pass: French (consumes another rate-limit slot, another API call)
    string frenchText = await ExtractWithLanguage(imagePath, "fre");

    // Manually merge results — no way to know which text belongs to which language
    return $"{englishText}\n{frenchText}";
}

private async Task<string> ExtractWithLanguage(string imagePath, string langCode)
{
    byte[] imageBytes = await File.ReadAllBytesAsync(imagePath);
    string base64 = Convert.ToBase64String(imageBytes);

    var content = new FormUrlEncodedContent(new[]
    {
        new KeyValuePair<string, string>("apikey", _apiKey),
        new KeyValuePair<string, string>("base64Image", $"data:image/png;base64,{base64}"),
        new KeyValuePair<string, string>("language", langCode) // One language per call
    });

    var response = await _httpClient.PostAsync("https://api.ocr.space/parse/image", content);
    string json = await response.Content.ReadAsStringAsync();

    using var doc = JsonDocument.Parse(json);
    return doc.RootElement
        .GetProperty("ParsedResults")[0]
        .GetProperty("ParsedText")
        .GetString() ?? string.Empty;
}
Imports System.IO
Imports System.Net.Http
Imports System.Text.Json
Imports System.Threading.Tasks

Public Class OCRSpace
    Private _apiKey As String
    Private _httpClient As HttpClient

    Public Async Function ExtractMultiLanguage(imagePath As String) As Task(Of String)
        ' First pass: English
        Dim englishText As String = Await ExtractWithLanguage(imagePath, "eng")

        ' Second pass: French (consumes another rate-limit slot, another API call)
        Dim frenchText As String = Await ExtractWithLanguage(imagePath, "fre")

        ' Manually merge results — no way to know which text belongs to which language
        Return $"{englishText}{vbLf}{frenchText}"
    End Function

    Private Async Function ExtractWithLanguage(imagePath As String, langCode As String) As Task(Of String)
        Dim imageBytes As Byte() = Await File.ReadAllBytesAsync(imagePath)
        Dim base64 As String = Convert.ToBase64String(imageBytes)

        Dim content = New FormUrlEncodedContent(New KeyValuePair(Of String, String)() {
            New KeyValuePair(Of String, String)("apikey", _apiKey),
            New KeyValuePair(Of String, String)("base64Image", $"data:image/png;base64,{base64}"),
            New KeyValuePair(Of String, String)("language", langCode) ' One language per call
        })

        Dim response = Await _httpClient.PostAsync("https://api.ocr.space/parse/image", content)
        Dim json As String = Await response.Content.ReadAsStringAsync()

        Using doc = JsonDocument.Parse(json)
            Return doc.RootElement _
                .GetProperty("ParsedResults")(0) _
                .GetProperty("ParsedText") _
                .GetString() OrElse String.Empty
        End Using
    End Function
End Class
$vbLabelText   $csharpLabel

IronOCR方法:

// IronOCR: multiple languages in a single read — one pass, correct output
public string ExtractMultiLanguage(string imagePath)
{
    var ocr = new IronTesseract();

    // Combine languages with + operator — processed simultaneously
    ocr.Language = OcrLanguage.English + OcrLanguage.French + OcrLanguage.German;

    var result = ocr.Read(imagePath);
    return result.Text; // Correctly interleaved multilingual output
}
// IronOCR: multiple languages in a single read — one pass, correct output
public string ExtractMultiLanguage(string imagePath)
{
    var ocr = new IronTesseract();

    // Combine languages with + operator — processed simultaneously
    ocr.Language = OcrLanguage.English + OcrLanguage.French + OcrLanguage.German;

    var result = ocr.Read(imagePath);
    return result.Text; // Correctly interleaved multilingual output
}
Imports IronOcr

Public Function ExtractMultiLanguage(imagePath As String) As String
    Dim ocr As New IronTesseract()

    ' Combine languages with + operator — processed simultaneously
    ocr.Language = OcrLanguage.English + OcrLanguage.French + OcrLanguage.German

    Dim result = ocr.Read(imagePath)
    Return result.Text ' Correctly interleaved multilingual output
End Function
$vbLabelText   $csharpLabel

OCR.space的單次呼叫單一語言限制迫使開發人員對於N語言文件進行N次API呼叫,並猜測如何對結果進行和解。 IronOCR將語言模型合併到一個引擎傳遞中,生成正確交叉的輸出,無需後處理。 語言包作為NuGet包安裝 — IronOcr.Languages.German等等 — 並且可以脫機使用。多語言指南涵蓋了包安裝和+運算子語法,支援的語言超過125種。

帶有詞座標的結構化資料提取

OCR.space從ParsedResults[0].ParsedText返回純文字。 沒有單詞級別資料、沒有邊框、沒有行界限,也沒有逐元素的信任分數。 需要定位特定欄位的應用程式 — 如發票右上角的日期,表格右下角的總數 — 沒有來自OCR.space回應的結構化基礎可供構建。 IronOCR提供了完整的文件層次結構:頁面、段落、行、單詞和字元,每個都有像素座標和信任分數。

OCR.space方法:

// OCR.space: plain text only — no structure, no coordinates
public async Task<string> ExtractInvoiceFields(string invoicePath)
{
    byte[] invoiceBytes = await File.ReadAllBytesAsync(invoicePath);
    string base64 = Convert.ToBase64String(invoiceBytes);

    var content = new FormUrlEncodedContent(new[]
    {
        new KeyValuePair<string, string>("apikey", _apiKey),
        new KeyValuePair<string, string>("base64Image", $"data:application/pdf;base64,{base64}"),
        new KeyValuePair<string, string>("filetype", "PDF"),
        new KeyValuePair<string, string>("language", "eng"),
        // isOverlayRequired=true returns word boxes, but only as raw JSON coordinates
        new KeyValuePair<string, string>("isOverlayRequired", "true")
    });

    var response = await _httpClient.PostAsync("https://api.ocr.space/parse/image", content);
    string json = await response.Content.ReadAsStringAsync();

    // Navigate deeply-nested JSON to find word boxes — no typed models
    using var doc = JsonDocument.Parse(json);
    var overlay = doc.RootElement
        .GetProperty("ParsedResults")[0]
        .GetProperty("TextOverlay");

    // Parse word coordinate arrays manually — fragile JSON path traversal
    var wordData = new List<(string word, int x, int y)>();
    foreach (var line in overlay.GetProperty("Lines").EnumerateArray())
    {
        foreach (var word in line.GetProperty("Words").EnumerateArray())
        {
            string wordText = word.GetProperty("WordText").GetString() ?? "";
            int left = word.GetProperty("Left").GetInt32();
            int top = word.GetProperty("Top").GetInt32();
            wordData.Add((wordText, left, top));
        }
    }

    // Reconstruct full text from raw JSON — still no typed result
    return string.Join(" ", wordData.Select(w => w.word));
}
// OCR.space: plain text only — no structure, no coordinates
public async Task<string> ExtractInvoiceFields(string invoicePath)
{
    byte[] invoiceBytes = await File.ReadAllBytesAsync(invoicePath);
    string base64 = Convert.ToBase64String(invoiceBytes);

    var content = new FormUrlEncodedContent(new[]
    {
        new KeyValuePair<string, string>("apikey", _apiKey),
        new KeyValuePair<string, string>("base64Image", $"data:application/pdf;base64,{base64}"),
        new KeyValuePair<string, string>("filetype", "PDF"),
        new KeyValuePair<string, string>("language", "eng"),
        // isOverlayRequired=true returns word boxes, but only as raw JSON coordinates
        new KeyValuePair<string, string>("isOverlayRequired", "true")
    });

    var response = await _httpClient.PostAsync("https://api.ocr.space/parse/image", content);
    string json = await response.Content.ReadAsStringAsync();

    // Navigate deeply-nested JSON to find word boxes — no typed models
    using var doc = JsonDocument.Parse(json);
    var overlay = doc.RootElement
        .GetProperty("ParsedResults")[0]
        .GetProperty("TextOverlay");

    // Parse word coordinate arrays manually — fragile JSON path traversal
    var wordData = new List<(string word, int x, int y)>();
    foreach (var line in overlay.GetProperty("Lines").EnumerateArray())
    {
        foreach (var word in line.GetProperty("Words").EnumerateArray())
        {
            string wordText = word.GetProperty("WordText").GetString() ?? "";
            int left = word.GetProperty("Left").GetInt32();
            int top = word.GetProperty("Top").GetInt32();
            wordData.Add((wordText, left, top));
        }
    }

    // Reconstruct full text from raw JSON — still no typed result
    return string.Join(" ", wordData.Select(w => w.word));
}
Imports System.IO
Imports System.Net.Http
Imports System.Text.Json
Imports System.Threading.Tasks

Public Class InvoiceProcessor
    Private _apiKey As String
    Private _httpClient As HttpClient

    Public Sub New(apiKey As String, httpClient As HttpClient)
        _apiKey = apiKey
        _httpClient = httpClient
    End Sub

    Public Async Function ExtractInvoiceFields(invoicePath As String) As Task(Of String)
        Dim invoiceBytes As Byte() = Await File.ReadAllBytesAsync(invoicePath)
        Dim base64 As String = Convert.ToBase64String(invoiceBytes)

        Dim content As New FormUrlEncodedContent(New KeyValuePair(Of String, String)() {
            New KeyValuePair(Of String, String)("apikey", _apiKey),
            New KeyValuePair(Of String, String)("base64Image", $"data:application/pdf;base64,{base64}"),
            New KeyValuePair(Of String, String)("filetype", "PDF"),
            New KeyValuePair(Of String, String)("language", "eng"),
            New KeyValuePair(Of String, String)("isOverlayRequired", "true")
        })

        Dim response As HttpResponseMessage = Await _httpClient.PostAsync("https://api.ocr.space/parse/image", content)
        Dim json As String = Await response.Content.ReadAsStringAsync()

        Using doc As JsonDocument = JsonDocument.Parse(json)
            Dim overlay = doc.RootElement _
                .GetProperty("ParsedResults")(0) _
                .GetProperty("TextOverlay")

            Dim wordData As New List(Of (word As String, x As Integer, y As Integer))()
            For Each line In overlay.GetProperty("Lines").EnumerateArray()
                For Each word In line.GetProperty("Words").EnumerateArray()
                    Dim wordText As String = word.GetProperty("WordText").GetString() OrElse ""
                    Dim left As Integer = word.GetProperty("Left").GetInt32()
                    Dim top As Integer = word.GetProperty("Top").GetInt32()
                    wordData.Add((wordText, left, top))
                Next
            Next

            Return String.Join(" ", wordData.Select(Function(w) w.word))
        End Using
    End Function
End Class
$vbLabelText   $csharpLabel

IronOCR方法:

// IronOCR: full document hierarchy — typed, no JSON, no coordinates parsing
public void ExtractInvoiceFields(string invoicePath)
{
    var ocr = new IronTesseract();
    var result = ocr.Read(invoicePath);

    // Access the full document hierarchy — all strongly typed
    foreach (var page in result.Pages)
    {
        foreach (var paragraph in page.Paragraphs)
        {
            Console.WriteLine($"Paragraph at ({paragraph.X}, {paragraph.Y}): {paragraph.Text}");
        }

        foreach (var word in page.Words)
        {
            // Word-level confidence — identify low-quality extractions
            if (word.Confidence < 70)
                Console.WriteLine($"Low confidence word '{word.Text}' at ({word.X}, {word.Y})");
        }
    }

    // Or use region-based OCR to target specific invoice zones directly
    var totalRegion = new CropRectangle(400, 700, 200, 50); // Bottom-right total field
    using var input = new OcrInput();
    input.LoadImage(invoicePath, totalRegion);
    string totalText = ocr.Read(input).Text;
    Console.WriteLine($"Invoice total: {totalText}");
}
// IronOCR: full document hierarchy — typed, no JSON, no coordinates parsing
public void ExtractInvoiceFields(string invoicePath)
{
    var ocr = new IronTesseract();
    var result = ocr.Read(invoicePath);

    // Access the full document hierarchy — all strongly typed
    foreach (var page in result.Pages)
    {
        foreach (var paragraph in page.Paragraphs)
        {
            Console.WriteLine($"Paragraph at ({paragraph.X}, {paragraph.Y}): {paragraph.Text}");
        }

        foreach (var word in page.Words)
        {
            // Word-level confidence — identify low-quality extractions
            if (word.Confidence < 70)
                Console.WriteLine($"Low confidence word '{word.Text}' at ({word.X}, {word.Y})");
        }
    }

    // Or use region-based OCR to target specific invoice zones directly
    var totalRegion = new CropRectangle(400, 700, 200, 50); // Bottom-right total field
    using var input = new OcrInput();
    input.LoadImage(invoicePath, totalRegion);
    string totalText = ocr.Read(input).Text;
    Console.WriteLine($"Invoice total: {totalText}");
}
Imports IronOcr

Public Sub ExtractInvoiceFields(invoicePath As String)
    Dim ocr As New IronTesseract()
    Dim result = ocr.Read(invoicePath)

    ' Access the full document hierarchy — all strongly typed
    For Each page In result.Pages
        For Each paragraph In page.Paragraphs
            Console.WriteLine($"Paragraph at ({paragraph.X}, {paragraph.Y}): {paragraph.Text}")
        Next

        For Each word In page.Words
            ' Word-level confidence — identify low-quality extractions
            If word.Confidence < 70 Then
                Console.WriteLine($"Low confidence word '{word.Text}' at ({word.X}, {word.Y})")
            End If
        Next
    Next

    ' Or use region-based OCR to target specific invoice zones directly
    Dim totalRegion As New CropRectangle(400, 700, 200, 50) ' Bottom-right total field
    Using input As New OcrInput()
        input.LoadImage(invoicePath, totalRegion)
        Dim totalText As String = ocr.Read(input).Text
        Console.WriteLine($"Invoice total: {totalText}")
    End Using
End Sub
$vbLabelText   $csharpLabel

OCR.space的isOverlayRequired=true標誌返回JSON單詞座標,但回應結構需要通過字串鍵屬性存取導航巢狀JSON陣列 — 沒有型別化模型,沒有IntelliSense,如果回應結構改變,則脆弱的路徑遍歷會崩潰。 IronOCR的result.Lines是型別化.NET物件。 CropRectangle方法直接針對特定文件區域,而不是在提取整個文件後按座標篩選。 讀取結果指南基於區域的OCR指南詳細介紹了這兩種模式。

OCR.space API到IronOCR映射參考

OCR.space概念 IronOCR 等效
無NuGet包 dotnet add package IronOcr
HttpClient 構建 不需要 — 無HTTP層
SemaphoreSlim 速率限制器 不需要——無速率限制
FormUrlEncodedContent / MultipartFormDataContent OcrInput
base64Image 資料URI參數 input.LoadImage(bytes)
file 上載參數 input.LoadImage(path)
apikey 頭/表單字段 IronOcr.License.LicenseKey (應用啟動時一次)
language 參數(一個在每次呼叫中) ocr.Language = OcrLanguage.English + OcrLanguage.French
OCREngine=1(快速) 預設引擎(優化的Tesseract 5)
OCREngine=2 (高準確度) input.Deskew(); input.DeNoise(); input.Contrast();
scale=true 參數 input.Scale(200)
detectOrientation=true 參數 input.Deskew()
isOverlayRequired=true 參數 result.Pages[n].Words(始終可用,型別化)
isCreateSearchablePdf=true 參數 result.SaveAsSearchablePdf("output.pdf")
filetype=PDF 參數 input.LoadPdf(path)
ParsedResults[0].ParsedText result.Text
ParsedResults[n](逐頁文字) result.Pages[n].Text
TextOverlay.Lines[n].Words[n].WordText result.Pages[n].Words[n].Text
TextOverlay.Lines[n].Words[n].Left/Top result.Pages[n].Words[n].X / .Y
IsErroredOnProcessing JSON標誌 標準Exception帶資訊
FileParseExitCode 每頁標誌 標準Exception帶資訊
HTTP 429 請求過多 不適用 — 無速率限制
自訂 OcrResult POCO(使用者定義) IronOcr.OcrResult(由NuGet提供)
自訂 OcrSpaceException(使用者定義) 標準.NET例外型別

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

問題1:僅為HTTP存在的非同步程式碼

OCR.space:每個OCR呼叫都是async,因為涉及到雲端的HTTP往返。 服務方法、控制器操作和背景作業被設計為非同步,避免在網路等待時封鎖執行緒。

解決方案: IronOCR的Read()方法是同步的。 從完全因為OCR.space需要而成為非同步的方法中移除await。 在ASP.NET Core環境中,非阻塞執行至關重要,將同步呼叫包裹在Task.Run()中,或使用非同步OCR指南中記錄的非同步模式。 不要立即將await新增到IronOCR呼叫中 — 沒有必要,且在非網頁上下文中會增加不必要的開銷。

// Before: async because OCR.space required network I/O
public async Task<string> ProcessDocumentAsync(string path)
{
    return await _ocrSpaceClient.ExtractTextAsync(path); // Network wait
}

// After: synchronous — no network, no async needed
public string ProcessDocument(string path)
{
    return _ocr.Read(path).Text; // Local execution
}
// Before: async because OCR.space required network I/O
public async Task<string> ProcessDocumentAsync(string path)
{
    return await _ocrSpaceClient.ExtractTextAsync(path); // Network wait
}

// After: synchronous — no network, no async needed
public string ProcessDocument(string path)
{
    return _ocr.Read(path).Text; // Local execution
}
Imports System.Threading.Tasks

' Before: async because OCR.space required network I/O
Public Async Function ProcessDocumentAsync(path As String) As Task(Of String)
    Return Await _ocrSpaceClient.ExtractTextAsync(path) ' Network wait
End Function

' After: synchronous — no network, no async needed
Public Function ProcessDocument(path As String) As String
    Return _ocr.Read(path).Text ' Local execution
End Function
$vbLabelText   $csharpLabel

問題2:API金鑰儲存和旋轉基礎設施

OCR.space: API金鑰必須在每次請求中插入。 團隊通常將其儲存在IOptions<t>或構造函式注入它,並在暴露後進行旋轉。 金鑰旋轉需要更新每個部署環境並重新啟動應用程式。

解決方案: IronOCR的授權金鑰在啟動時設置一次,在執行期間不再引用。 移除每次請求的金鑰注入模式。 移除IOptions<OcrSpaceSettings>配置類。 金鑰初始化模式是一行:

// Startup.cs or Program.cs — once only
IronOcr.License.LicenseKey = Environment.GetEnvironmentVariable("IRONOCR_LICENSE_KEY");
// Startup.cs or Program.cs — once only
IronOcr.License.LicenseKey = Environment.GetEnvironmentVariable("IRONOCR_LICENSE_KEY");
Imports System

' Startup.vb or Program.vb — once only
IronOcr.License.LicenseKey = Environment.GetEnvironmentVariable("IRONOCR_LICENSE_KEY")
$vbLabelText   $csharpLabel

沒有每次請求的認證注入,沒有金鑰旋轉過程,也沒有在請求跟蹤中意外記錄金鑰的風險。

問題3:文件大小預驗證邏輯

OCR.space:免費層拒絕超過5 MB的文件,並給予錯誤回應。 生產程式碼在每次請求之前新增文件大小檢查,以避免在會失敗的呼叫上浪費速率限制槽:

// OCR.space: pre-validation to avoid wasting quota on large files
var fileInfo = new FileInfo(filePath);
if (fileInfo.Length > 5 * 1024 * 1024)
    throw new InvalidOperationException("File exceeds 5MB free tier limit.");
// OCR.space: pre-validation to avoid wasting quota on large files
var fileInfo = new FileInfo(filePath);
if (fileInfo.Length > 5 * 1024 * 1024)
    throw new InvalidOperationException("File exceeds 5MB free tier limit.");
Dim fileInfo As New FileInfo(filePath)
If fileInfo.Length > 5 * 1024 * 1024 Then
    Throw New InvalidOperationException("File exceeds 5MB free tier limit.")
End If
$vbLabelText   $csharpLabel

解決方案: 完全刪除此檢查。 IronOCR的OcrInput.LoadImage()除了可用系統記憶體外,沒有大小限制。 基於OCR.space的免費層出於伺服器容量原因而強加的人為5 MB閾值只存在於這裡。 50 MB的掃描PDF與500 KB的掃描PDF載入方式相同。

問題4:JSON回應導航的脆弱性

OCR.space:回應解析依賴於JsonDocument的字元鍵屬性存取導航。 如果回應形狀改變,程式碼如IndexOutOfRangeException。 這兩個都需要整個回程的try-catch保護或空檢查。

解決方案: IronOCR返回型別化的OcrResult物件。 string — 永遠不為空,永遠不存在。 如果OCR沒有產生輸出(空白頁,無法讀取的圖像),result.Text是一個空字串。 沒有要導航的JSON,沒有需要防護的屬性路徑脆弱性。針對信任基於的過濾,double,您可以直接比較:

// IronOCR: typed result — no JSON path fragility
var result = new IronTesseract().Read("document.jpg");

if (result.Confidence < 50)
    Console.WriteLine("Low confidence — consider preprocessing");
else
    Console.WriteLine(result.Text);
// IronOCR: typed result — no JSON path fragility
var result = new IronTesseract().Read("document.jpg");

if (result.Confidence < 50)
    Console.WriteLine("Low confidence — consider preprocessing");
else
    Console.WriteLine(result.Text);
Imports IronOcr

Dim result = New IronTesseract().Read("document.jpg")

If result.Confidence < 50 Then
    Console.WriteLine("Low confidence — consider preprocessing")
Else
    Console.WriteLine(result.Text)
End If
$vbLabelText   $csharpLabel

信任分數指南涵蓋了每個單詞和每個文件的信任閾值。

問題5:CI/CD中的共享IP速率限制耗盡

OCR.space:在OCR.space上運行整合測試的CI/CD管道使用與開發辦公網路相同的外向IP地址。 免費層帳戶共享每IP每天500個請求的限制。每次運行處理200個測試文件的管道可能在第一個開發人員進行手動測試之前就耗盡日配額。團隊需透過在測試中模擬OCR.space回應來繞過此問題,這違背了整合測試的目的。

解決方案: IronOCR在本地處理。 測試套件直接調用new IronTesseract().Read(testImagePath).Text — 無需模擬,無配額可耗盡,無網路依賴。 整合測試在CI/CD中以與生產相同的實際OCR結果運行,而無需進行任何速率限制管理或測試隔離模式。

問題6:IDisposable 從HttpClient管理模式

OCR.space:IDisposable釋放HTTP連接池。 每個OCR服務的消費者必須注入單例,使用using塊,或註冊至DI容器的釋放生命周期。 忘記釋放會在負載下導致socket耗盡。

解決方案: IronTesseract不管理網路連接。 不實現IDisposable。 每執行緒建立一個實例(或在ASP.NET中的每個請求中),調用.Read(),並讓GC收集它。 主要的using塊中。 從您的OCR服務包裝中移除IDisposable實作,將DI註冊從有釋放的範圍/轉態簡化為簡單的工廠或單例。

OCR.space移植清單

遷移前的任務

審核程式碼庫以識別所有OCR.space整合點:

# Find all OCR.space HTTP calls
grep -rn "ocr.space" --include="*.cs" .

# Find all base64 encoding related to OCR
grep -rn "base64Image\|Convert.ToBase64String" --include="*.cs" .

# Find rate limiter and retry logic
grep -rn "SemaphoreSlim\|TooManyRequests\|exponential" --include="*.cs" .

# Find JSON parsing for OCR responses
grep -rn "ParsedResults\|IsErroredOnProcessing\|FileParseExitCode" --include="*.cs" .

# Find custom OCR models and exception types
grep -rn "OcrSpaceException\|OcrResult\b" --include="*.cs" .

# Find API key configuration
grep -rn "apikey\|OcrSpaceApiKey\|ocr_space" --include="*.cs" --include="*.json" .
# Find all OCR.space HTTP calls
grep -rn "ocr.space" --include="*.cs" .

# Find all base64 encoding related to OCR
grep -rn "base64Image\|Convert.ToBase64String" --include="*.cs" .

# Find rate limiter and retry logic
grep -rn "SemaphoreSlim\|TooManyRequests\|exponential" --include="*.cs" .

# Find JSON parsing for OCR responses
grep -rn "ParsedResults\|IsErroredOnProcessing\|FileParseExitCode" --include="*.cs" .

# Find custom OCR models and exception types
grep -rn "OcrSpaceException\|OcrResult\b" --include="*.cs" .

# Find API key configuration
grep -rn "apikey\|OcrSpaceApiKey\|ocr_space" --include="*.cs" --include="*.json" .
SHELL

記錄包含OCR.space程式碼的文件列表。 說明哪些方法是async僅因OCR.space的HTTP依賴性 — 這些方法在遷移後可以同步。

程式碼更新任務

  1. 安裝dotnet add package IronOcr
  2. 新增IronOcr.License.LicenseKey = "..."到應用 啟動
  3. 刪除OcrSpaceApiClient類別和所有支持基礎設施
  4. 刪除自訂的IronOcr.OcrResult取代)
  5. 刪除自訂OcrSpaceException類(由標準.NET例外替代)
  6. 刪除SemaphoreSlim速率限制器和相關的Task.Delay邏輯
  7. 刪除所有用於OCR圖像編碼的Convert.ToBase64String()呼叫
  8. 取代FormUrlEncodedContent / OcrInput
  9. _httpClient.PostAsync(...)呼叫
  10. ParsedResults[0].ParsedText
  11. TextOverlayJSON座標解析
  12. 用適當的預處理過濾器替換OCREngine參數切換
  13. OcrLanguage枚舉值
  14. 移除文件大小預驗證檢查(5 MB限制不再適用)
  15. string,其中HTTP是唯一的異步原因
  16. 從配置文件和環境變數設置中刪除OCR.space API金鑰

遷移後測試

  • 驗證文字提取在相同測試文件上的準確性是否相同或更高
  • 確認大文件(超過5 MB)處理無錯誤
  • OcrLanguage.English + OcrLanguage.French測試多語言文件,並驗證交錯輸出
  • 在CI/CD管道中運行真實的OCR呼叫 — 確認在任何文件量下都沒有速率限制錯誤
  • 驗證可搜尋的PDF輸出無水印
  • 檢查轉換為同步後,先前的非同步控制器操作仍然正確響應
  • 測試在氣隙或網路受限部署環境中處理的文件無錯誤
  • 確認OCREngine=2的文件上是可接受的
  • 驗證result.Pages[n].Words座標與結構化文件中的期望欄位位置相匹配
  • 確保應用程式啟動的許可初始化在第一次OCR呼叫前成功

遷移至IronOCR的主要好處

80多行基礎設施稅消失了。每個OCR.space整合都附帶HTTP客戶端、取率限制器、JSON解串器、自訂例外型別和自訂結果模型。 這些程式碼都不是應用實際需要的 — 它存在是為了彌補缺乏SDK的OCR.space。 遷移後,這些程式碼會被刪除。 程式碼庫中的OCR表面縮小到呼叫站的new IronTesseract().Read(path).Text和啟動時的一行許可初始化。

文件處理速度變成了本地硬體的函式。OCR.space將網路延遲、OCR.space伺服器佇列深度和地理往返時間引入每次處理操作中。 IronOCR在內部執行。 一台本地工作站處理文件的速度比任何雲服務的API在任意吞吐量等級下都快,而且沒有批次處理序列化的每分鐘60個請求上限。 使用IronTesseract實例可隨CPU核心進行擴展 — 請參閱多執行緒範例

敏感文件永久保留在您的基礎設施中。遷移後,醫療記錄、財務文件、法律合同和身份文件永遠不會離開應用伺服器。 對HIPAA、GDPR、SOC 2和內部資料分類政策的合規性審查不再需要將OCR.space的資料處理實踐納入範圍。 審核範圍縮小到您自己的基礎設施。 Docker部署指南Azure部署指南涵蓋了在需要資料駐留合規的容器化和雲環境中部署IronOCR。

結構化輸出使文件智能應用成為可能。OCR.space的ParsedText字串是文件分析的終點。 IronOCR的result.Lines帶座標和逐單詞信任分數,使應用程式能夠定位特定欄位、驗證提取品質、提取表資料,並建構下游文件智能管道。 需要在OCR.space的純文字輸出之上構建自訂版面分析的功能轉變為直接的API呼叫。 表格提取指南掃描文件處理指南展示了這一結構化基礎使得可能的工作。

成本在任何量下都變得固定且可預測。OCR.space的免費層涵蓋每月25,000次請求。 超過此範圍,訂閱費用隨著使用量增加。 IronOCR的$999 Lite永久許可無任何文件收費,不論任何量。 一個每月處理100,000份文件的團隊支付同樣的許可費用作為每月處理1,000份文件的團隊。 文件處理應用的預算預測變為固定的年度成本,而不是隨著業務成功增長的可變行項目。 IronOCR產品頁面包含免費試用,讓團隊在購買前驗證其具體文件型別的準確性。

請注意OCR.space和Tesseract是其各自所有者的註冊商標。 本網站不與谷歌或OCR.space關聯,不受其認可或協贊。 所有產品名稱、標誌和品牌均為其各自所有者的財產。 比較僅供資訊用途,並反映撰寫時獲得的公開資訊。

常見問題

為什麼我應該從OCR.space API遷移到IronOCR?

常見的驅動因素包括消除COM互操作複雜性、更換基於文件的許可證管理、避免按頁收費、啟用Docker/容器部署,以及採取與標準.NET工具整合的NuGet本地工作流。

從OCR.space API遷移到IronOCR的主要程式碼更改是什麼?

用IronTesseract實例化來替換OCR.space的初始化序列,移除COM生命週期管理(明確的Create/Load/Close模式),並更新結果屬性名稱。結果是顯著減少樣板程式碼行數。

如何安裝IronOCR以開始遷移?

在套件管理器控制台中運行 'Install-Package IronOcr' 或在CLI中運行 'dotnet add package IronOcr'。語言包是單獨的包:例如 'dotnet add package IronOcr.Languages.French' 安裝法語支持。

對於標準業務文件,IronOCR能否與OCR.space API的OCR準確度匹配?

IronOCR在包括發票、合同、收據和鍵入的表單等標準業務內容上達到高準確性。圖像預處理過濾器(糾偏、噪音去除、對比度增強)進一步提高了變質輸入的識別。

IronOCR如何處理OCR.space API單獨安裝的語言資料?

IronOCR中的語言資料分發為NuGet包。'dotnet add package IronOcr.Languages.German' 安裝德語支持。不涉及手動文件放置或目錄路徑。

從OCR.space API遷移到IronOCR需要對部署基礎設施進行更改嗎?

IronOCR需要比OCR.space API更少的基礎設施更改。沒有SDK二進制路徑、授權檔案位置或授權伺服器配置。NuGet套件包含完整的OCR引擎,授權密鑰是一個在應用程式程式碼中設定的字串。

遷移後如何配置IronOCR許可證?

在應用啟動程式碼中分配IronOcr.License.LicenseKey = "YOUR-KEY"。在Docker或Kubernetes中,將金鑰儲存為環境變數並在啟動時讀取。在接受流量之前,使用License.IsValidLicense驗證。

IronOCR可以像OCR.space一樣處理PDF嗎?

是的。IronOCR能讀取本地和掃描的PDF。實例化IronTesseract,調用ocr.Read(input),其中input是PDF路徑或OcrPdfInput,並迭代OcrResult頁面。不需要單獨的PDF渲染管道。

IronOCR如何處理大批量處理中的多執行緒?

IronTesseract可以安全地在每個執行緒中實例化。在Parallel.ForEach或Task池中為每個執行緒建立一個實例,並發併運行OCR,當完成時處置每個實例。不需要全域狀態或鎖定。

IronOCR在文字提取後支持哪些輸出格式?

IronOCR返回結構化結果,包括文字、單詞坐標、信心分數和頁面結構。導出選項包括純文字、可搜索PDF以及下游處理的結構化結果物件。

IronOCR的定價在擴展工作負載時是否比OCR.space API更可預測?

IronOCR使用固定費率的永久性許可證,沒有按頁或按量收費。無論您處理10000頁還是1000萬頁,許可證費用保持不變。團隊和批量許可證選項可在IronOCR定價頁面上找到。

從OCR.space API遷移到IronOCR後,我現有的測試會怎樣?

遷移後,斷言提取的文字內容的測試應繼續通過。需要更新的測試是驗證API調用模式或COM物件生命週期,這需要反映IronOCR較簡單的初始化和結果模型。

Kannaopat Udonpant
軟體工程師
在成為軟體工程師之前,Kannapat在日本北海道大學完成了環境資源博士學位。在攻讀學位期間,Kannapat還成為車輛機器人實驗室的一員,該實驗室隸屬於生產工程系。在2022年,他憑藉C#技能加入了Iron Software的工程團隊,專注於IronPDF。Kannapat珍視他的工作,因為他能直接向撰寫大部分IronPDF程式碼的開發者學習。除了同儕學習,Kannapat還喜歡在Iron Software工作的社交方面。不寫程式碼或文件時,Kannapat通常在他的PS5上玩遊戲或重看The Last of Us。

Iron 支援團隊

我們線上24小時,每週5天。
聊天
電子郵件
給我打電話