使用 IronOCR 處理驗證碼

Curtis Chau
Curtis Chau
已更新: 2026年8月2日

IronOCR能讀取驗證碼嗎?

這有可能,但不能保證。

大多數 CAPTCHA 產生器都經過精心設計,旨在欺騙 OCR 軟體,有些甚至使用「無法被 OCR 軟體(如 Tesseract)讀取」作為單元測試。

根據定義,驗證碼對於OCR引擎來說非常難以讀取。 解析度非常低,每個字元都經過精心安排,與其他字元的角度和間距各不相同,並且還加入了變化的背景噪音。

移除背景雜訊後的灰階影像比彩色影像更容易處理,但仍具有挑戰性:

以下是 C# 程式碼範例,嘗試移除雜訊並將 CAPTCHA 影像轉換為灰階影像,以提高 OCR 結果:

using IronOcr;

class CaptchaReader
{
    static void Main(string[] args)
    {
        // Initialize the IronOCR engine
        var Ocr = new IronTesseract();

        // Create an OCR input object and load the CAPTCHA image
        var Input = new OcrInput();
        Input.LoadImage("captcha-image.jpg");

        // Apply noise reduction to improve OCR accuracy
        // This removes background noise while preserving text
        Input.DeNoise();

        // Optionally apply despeckle for more aggressive noise removal
        Input.Despeckle();

        // Convert the image to grayscale 
        // OCR works better on grayscale images compared to colored ones
        Input.ToGrayScale();

        // Perform OCR to extract text from the image
        var Result = Ocr.Read(Input);

        // Output the recognized text to the console
        Console.WriteLine(Result.Text);
    }
}

說明:

  • IronOcr: 這個程式庫用於從影像中讀取文字。
  • OcrInput: 這個類別代表了OCR處理的影像輸入。
  • DeNoise: 此方法用於減少影像中的背景雜訊。
  • Despeckle: 當基本的 DeNoise 不足時,此方法用於更積極的降噪。
  • ToGrayScale: 這將影像轉換為灰階以提高識別準確性。
  • Read: 調用此方法以從預處理好的影像中提取文字。

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

閱讀更多

準備好開始了嗎?

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