如何使用 IronOCR 在 C# 中提取读取结果。

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

IronOCR 的 Read 方法会返回一个 OcrResult 对象,其中包含提取的文本以及详细的元数据,包括每个检测到的元素的精确坐标、尺寸、文本方向和层次结构(段落、行、单词、字符)。

OCR 结果包含有关检测到的段落、行、单词和单个字符的全面信息。

对于每个元素,它都提供了文本内容、精确的 X 和 Y 坐标、尺寸(宽和高)、文本方向(从左到右或从上到下)以及在 CropRectangle 对象中的位置。

快速入门:从检测到的第一个单词中检索单词文本

使用 IronTesseractRead 方法 OCR 图像,并使用 Words 集合提取第一个单词的文本。

Nuget Icon立即开始使用 NuGet 创建 PDF 文件:

  1. 使用 NuGet 包管理器安装 IronOCR

    PM > Install-Package IronOcr

  2. 复制并运行这段代码。

    string wordText = new IronTesseract().Read("file.jpg").Words[0].Text;
  3. 部署到您的生产环境中进行测试

    立即开始在您的项目中使用 IronOCR,免费试用!
    arrow pointer

我可以从 OCR 结果中提取哪些数据?

结果值不仅包含提取的文本,还包含 IronOCR 在 PDF 和图像文件中发现的页面、段落、行、单词、字符和条形码等信息。 您可以使用 Read 方法从返回的 OcrResult 对象中访问这些信息。

IronOCR 的综合结果系统建立在强大的 Tesseract 5 引擎之上,为开发人员提供了超越简单文本识别的结构化数据提取功能。 无论是处理 扫描文档照片,还是 屏幕截图OcrResult 类都可让您对提取的数据进行细粒度控制。

:path=/static-assets/ocr/content-code-examples/how-to/read-results-output-information.cs
using IronOcr;
using System;
using static IronOcr.OcrResult;

// Instantiate IronTesseract
IronTesseract ocrTesseract = new IronTesseract();

// Add image
using var imageInput = new OcrImageInput("sample.jpg");
// Perform OCR
OcrResult ocrResult = ocrTesseract.Read(imageInput);

// Retrieve list of detected paragraphs
Paragraph[] paragraphs = ocrResult.Paragraphs;

// Output information to console
Console.WriteLine($"Text: {paragraphs[0].Text}");
Console.WriteLine($"X: {paragraphs[0].X}");
Console.WriteLine($"Y: {paragraphs[0].Y}");
Console.WriteLine($"Width: {paragraphs[0].Width}");
Console.WriteLine($"Height: {paragraphs[0].Height}");
Console.WriteLine($"Text direction: {paragraphs[0].TextDirection}");
Imports IronOcr
Imports System
Imports IronOcr.OcrResult

' Instantiate IronTesseract
Private ocrTesseract As New IronTesseract()

' Add image
Private imageInput = New OcrImageInput("sample.jpg")
' Perform OCR
Private ocrResult As OcrResult = ocrTesseract.Read(imageInput)

' Retrieve list of detected paragraphs
Private paragraphs() As Paragraph = ocrResult.Paragraphs

' Output information to console
Console.WriteLine($"Text: {paragraphs(0).Text}")
Console.WriteLine($"X: {paragraphs(0).X}")
Console.WriteLine($"Y: {paragraphs(0).Y}")
Console.WriteLine($"Width: {paragraphs(0).Width}")
Console.WriteLine($"Height: {paragraphs(0).Height}")
Console.WriteLine($"Text direction: {paragraphs(0).TextDirection}")
$vbLabelText   $csharpLabel
Visual Studio 调试器显示 OCR 提取结果,包含日语商业文档中的坐标和文本

如何从 OCR 结果中访问文本内容?

OcrResult 对象以简单、直观的方式呈现提取的文本,允许开发人员直接使用或将其集成到其他应用程序组件中。 分层结构反映了自然文档的文本组织,使其可以直接处理不同粒度级别的内容。

对于需要多语言支持的应用程序,IronOCR 可以无缝处理多语言文档,在所有125 种支持语言中保持相同的结构化结果格式。

以下代码示例在一个循环中打印文本以验证结果。

:path=/static-assets/ocr/content-code-examples/how-to/read-results-output-text.cs
using IronOcr;
using System;
using static IronOcr.OcrResult;

// Instantiate IronTesseract
IronTesseract ocrTesseract = new IronTesseract();

// Add image
using var imageInput = new OcrImageInput("sampleText.png");
// Perform OCR
OcrResult ocrResult = ocrTesseract.Read(imageInput);

// Retrieve list of detected paragraphs
Paragraph[] paragraphs = ocrResult.Paragraphs;

// Loop through each paragraph in the array
Console.WriteLine("--- All Detected Paragraphs ---");
foreach (Paragraph paragraph in paragraphs)
{
    // Print the text of the current paragraph
    Console.WriteLine(paragraph.Text);
    
    // Add a blank line for better separation (optional)
    Console.WriteLine(); 
}

Imports IronOcr
Imports System
Imports IronOcr.OcrResult

' Instantiate IronTesseract
Dim ocrTesseract As New IronTesseract()

