高级阅读的 OCR 配置

This article was translated from English: Does it need improvement?
Translated
View the article in English

IronOCR provides advanced scan reading methods such as ReadPassport, ReadLicensePlate, and ReadPhoto that go beyond standard OCR. 这些方法由IronOCR包提供支持。 To fine-tune how these methods process text, IronOCR exposes the TesseractConfiguration class, giving developers full control over character whitelisting, blacklisting, barcode detection, data table reading, and more.

This article covers the TesseractConfiguration properties available for advanced reading and practical examples for configuring OCR in real-world scenarios.

快速入门:将 OCR 输出限制为字符白名单

Set WhiteListCharacters on TesseractConfiguration before calling Read. 任何不在白名单中的字符都会被静默地从结果中删除,从而消除噪声而无需任何后期处理。

  1. 使用 NuGet 包管理器安装 https://www.nuget.org/packages/IronOcr

    PM > Install-Package IronOcr
  2. 复制并运行这段代码。

    var result = new IronTesseract() { Configuration = new TesseractConfiguration { WhiteListCharacters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789- " } }.Read(new OcrInput("image.png")); Console.WriteLine(result.Text);
  3. 部署到您的生产环境中进行测试

    通过免费试用立即在您的项目中开始使用IronOCR

    arrow pointer


Tesseract配置属性

The TesseractConfiguration class provides the following properties for customizing OCR behavior. These are set through IronTesseract.Configuration.

属性 翻译类型 说明
WhiteListCharacters 字符串 OCR 输出仅识别此字符串中存在的字符,所有其他字符都将被排除。
BlackListCharacters 字符串 该字符串中的字符将被主动忽略并从 OCR 输出中删除。
ReadBarCodes 布尔值 在 OCR 处理过程中启用或禁用文档中的条形码检测。
ReadDataTables 布尔值 使用 Tesseract 启用或禁用文档中的表格结构检测。
PageSegmentationMode TesseractPageSegmentationMode 决定 Tesseract 如何分割输入图像。选项包括AutoOsdAutoSingleBlockSingleLineSingleWord等。
RenderSearchablePdf 布尔值 启用后,OCR 输出可以保存为带有不可见文本层的可搜索 PDF。
RenderHocr 布尔值 启用后,OCR 输出将包含 hOCR 数据,以便进一步处理或导出。
TesseractVariables Dictionary<字符串, object> Provides direct access to low-level Tesseract configuration variables for fine-grained control.

The TesseractVariables dictionary goes further still, exposing hundreds of underlying Tesseract engine parameters for cases where the high-level properties are not sufficient.

以下示例演示了每个属性组,首先是字符白名单。

设置车牌字符白名单

A common use case for WhiteListCharacters is restricting OCR output to only the characters that can appear on a license plate: uppercase letters, digits, hyphens, and spaces. 这样可以消除噪声,提高准确性,方法是告诉引擎忽略预期字符集之外的所有内容。

输入

The following vehicle registration record contains a mix of uppercase text, lowercase text, special symbols (@, $, #, |, ~, ^, *), and punctuation.

用于 OCR 白名单演示的包含混合字符的车辆登记记录

BlackListCharacters supplements the whitelist by actively excluding known noise symbols like `, ~, @, #, $, %, &, and *.

:path=/static-assets/ocr/content-code-examples/how-to/ocr-configurations-for-advanced-reading.cs
using IronOcr;

// Initialize the Tesseract OCR engine
IronTesseract ocr = new IronTesseract();

ocr.Configuration = new TesseractConfiguration
{
    // Whitelist only characters that appear on license plates
    WhiteListCharacters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789- ",

    // Blacklist common noise characters
    BlackListCharacters = "`~@#$%&*",
};

var ocrInput = new OcrInput();
// Load the input image
ocrInput.LoadImage("advanced-input.png");
// Perform OCR on the input image with ReadPhoto method
var results = ocr.ReadPhoto(ocrInput);

// Print the filtered text result to the console
Console.WriteLine(results.Text);
Imports IronOcr

' Initialize the Tesseract OCR engine
Dim ocr As New IronTesseract()

ocr.Configuration = New TesseractConfiguration With {
    ' Whitelist only characters that appear on license plates
    .WhiteListCharacters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789- ",
    
    ' Blacklist common noise characters
    .BlackListCharacters = "`~@#$%&*"
}

Dim ocrInput As New OcrInput()
' Load the input image
ocrInput.LoadImage("advanced-input.png")
' Perform OCR on the input image with ReadPhoto method
Dim results = ocr.ReadPhoto(ocrInput)

' Print the filtered text result to the console
Console.WriteLine(results.Text)
$vbLabelText   $csharpLabel

输出

OCR 输出仅显示白名单中的车牌字符

白名单过滤的效果在结果中清晰可见:

  • "车牌:ABC-1234"变为"P ABC-1234" 。 小写字母"late:"被省略,而车牌号码则完全保留。
  • "VIN: 1HGBH41JXMN109186"变为"VIN 1HGBH41JXMN109186" 。 冒号被省略,但大写的车辆识别码和完整号码被保留。
  • "Owner: john.doe@email.com"变为"O" 。 所有小写​​电子邮件地址和标点符号均被删除。 -地区:CA-90210 | "5区"变为"R CA-90210 Z 5" 。 管道(|) and hash (#) are removed, while the uppercase letters and numbers survive.
  • *"费用:$125.00 + 税"变为"F 12500"** 。 美元符号、小数点、Plus号和小写字母"tax"都被移除。
  • "Ref: ~record_v2^final"变为"R 2" 。 The tilde (~), underscore, caret (^), and all lowercase characters are stripped.

The same WhiteListCharacters and BlackListCharacters approach works for any document type, not just license plates. 下一节将展示如何扩展读取操作,以便在同一次读取过程中检测条形码和表格结构。

条形码和数据表读取配置

IronOCR可以检测文档中的条形码、结构化表格以及文本。 These features are controlled through TesseractConfiguration:

IronTesseract ocr = new IronTesseract();

ocr.Configuration = new TesseractConfiguration
{
    // Enable barcode detection within documents
    ReadBarCodes = true,

    // Enable table structure detection
    ReadDataTables = true,
};
IronTesseract ocr = new IronTesseract();

ocr.Configuration = new TesseractConfiguration
{
    // Enable barcode detection within documents
    ReadBarCodes = true,

    // Enable table structure detection
    ReadDataTables = true,
};
Dim ocr As New IronTesseract()

ocr.Configuration = New TesseractConfiguration With {
    .ReadBarCodes = True,
    .ReadDataTables = True
}
$vbLabelText   $csharpLabel
  • ReadBarCodes: When set to true, IronOCR scans the document for barcodes in addition to text. Set to false to skip barcode detection and speed up processing when barcodes are not expected.
  • ReadDataTables: When set to true, Tesseract attempts to detect and preserve table structures in the document. 这对于发票、报告和其他表格文档非常有用。

These options can be combined with WhiteListCharacters and BlackListCharacters for precise control over what is extracted from complex documents.

过滤和检测控制着提取的内容,而布局解释则是另一个需要考虑的问题。 The next section covers how to select the right PageSegmentationMode for the document type.

控制页面分段模式

PageSegmentationMode tells Tesseract how to segment the input image before recognition. 为给定布局选择错误的模式会导致引擎误读或完全跳过文本。

模式 使用案例
AutoOsd 自动版面分析,具备方向和脚本检测功能
Auto 自动布局分析(不带 OSD)(默认)
SingleColumn 假设图像为单列文本。
SingleBlock 假设图像是由单个均匀的文本块组成。
SingleLine 假设图像是一行文字。
SparseText 尽可能多地查找文本,顺序不限。

For a label or banner that contains a single line, SingleLine eliminates multi-block analysis and improves both speed and accuracy.

输入

single-line-label.png is a narrow shipping label with exactly one line of bold Courier text: SHIPPING LABEL: TRK-2024-XR9-001.

用于 OCR 单行分割模式的单行货运标签
IronTesseract ocr = new IronTesseract();

ocr.Configuration = new TesseractConfiguration
{
    PageSegmentationMode = TesseractPageSegmentationMode.SingleLine,
};

using OcrInput input = new OcrInput();
input.LoadImage("single-line-label.png");

OcrResult result = ocr.Read(input);
Console.WriteLine(result.Text);
IronTesseract ocr = new IronTesseract();

ocr.Configuration = new TesseractConfiguration
{
    PageSegmentationMode = TesseractPageSegmentationMode.SingleLine,
};

using OcrInput input = new OcrInput();
input.LoadImage("single-line-label.png");

OcrResult result = ocr.Read(input);
Console.WriteLine(result.Text);
Imports IronOcr

Dim ocr As New IronTesseract()

ocr.Configuration = New TesseractConfiguration With {
    .PageSegmentationMode = TesseractPageSegmentationMode.SingleLine
}

Using input As New OcrInput()
    input.LoadImage("single-line-label.png")

    Dim result As OcrResult = ocr.Read(input)
    Console.WriteLine(result.Text)
End Using
$vbLabelText   $csharpLabel

For a scanned page with irregular text placement, SparseText recovers more content than Auto.

输入

receipt-scan.png is a Corner Market thermal receipt with four line items (coffee, muffin, juice, granola bar), a dashed separator, subtotal, tax, and total. 在这种布局中,固定块分割会遗漏不同水平位置的条目。

用于 OCR 稀疏文本分割模式的热敏收据
IronTesseract ocr = new IronTesseract();

ocr.Configuration = new TesseractConfiguration
{
    PageSegmentationMode = TesseractPageSegmentationMode.SparseText,
};

using OcrInput input = new OcrInput();
input.LoadImage("receipt-scan.png");

OcrResult result = ocr.Read(input);
Console.WriteLine(result.Text);
IronTesseract ocr = new IronTesseract();

ocr.Configuration = new TesseractConfiguration
{
    PageSegmentationMode = TesseractPageSegmentationMode.SparseText,
};

using OcrInput input = new OcrInput();
input.LoadImage("receipt-scan.png");

OcrResult result = ocr.Read(input);
Console.WriteLine(result.Text);
Imports IronTesseract

Dim ocr As New IronTesseract()

ocr.Configuration = New TesseractConfiguration With {
    .PageSegmentationMode = TesseractPageSegmentationMode.SparseText
}

Using input As New OcrInput()
    input.LoadImage("receipt-scan.png")

    Dim result As OcrResult = ocr.Read(input)
    Console.WriteLine(result.Text)
End Using
$vbLabelText   $csharpLabel

布局分割已根据文档类型进行了调整,下一步是控制下游处理的输出格式。

生成可搜索的 PDF 和 hOCR 输出

RenderSearchablePdf and RenderHocr control the output formats that IronOCR produces alongside the plain text result.

RenderSearchablePdf embeds an invisible text layer over the original image, producing a PDF where users can search and copy text while the scanned image remains visible. 这是文档归档工作流程的标准输出格式。

输入

scanned-document.pdf is a single-page business letter from IronOCR Solutions Ltd. (dated 15 March 2024, reference DOC-2024-OCR-0315). The result is saved as searchable-output.pdf.

IronTesseract ocr = new IronTesseract();

ocr.Configuration = new TesseractConfiguration
{
    RenderSearchablePdf = true,
};

using OcrInput input = new OcrInput();
input.LoadPdf("scanned-document.pdf");

OcrResult result = ocr.Read(input);
result.SaveAsSearchablePdf("searchable-output.pdf");
IronTesseract ocr = new IronTesseract();

ocr.Configuration = new TesseractConfiguration
{
    RenderSearchablePdf = true,
};

using OcrInput input = new OcrInput();
input.LoadPdf("scanned-document.pdf");

OcrResult result = ocr.Read(input);
result.SaveAsSearchablePdf("searchable-output.pdf");
Imports IronOcr

Dim ocr As New IronTesseract()

ocr.Configuration = New TesseractConfiguration With {
    .RenderSearchablePdf = True
}

Using input As New OcrInput()
    input.LoadPdf("scanned-document.pdf")

    Dim result As OcrResult = ocr.Read(input)
    result.SaveAsSearchablePdf("searchable-output.pdf")
End Using
$vbLabelText   $csharpLabel

输出

输出结果是一个 PDF 文件,它看起来与输入文件完全相同,但其中包含一个隐藏的文本层。 Open searchable-output.pdf and use Ctrl+F to verify that the embedded text is searchable and copyable.

RenderHocr produces an hOCR document, an HTML file that encodes the text content together with bounding box coordinates for every word. 当下游工具需要精确的词语定位时,例如编辑引擎或文档布局分析,这非常有用。

输入

document-page.png is a document page with the heading "Quarterly Summary Q1 2024" and two paragraphs of financial data covering revenue, operating costs, and growth drivers. The result is saved as output.html.

用于 hOCR 边界框输出的文档页面输入
IronTesseract ocr = new IronTesseract();

ocr.Configuration = new TesseractConfiguration
{
    RenderHocr = true,
};

using OcrInput input = new OcrInput();
input.LoadImage("document-page.png");

OcrResult result = ocr.Read(input);
result.SaveAsHocrFile("output.html");
IronTesseract ocr = new IronTesseract();

ocr.Configuration = new TesseractConfiguration
{
    RenderHocr = true,
};

using OcrInput input = new OcrInput();
input.LoadImage("document-page.png");

OcrResult result = ocr.Read(input);
result.SaveAsHocrFile("output.html");
Imports IronOcr

Dim ocr As New IronTesseract()

ocr.Configuration = New TesseractConfiguration With {
    .RenderHocr = True
}

Using input As New OcrInput()
    input.LoadImage("document-page.png")

    Dim result As OcrResult = ocr.Read(input)
    result.SaveAsHocrFile("output.html")
End Using
$vbLabelText   $csharpLabel

输出

output.html encodes each recognized word with its bounding box coordinates. 在浏览器中打开文件以检查 hOCR 结构,或将其传递给下游工具进行布局分析或编辑。

如果您需要从单个读取调用中获得所有三种输出格式(纯文本、可搜索 PDF 和 hOCR),则可以同时启用这两个标志。

这些输出标志与读取的语言无关,包括非拉丁字母。下一节将介绍如何对日语文本应用字符过滤。

国际文档的Unicode字符过滤

For international documents in Chinese, Japanese, or Korean, the WhiteListCharacters and BlackListCharacters properties work with Unicode characters. 这样可以将输出限制为特定脚本,例如日语仅输出平假名和片假名。

请注意 请确保已安装相应的语言包(例如IronOCR ),然后再继续。

输入

该文档包含标题 (テsuto)、混合平假名和片假名以及浊音变体 (プ、で) 的日语句子、包含列入黑名单的噪音符号 (★、■) 和汉字 (価格) 的价格线、包含另一个列入黑名单的符号 (§) 的备注行、更多汉字 (购入)、其他浊音标记变体 (プ、デ) 和基本片假名(メモ,ール)。 白名单仅允许平假名、片假名、数字和常用日语标点符号通过; 这三个噪声符号已被明确列入黑名单。

OCR高级配置日语输入

The Unicode character ranges for Hiragana and Katakana are passed as 字符串 literals in WhiteListCharacters, with the noise symbols listed in BlackListCharacters.

警告 控制台可能不支持显示 Unicode 字符。 将输出重定向到 .txt 文件是处理此类字符时验证结果的可靠方法。

:path=/static-assets/ocr/content-code-examples/how-to/ocr-configurations-for-advanced-reading-jp.cs
using IronOcr;
using System.IO;

IronTesseract ocr = new IronTesseract();

ocr.Configuration = new TesseractConfiguration
{
    // Whitelist only Hiragana, Katakana, numbers, and common Japanese punctuation
    WhiteListCharacters = "あいうえおかきくけこさしすせそたちつてとなにぬねのはひふへほまみむめもやゆよらりるれろわをん" +
                            "アイウエオカキクケコサシスセソタチツテトナニヌネノハヒフヘホマミムメモヤユヨラリルレロワヲン" +
                            "0123456789、。?!()¥ー",

    // Blacklist common noise/symbols you want to ignore
    BlackListCharacters = "★■§",
};

var ocrInput = new OcrInput();

// Load Japanese input image
ocrInput.LoadImage("jp.png");

// Perform OCR on the input image with ReadPhoto method
var results = ocr.ReadPhoto(ocrInput);

// Write the text result directly to a file named "output.txt"
File.WriteAllText("output.txt", results.Text);

// You can add this line to confirm the file was saved:
Console.WriteLine("OCR results saved to output.txt");
Imports IronOcr
Imports System.IO

Dim ocr As New IronTesseract()

ocr.Configuration = New TesseractConfiguration With {
    .WhiteListCharacters = "あいうえおかきくけこさしすせそたちつてとなにぬねのはひふへほまみむめもやゆよらりるれろわをん" &
                           "アイウエオカキクケコサシスセソタチツテトナニヌネノハヒフヘホマミムメモヤユヨラリルレロワヲン" &
                           "0123456789、。?!()¥ー",
    .BlackListCharacters = "★■§"
}

Dim ocrInput As New OcrInput()

' Load Japanese input image
ocrInput.LoadImage("jp.png")

' Perform OCR on the input image with ReadPhoto method
Dim results = ocr.ReadPhoto(ocrInput)

' Write the text result directly to a file named "output.txt"
File.WriteAllText("output.txt", results.Text)

' You can add this line to confirm the file was saved:
Console.WriteLine("OCR results saved to output.txt")
$vbLabelText   $csharpLabel

输出

OCR高级配置日语输出

完整的过滤输出以文本文件的形式提供: jp-output.txt

因为白名单只包含基本的平假名和片假名字符,所以像プ(pu)和デ(de)这样的派生声标变体被删除了。 像価格(价格)和購入(购买)这样的汉字也被排除在外,因为它们不在白名单字符集中。 Blacklisted symbols like , , and § are actively removed regardless of the whitelist.

接下来我该去哪里?

现在您已经了解如何配置IronOCR以适应高级阅读场景,请探索以下内容:

如需用于生产,请务必获取许可证以去除水印并使用全部功能。

常见问题解答

IronOCR 中的 TesseractConfiguration 是什么?

IronOCR 中的 TesseractConfiguration 允许用户自定义 OCR 设置,从而实现字符白名单、BarCode 识别和多语言支持等高级识别功能。

如何在 IronOCR 中设置字符白名单?

在 IronOCR 中,您可以通过 TesseractConfiguration 设置字符白名单,从而指定 OCR 引擎应识别的字符,这对于读取车牌号等任务非常有用。

IronOCR 能读取 BarCode 和数据表吗?

是的,IronOCR 可以通过调整 TesseractConfiguration 属性中的特定设置,配置为读取 BarCode 和数据表,从而实现精确的 OCR 数据提取。

IronOCR 是否支持中文、日语和韩语等国际语言?

IronOCR 通过其多语言 TesseractConfiguration 选项支持多种国际语言,包括中文、日语和韩语。

在 IronOCR 中使用高级 OCR 配置有哪些优势?

利用 IronOCR 中的高级 OCR 配置,可实现更准确、更高效的文本识别,并支持特定语言的文本识别和结构化数据提取等专业任务。

能否针对特定的 OCR 任务对 IronOCR 进行优化?

是的,IronOCR 可以通过配置字符白名单、启用 BARCODE 或表格识别等设置,针对特定的 OCR 任务进行优化,从而提升针对特定应用的性能。

如何在 IronOCR 中启用多语言支持?

要在 IronOCR 中启用多语言支持,您可以在 TesseractConfiguration 中调整语言设置,从而使 OCR 引擎能够识别多种语言的文本。

什么是字符白名单,它们在 IronOCR 中如何使用?

IronOCR 中的字符白名单是 OCR 引擎配置为识别的特定字符列表,非常适合读取数字或特定文本模式等针对性任务。

IronOCR 能否用于读取结构化数据格式?

是的,IronOCR 可以配置为读取和处理 BARCODE 和表格等结构化数据格式,为各种数据提取需求提供多功能的 OCR 能力。

IronOCR 提供了哪些用于高级文本识别的配置选项?

IronOCR 提供字符白名单、多语言支持和 BarCode 识别等配置选项,以增强高级文本识别能力,满足特定需求。

Curtis Chau
技术作家

Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。

除了开发之外,Curtis 对物联网 (IoT) 有浓厚的兴趣,探索将硬件和软件集成的新方法。在空闲时间,他喜欢玩游戏和构建 Discord 机器人,将他对技术的热爱与创造力相结合。

准备开始了吗?
Nuget 下载 5,585,834 | 版本: 2026.4 刚刚发布
Still Scrolling Icon

还在滚动吗?

想快速获得证据? PM > Install-Package IronOcr
运行示例 观看您的图像变成可搜索文本。