如何提取读取结果
读取或 OCR 结果包含与检测到的段落、行、单词和单个字符相关的大量信息。对于其中的每一个元素,结果都会提供一套全面的详细信息。
对于每个元素,它都提供了文本内容、精确的 X 坐标和 Y 坐标、维度 (宽度和高度)文本方向 (从左到右或从上到下)在一个 裁剪矩形 反对
如何提取读取结果
- 下载用于访问读取结果的 C# 库
- 准备目标图像和 PDF 文档
- 使用
读取
方法对导入的文件执行 OCR 识别 - 访问结果的 X、Y、宽度、高度和文字方向
- 检查检测到的段落、行、单词和字符比较
开始在您的项目中使用IronPDF,并立即获取免费试用。
查看 IronOCR 上 Nuget 用于快速安装和部署。它有超过800万次下载,正在使用C#改变OCR。
Install-Package IronOcr
考虑安装 IronOCR DLL 直接。下载并手动安装到您的项目或GAC表单中: IronOcr.zip
手动安装到你的项目中
下载DLLOcrResult 中的数据
结果值不仅包含提取的文本,还提供了 IronOcr 在 PDF 和图像文档中发现的有关页面、段落、行、单词、字符和条形码的信息。您可以使用 "Read "方法从返回的 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}")
对于文本的每个部分,如段落、行、单词和单个字符,我们都会提供以下信息:
- 文本:实际文本字符串。
- X:页面左边缘的位置,单位为像素。
- Y:从页面顶部边缘算起的位置,单位为像素。
- 宽度:宽度,单位为像素。
- Height:高度,以像素为单位:高度,单位为像素。
文本方向:文字的阅读方向,如 "从左到右 "或 "从上到下"。
- 位置:以像素为单位显示文字在页面上位置的矩形。
段落、行、字和字符比较
下面是检测到的段落、行、单词和字符的比较。
段落 | 线路 |
字词 | 人物 |
条形码和 QR 码
正确! IronOcr 可以读取条形码和 QR 码。虽然该功能可能不如 IronBarcode 那么强大,但 IronOcr 确实支持常见的条形码类型。要启用条形码检测,请将 Configuration.ReadBarCodes 属性设置为 true。
此外,还可以从检测到的条形码中提取有价值的信息,包括其格式、值、坐标等。 (x, y)、高度、宽度和位置作为 IronSoftware.Drawing.Rectangle 对象。该矩形类在 IronDrawing 可在文档上精确定位。
: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