' Add image
Using imageInput As New OcrImageInput("sampleText.png")
    ' Perform OCR
    Dim ocrResult As OcrResult = ocrTesseract.Read(imageInput)

    ' Retrieve list of detected paragraphs
    Dim paragraphs As Paragraph() = ocrResult.Paragraphs

    ' Loop through each paragraph in the array
    Console.WriteLine("--- All Detected Paragraphs ---")
    For Each paragraph As Paragraph In paragraphs
        ' Print the text of the current paragraph
        Console.WriteLine(paragraph.Text)

        ' Add a blank line for better separation (optional)
        Console.WriteLine()
    Next
End Using
$vbLabelText   $csharpLabel

输出

显示 OCR 段落检测结果的终端,其中包含关于孙正义和重田康光的提取文本

控制台输出显示 IronOCR 正在逐行精确提取段落文本。 该引擎可自动检测段落边界,因此非常适合处理包含多个文本块的复杂文档。

如何获取检测到的文本的位置坐标?

除提取的文本外,OcrResult 还提供了详细的位置数据。 这些空间信息对于需要保持布局保真度或从特定文档区域执行目标文本提取的应用程序至关重要。 坐标系使用从页面左上角开始的标准像素测量。

为提高基于坐标的操作的精度,请考虑使用 OCR 区域定位来关注特定区域,或利用 计算机视觉功能来自动识别文本区域。

以下代码演示了迭代每个段落并将其坐标(X 和 Y)打印到控制台。

:path=/static-assets/ocr/content-code-examples/how-to/read-results-output-text.cs
using IronOcr;
using System;
using static IronOcr.OcrResult;

// Instantiate IronTesseract
IronTesseract ocrTesseract = new IronTesseract();

// Add image
using var imageInput = new OcrImageInput("sampleText.png");
// Perform OCR
OcrResult ocrResult = ocrTesseract.Read(imageInput);

// Retrieve list of detected paragraphs
Paragraph[] paragraphs = ocrResult.Paragraphs;

// Loop through each paragraph in the array
Console.WriteLine("--- All Detected Paragraphs ---");
foreach (Paragraph paragraph in paragraphs)
{
    // Print the text of the current paragraph
    Console.WriteLine(paragraph.Text);
    
    // Add a blank line for better separation (optional)
    Console.WriteLine(); 
}

Imports IronOcr
Imports System
Imports IronOcr.OcrResult

' Instantiate IronTesseract
Dim ocrTesseract As New IronTesseract()

' Add image
Using imageInput As New OcrImageInput("sampleText.png")
    ' Perform OCR
    Dim ocrResult As OcrResult = ocrTesseract.Read(imageInput)

    ' Retrieve list of detected paragraphs
    Dim paragraphs As Paragraph() = ocrResult.Paragraphs

    ' Loop through each paragraph in the array
    Console.WriteLine("--- All Detected Paragraphs ---")
    For Each paragraph As Paragraph In paragraphs
        ' Print the text of the current paragraph
        Console.WriteLine(paragraph.Text)

        ' Add a blank line for better separation (optional)
        Console.WriteLine()
    Next
End Using
$vbLabelText   $csharpLabel

输出

终端输出显示 OCR 检测到的段落坐标,X、Y 值分别为:(29,30)、(28,74) 和 (27,362)

输出结果显示了与三个段落相对应的三组坐标。这些坐标可用于绘制边界框、提取特定区域或维护文本元素之间的空间关系。

OCR 结果中还有哪些其他属性?

除了文本和文本坐标,IronOCR 还提供了其他信息。 对于每个文本元素(段落、行、单词和单个字符),可提供以下信息:

  • 文本:实际文本字符串。
  • X:页面左边缘的位置,单位为像素。
  • Y:从页面顶部边缘算起的位置,单位为像素。
  • 宽度:以像素为单位的宽度。
  • 高度:高度,单位为像素。
  • 文本方向:文本的阅读方向(从左到右或从上到下)。
  • 位置:以像素为单位显示文本在页面上位置的矩形。

这些特性在实施时尤其有用:

  • 文本高亮和注释系统
  • 自动表单字段检测
  • 文档转换中的版面保留
  • 用于数据提取的空间文本分析

在调试和可视化方面,请使用 highlight texts 功能 直观验证检测到的区域的准确性。

如何比较段落、行、字和字符?

IronOCR 的分层文本结构允许开发人员根据具体使用情况在适当的细节级别上工作。 了解这些元素之间的差异有助于为您的应用程序选择正确的粒度。

以下是检测到的段落、行、单词和字符的对比。

突出介绍日本科技企业家孙正义和重田康光的简历
文件用红色高亮显示日本科技高管孙正义和重田康光的简介
文本高亮显示功能,在关于技术投资的段落中显示红色方框覆盖所选单词
在 OCR 结果中显示单个字符边界的字符级文本检测

每个粒度级别都有不同的目的:

  • 段落:最适用于文档结构分析和批量文本提取
  • :用于保持阅读顺序和处理表格数据
  • 词语:搜索功能和文本分析的理想选择
  • 字符:拼写检查和精确文本编辑应用程序的完美选择

