如何提取读取结果

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

查克尼特·宾

读取或 OCR 结果包含与检测到的段落、行、单词和单个字符相关的大量信息。对于其中的每一个元素,结果都会提供一套全面的详细信息。

对于每个元素,它都提供了文本内容、精确的 X 坐标和 Y 坐标、维度 (宽度和高度)文本方向 (从左到右或从上到下)在一个 裁剪矩形 反对


适用于OCR的C# NuGet库

安装使用 NuGet

Install-Package IronOcr
Java PDF JAR

下载 DLL

下载DLL

手动安装到你的项目中

适用于OCR的C# NuGet库

安装使用 NuGet

Install-Package IronOcr
Java PDF JAR

下载 DLL

下载DLL

手动安装到你的项目中

开始在您的项目中使用IronPDF,并立即获取免费试用。

第一步:
green arrow pointer

查看 IronOCRNuget 用于快速安装和部署。它有超过800万次下载,正在使用C#改变OCR。

适用于OCR的C# NuGet库 nuget.org/packages/IronOcr/
Install-Package IronOcr

考虑安装 IronOCR DLL 直接。下载并手动安装到您的项目或GAC表单中: IronOcr.zip

手动安装到你的项目中

下载DLL

OcrResult 中的数据

结果值不仅包含提取的文本,还提供了 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}")
VB   C#
OcrResult 中的数据

对于文本的每个部分,如段落、行、单词和单个字符,我们都会提供以下信息:

  • 文本:实际文本字符串。
  • 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
VB   C#

输出

检测条形码

查克尼特·宾

软件工程师

Chaknith 是开发者中的福尔摩斯。他第一次意识到自己可能在软件工程方面有前途,是在他出于乐趣做代码挑战的时候。他的重点是 IronXL 和 IronBarcode,但他为能帮助客户解决每一款产品的问题而感到自豪。Chaknith 利用他从直接与客户交谈中获得的知识,帮助进一步改进产品。他的轶事反馈不仅仅局限于 Jira 票据,还支持产品开发、文档编写和市场营销,从而提升客户的整体体验。当他不在办公室时,他可能会在学习机器学习、编程或徒步旅行。