如何使用 IronOCR 在 C# 中提取读取结果
IronOCR的OcrResult对象,其中包括精确坐标、尺寸、文本方向和层级结构(段落、行、单词、字符)对于每个检测元素。
OCR 结果包含有关检测到的段落、行、单词和单个字符的全面信息。
对于每个元素,它提供文本内容、精确的X和Y坐标、尺寸(宽度和高度)、文本方向(从左到右或自上而下)以及在CropRectangle对象中的位置。
快速入门:从第一个检测到的词中获取词文本
使用Words集合提取第一个单词的文本。
最小工作流程(5 个步骤)
- 下载用于访问读取结果的 C# 库
- 准备目标图像和 PDF 文档
- 使用
Read方法对导入的文档执行 OCR 操作。 - 访问结果的
X、Y、width、height和text direction - 检查检测到的段落、行、单词和字符比较
我可以从 OCR 结果中提取哪些数据?
结果值不仅包含提取的文本,还包含 IronOCR 在 PDF 和图像文件中发现的页面、段落、行、单词、字符和条形码等信息。 可以从返回的OcrResult对象中使用Read方法访问此信息。
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}")
如何从 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
输出
控制台输出显示 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
输出
输出结果显示了与三个段落相对应的三组坐标。这些坐标可用于绘制边界框、提取特定区域或维护文本元素之间的空间关系。
OCR 结果中还有哪些其他属性?
除了文本和文本坐标,IronOCR 还提供了其他信息。 对于每个文本元素(段落、行、单词和单个字符),可提供以下信息:
- 文本:作为字符串的实际文本内容。
- X:相对于页面左边缘的像素位置。
- Y:相对于页面顶边缘的像素位置。
- 宽度:以像素为单位的宽度。
- 高度:以像素为单位的高度。
- 文本方向:文本的阅读方向(从左到右或从上到下)。
- 位置:一个矩形,以像素为单位显示该文本在页面上的位置。
这些特性在实施时尤其有用:
- 文本高亮和注释系统
- 自动表单字段检测
- 文档转换中的版面保留
- 用于数据提取的空间文本分析
在调试和可视化方面,请使用 highlight texts 功能 直观验证检测到的区域的准确性。
如何比较段落、行、字和字符?
IronOCR 的分层文本结构允许开发人员根据具体使用情况在适当的细节级别上工作。 了解这些元素之间的差异有助于为您的应用程序选择正确的粒度。
以下是检测到的段落、行、单词和字符的对比。
段落 |
线 |
Word |
特点 |
每个粒度级别都有不同的目的:
- 段落:最适用于文档结构分析和批量文本提取
- 行:用于保持阅读顺序和处理表格数据
- 词语:搜索功能和文本分析的理想选择
- 字符:拼写检查和精确文本编辑应用程序的完美选择
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
条码检测输出是什么样的?
IronOCR 中的条形码检测功能与文本提取功能无缝集成,提供包括文本内容和条形码数据在内的统一结果。 这种双重能力对于需要提取和关联两种信息类型的自动文档处理工作流程非常有价值。
输出结果展示了 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 的免费试用,使用户在做出购买决定之前可以测试其功能和能力。

