如何使用 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 集合提取第一个 WORD 的文本。

  1. 使用 NuGet 包管理器安装 https://www.nuget.org/packages/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-coordinates.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;

Console.WriteLine("--- All Detected Coordinates of Paragraphs ---");

// Use a 'for' loop to get an index for each paragraph
for (int i = 0; i < paragraphs.Length; i++)
{
    // Get the current paragraph
    Paragraph paragraph = paragraphs[i];

    // Print the paragraph number, text, and its location
    Console.WriteLine($"X: {paragraph.X}"); // X-coordinate (left edge)
    Console.WriteLine($"Y: {paragraph.Y}"); // Y-coordinate (top edge)

    // Add a blank line for better separation
    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

    Console.WriteLine("--- All Detected Coordinates of Paragraphs ---")

    ' Use a 'for' loop to get an index for each paragraph
    For i As Integer = 0 To paragraphs.Length - 1
        ' Get the current paragraph
        Dim paragraph As Paragraph = paragraphs(i)

        ' Print the paragraph number, text, and its location
        Console.WriteLine($"X: {paragraph.X}") ' X-coordinate (left edge)
        Console.WriteLine($"Y: {paragraph.Y}") ' Y-coordinate (top edge)

        ' Add a blank line for better separation
        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 码。 虽然 IronOCR 的功能可能不如 IronBarcode 强大,但它支持常见的 BarCode 类型。要启用 BarCode 检测,请将 Configuration.ReadBarCodes 属性设置为 true。 这种集成功能使 IronOCR 成为处理包含文本和 BarCode 的文档(如发票、发货标签或产品目录)的绝佳选择。

此外,可从检测到的 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 对象以不同的粒度呈现内容--从整个页面到单个字符--使您可以轻松地在适合您应用的级别处理文本。

IronOCR可以集成到现有应用程序中吗?

IronOCR设计为易于使用C#集成到现有应用程序中,允许开发人员以最小的努力为他们的软件添加OCR功能。

使用IronOCR进行文档管理有什么好处?

使用IronOCR进行文档管理可以通过将扫描的文档转换为可搜索和可编辑文本来简化工作流程,减少手动数据输入的需要,提高文档可访问性。

IronOCR如何提高数据准确性?

IronOCR通过其高级识别算法和图像校正功能提高数据准确性,确保文本提取过程既可靠又精确。

IronOCR 有免费试用版吗?

是的,Iron Software 提供IronOCR 的免费试用,使用户在做出购买决定之前可以测试其功能和能力。

Curtis Chau
技术作家

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

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

准备开始了吗?
Nuget 下载 5,888,303 | 版本: 2026.5 just released
Still Scrolling Icon

还在滚动吗?

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