IronOCR 操作指南 高级阅读的OCR配置 高级阅读的 OCR 配置 Curtis Chau 已更新:2026年2月26日 下载 IronOCR NuGet 下载 DLL 下载 Windows 安装程序 免费试用 LLM副本 LLM副本 将页面复制为 Markdown 格式,用于 LLMs 在 ChatGPT 中打开 向 ChatGPT 咨询此页面 在双子座打开 向 Gemini 询问此页面 在 Grok 中打开 向 Grok 询问此页面 打开困惑 向 Perplexity 询问有关此页面的信息 分享 在 Facebook 上分享 分享到 X(Twitter) 在 LinkedIn 上分享 复制链接 电子邮件文章 This article was translated from English: Does it need improvement? Translated View the article in English IronOCR提供超越标准 OCR 的高级扫描读取方法,例如 ReadLicensePlate 和 ReadPhoto。 这些方法由IronOCR包提供支持。 为了微调这些方法处理文本的方式, IronOCR公开了 TesseractConfiguration 类,使开发人员能够完全控制字符白名单、黑名单、条形码检测、数据表读取等。 本文涵盖了 TesseractConfiguration 属性,可用于高级阅读,并提供在实际场景中配置 OCR 的实用示例。 快速入门:将 OCR 输出限制为字符白名单 在调用 Read 之前,在 TesseractConfiguration 上设置 WhiteListCharacters。 任何不在白名单中的字符都会被静默地从结果中删除,从而消除噪声而无需任何后期处理。 使用 NuGet 包管理器安装 https://www.nuget.org/packages/IronOcr PM > Install-Package IronOcr 复制并运行这段代码。 var result = new IronTesseract() { Configuration = new TesseractConfiguration { WhiteListCharacters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789- " } }.Read(new OcrInput("image.png")); Console.WriteLine(result.Text); 部署到您的生产环境中进行测试 通过免费试用立即在您的项目中开始使用IronOCR Free 30 Day Trial 如何配置 OCR 以实现高级阅读 从NuGet安装IronOCR 安装IronOCR包 配置TesseractConfiguration属性,例如WhiteListCharacters和ReadBarCodes 使用OcrInput加载输入图像 使用ReadPhoto 、 ReadLicensePlate或ReadPassport等高级方法读取图像。 Tesseract配置属性 TesseractConfiguration 类提供以下属性,用于自定义 OCR 行为。 这些是通过 IronTesseract.Configuration 设置的。 属性 翻译类型 说明 WhiteListCharacters 字符串 OCR 输出仅识别此字符串中存在的字符,所有其他字符都将被排除。 BlackListCharacters 字符串 该字符串中的字符将被主动忽略并从 OCR 输出中删除。 ReadBarCodes 布尔值 在 OCR 处理过程中启用或禁用文档中的条形码检测。 ReadDataTables 布尔值 使用 Tesseract 启用或禁用文档中的表格结构检测。 PageSegmentationMode TesseractPageSegmentationMode 决定 Tesseract 如何分割输入图像。选项包括AutoOsd 、 Auto 、 SingleBlock 、 SingleLine 、 SingleWord等。 RenderSearchablePdf 布尔值 启用后,OCR 输出可以保存为带有不可见文本层的可搜索 PDF。 RenderHocr 布尔值 启用后,OCR 输出将包含 hOCR 数据,以便进一步处理或导出。 TesseractVariables Dictionary<字符串, object> 提供对 Tesseract 底层配置变量的直接访问,以实现精细化控制。查看完整的 Tesseract 变量列表。 TesseractVariables 字典更进一步,公开了数百个底层 Tesseract 引擎参数,以应对高级属性不足以满足需求的情况。 以下示例演示了每个属性组,首先是字符白名单。 设置车牌字符白名单 WhiteListCharacters 的一个常见用例是将 OCR 输出限制为仅包含车牌上可以出现的字符:大写字母、数字、连字符和空格。 这样可以消除噪声,提高准确性,方法是告诉引擎忽略预期字符集之外的所有内容。 输入 以下车辆登记记录包含大写字母、小写字母和特殊符号(@, $, #, |, *),以及标点符号。 BlackListCharacters 通过主动排除已知的噪声符号(例如 `, 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); $vbLabelText $csharpLabel 输出 白名单过滤的效果在结果中清晰可见: "车牌: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 (#),而保留大写字母和数字。 *"费用:$125.00 + 税"变为"F 12500"** 。 美元符号、小数点、Plus号和小写字母"tax"都被移除。 "Ref: ~record_v2^final"变为"R 2" 。 波浪号 (~)、下划线、插入符号 (^) 和所有小写字符都会被移除。 同样的 WhiteListCharacters 和 BlackListCharacters 方法适用于任何文档类型,而不仅仅是车牌。 下一节将展示如何扩展读取操作,以便在同一次读取过程中检测条形码和表格结构。 条形码和数据表读取配置 IronOCR可以检测文档中的条形码、结构化表格以及文本。 这些功能由 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, }; $vbLabelText $csharpLabel ReadBarCodes :当设置为 true 时, IronOCR除了扫描文本外,还会扫描文档中的条形码。 设置为 false 可跳过条形码检测,并在不需要条形码时加快处理速度。 ReadDataTables :当设置为 true 时,Tesseract 会尝试检测并保留文档中的表结构。 这对于发票、报告和其他表格文档非常有用。 这些选项可以与 WhiteListCharacters 和 BlackListCharacters 结合使用,以精确控制从复杂文档中提取的内容。 过滤和检测控制着提取的内容,而布局解释则是另一个需要考虑的问题。 下一节将介绍如何为文档类型选择正确的 PageSegmentationMode。 控制页面分段模式 PageSegmentationMode 告诉 Tesseract 如何在识别之前分割输入图像。 为给定布局选择错误的模式会导致引擎误读或完全跳过文本。 模式 使用案例 AutoOsd 自动版面分析,具备方向和脚本检测功能 Auto 自动布局分析(不带 OSD)(默认) SingleColumn 假设图像为单列文本。 SingleBlock 假设图像是由单个均匀的文本块组成。 SingleLine 假设图像是一行文字。 SparseText 尽可能多地查找文本,顺序不限。 对于包含单行的标签或横幅,SingleLine 可以消除多块分析,从而提高速度和准确性。 输入 single-line-label.png 是一个窄的货运标签,上面只有一行粗体快递文字:SHIPPING LABEL: TRK-2024-XR9-001。 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); $vbLabelText $csharpLabel 对于文本位置不规则的扫描页面,SparseText 比 Auto 恢复的内容更多。 输入 receipt-scan.png 是 Corner Market 的热敏收据,包含四项商品(咖啡、松饼、果汁、格兰诺拉麦片棒)、虚线分隔符、小计、税金和总计。 在这种布局中,固定块分割会遗漏不同水平位置的条目。 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); $vbLabelText $csharpLabel 布局分割已根据文档类型进行了调整,下一步是控制下游处理的输出格式。 生成可搜索的 PDF 和 hOCR 输出 RenderSearchablePdf 和 RenderHocr 控制IronOCR除了纯文本结果外生成的输出格式。 RenderSearchablePdf在原始图像上嵌入一个不可见的文本层,生成一个 PDF 文件,用户可以在其中搜索和复制文本,而扫描的图像仍然可见。 这是文档归档工作流程的标准输出格式。 输入 scanned-document.pdf 是IronOCR Solutions Ltd. 出具的单页商业信函(日期为 2024 年 3 月 15 日,参考 DOC-2024-OCR-0315)。 结果保存为 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"); $vbLabelText $csharpLabel 输出 输出结果是一个 PDF 文件,它看起来与输入文件完全相同,但其中包含一个隐藏的文本层。 打开 searchable-output.pdf,并使用 Ctrl+F 验证嵌入的文本是否可搜索和可复制。 RenderHocr生成 hOCR 文档,这是一个 HTML 文件,它将文本内容与每个单词的边界框坐标一起编码。 当下游工具需要精确的词语定位时,例如编辑引擎或文档布局分析,这非常有用。 输入 document-page.png 是一个文档页面,标题为"2024 年第一季度摘要",包含两段财务数据,涵盖收入、运营成本和增长驱动因素。 结果保存为 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"); 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"); $vbLabelText $csharpLabel 输出 output.html 将每个识别出的单词与其边界框坐标进行编码。 在浏览器中打开文件以检查 hOCR 结构,或将其传递给下游工具进行布局分析或编辑。 如果您需要从单个读取调用中获得所有三种输出格式(纯文本、可搜索 PDF 和 hOCR),则可以同时启用这两个标志。 这些输出标志与读取的语言无关,包括非拉丁字母。下一节将介绍如何对日语文本应用字符过滤。 国际文档的Unicode字符过滤 对于中文、日文或韩文的国际文档,WhiteListCharacters 和 BlackListCharacters 属性适用于 Unicode 字符。 这样可以将输出限制为特定脚本,例如日语仅输出平假名和片假名。 请注意 请确保已安装相应的语言包(例如IronOCR ),然后再继续。 输入 该文档包含标题 (テsuto)、混合平假名和片假名以及浊音变体 (プ、で) 的日语句子、包含列入黑名单的噪音符号 (★、■) 和汉字 (価格) 的价格线、包含另一个列入黑名单的符号 (§) 的备注行、更多汉字 (购入)、其他浊音标记变体 (プ、デ) 和基本片假名(メモ,ール)。 白名单仅允许平假名、片假名、数字和常用日语标点符号通过; 这三个噪声符号已被明确列入黑名单。 平假名和片假名的 Unicode 字符范围以字符串字面量的形式在 WhiteListCharacters 中传递,噪声符号列在 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"); $vbLabelText $csharpLabel 输出 完整的过滤输出以文本文件的形式提供: jp-output.txt 。 因为白名单只包含基本的平假名和片假名字符,所以像プ(pu)和デ(de)这样的派生声标变体被删除了。 像価格(价格)和購入(购买)这样的汉字也被排除在外,因为它们不在白名单字符集中。 像 ■ 和 § 这样的黑名单符号会被主动移除,而不管白名单如何。 接下来我该去哪里? 现在您已经了解如何配置IronOCR以适应高级阅读场景,请探索以下内容: 读取特定类型的文件,例如护照和车牌 用于精细化引擎调优的Tesseract 配置变量完整列表 -条形码和二维码读取作为独立的 OCR 用例 从处理结果中导出 hOCR 和可搜索的 PDF 文件 如需用于生产,请务必获取许可证以去除水印并使用全部功能。 Curtis Chau 立即与工程团队聊天 技术作家 Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。除了开发之外,Curtis 对物联网 (IoT) 有浓厚的兴趣,探索将硬件和软件集成的新方法。在空闲时间,他喜欢玩游戏和构建 Discord 机器人,将他对技术的热爱与创造力相结合。 准备开始了吗? Nuget 下载 5,556,263 | 版本: 2026.3 刚刚发布 免费试用 免费 NuGet 下载 总下载量:5,556,263 查看许可证 还在滚动吗? 想快速获得证据? PM > Install-Package IronOcr 运行示例 观看您的图像变成可搜索文本。 免费 NuGet 下载 总下载量:5,556,263 查看许可证