IronOCR 可以读取条形码和 QR 码吗?

是的,IronOCR 可以读取条形码和 QR 码。 虽然功能可能不如 IronBarcode 强大,但 IronOCR 提供了对常见条形码类型的支持。要启用条形码检测,请将 Configuration.ReadBarCodes 属性设置为 true。 这种集成功能使 IronOCR 成为处理包含文本和 BarCode 的文档(如发票、发货标签或产品目录)的绝佳选择。

此外,还可以从检测到的条形码中提取有价值的信息,包括格式、值、坐标(x、y)、高度、宽度和位置,作为 IronSoftware.Drawing.Rectangle 对象。 IronDrawing中的Rectangle类允许在文档上进行精确定位。

有关更高级的条形码读取场景,请查看我们文档中全面的条形码读取示例

:path=/static-assets/ocr/content-code-examples/how-to/read-results-barcodes.cs
using IronOcr;
using System;
using static IronOcr.OcrResult;

// Instantiate IronTesseract
IronTesseract ocrTesseract = new IronTesseract();

// Enable barcodes detection
ocrTesseract.Configuration.ReadBarCodes = true;

// Add image
using OcrInput ocrInput = new OcrInput();
ocrInput.LoadPdf("sample.pdf");

// Perform OCR
OcrResult ocrResult = ocrTesseract.Read(ocrInput);

// Output information to console
foreach(var barcode in ocrResult.Barcodes)
{
    Console.WriteLine("Format = " + barcode.Format);
    Console.WriteLine("Value = " + barcode.Value);
    Console.WriteLine("X = " + barcode.X);
    Console.WriteLine("Y = " + barcode.Y);
}
Console.WriteLine(ocrResult.Text);
Imports IronOcr
Imports System
Imports IronOcr.OcrResult

' Instantiate IronTesseract
Private ocrTesseract As New IronTesseract()

' Enable barcodes detection
ocrTesseract.Configuration.ReadBarCodes = True

' Add image
Using ocrInput As New OcrInput()
	ocrInput.LoadPdf("sample.pdf")
	
	' Perform OCR
	Dim ocrResult As OcrResult = ocrTesseract.Read(ocrInput)
	
	' Output information to console
	For Each barcode In ocrResult.Barcodes
		Console.WriteLine("Format = " & barcode.Format)
		Console.WriteLine("Value = " & barcode.Value)
		Console.WriteLine("X = " & barcode.X)
		Console.WriteLine("Y = " & barcode.Y)
	Next barcode
	Console.WriteLine(ocrResult.Text)
End Using
$vbLabelText   $csharpLabel

条码检测输出是什么样的?

IronOCR 中的条形码检测功能与文本提取功能无缝集成,提供包括文本内容和条形码数据在内的统一结果。 这种双重能力对于需要提取和关联两种信息类型的自动文档处理工作流程非常有价值。

调试控制台显示 QR 码和 EAN8 条码检测结果,包括格式、值和坐标

输出结果展示了 IronOCR 同时检测多种条形码类型的能力,为每个检测到的代码提供格式标识(如 QRCode 或 EAN8)、解码值和精确的坐标信息。 通过这些全面的数据,开发人员可以构建复杂的文档处理应用程序,高效处理混合内容类型。

常见问题解答

OcrResult 对象包含哪些信息?

IronOCR 的 OcrResult 对象包含提取的文本和详细的元数据,包括精确的 X/Y 坐标、尺寸(宽度和高度)、文本方向(从左到右或从上到下),以及按段落、行、单词和每个检测元素的单个字符组织的分层结构。

如何从 OCR 结果中快速提取第一个单词?

您可以使用 IronOCR 的读取方法提取第一个单词的文本,并访问 Words 集合:`string wordText = new IronTesseract().Read("file.jpg").Words[0].Text;`.这样就可以立即访问 OCR 结果中的单个单词元素。

OCR 结果中有哪些类型的坐标数据?

IronOCR 为每个检测到的元素(段落、行、单词和字符)提供精确的 X 和 Y 坐标,以及宽度和高度尺寸。这些坐标数据可通过 CropRectangle 对象访问,从而实现文本元素的精确位置跟踪。

除了文本内容,我还能提取元数据吗?

是的,IronOCR 可以提取全面的元数据,包括在 PDF 和图像文档中发现的页面、段落、行、单词、字符,甚至条形码。OcrResult 对象提供了对每个元素的文本方向、层次结构和空间信息的访问。

哪些文档类型可以处理 OCR 结果?

IronOCR 可以处理各种文档类型,包括扫描文档、照片、截图、PDF 和图像文件。读取方法可在这些格式中一致运行,返回具有完整元数据的相同结构的 OcrResult 对象。

提取的文本在结果中是如何组织的?

IronOCR 以反映自然文档组织的分层结构组织提取的文本。OcrResult 对象以不同的粒度呈现内容--从整个页面到单个字符--使您可以轻松地在适合您应用的级别处理文本。

Curtis Chau
技术作家

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

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

准备开始了吗?
Nuget 下载 5,384,824 | 版本: 2026.2 刚刚发布