如何使用 IronOCR 在 C# 中检测页面旋转
IronOCR 的 DetectPageOrientation 方法自动识别 PDF 文档和图像中的页面旋转角度(0°、90°、180°、270°)。 它为每个页面返回一个 RotationAngle 属性,允许通过信心评分对文本提取进行准确的程序化方向校正。
页面旋转检测可识别文档页面是否顺时针或逆时针旋转了 0、90、180 或 270 度。 这些信息可确保页面以正确的方向显示或处理,从而实现准确的渲染和文本提取。
快速入门:使用DetectPageOrientation 识别页面旋转
此示例演示如何在 PDF 上使用 IronOCR 的 DetectPageOrientation 访问 RotationAngle 属性。 它以最少的代码提供快速的页面旋转检测和校正。
-
1使用 IronOCR 安装 NuGet Package Manager
-
2复制并运行这段代码。
var rotationResults = new IronOcr.OcrInput().LoadPdf("doc.pdf").DetectPageOrientation(); Console.WriteLine(rotationResults.First().RotationAngle);Dim rotationResults = New IronOcr.OcrInput().LoadPdf("doc.pdf").DetectPageOrientation() Console.WriteLine(rotationResults.First().RotationAngle)Dim rotationResults = New IronOcr.OcrInput().LoadPdf("doc.pdf").DetectPageOrientation() Console.WriteLine(rotationResults.First().RotationAngle) -
3部署到您的生产环境中进行测试
今天就用免费试用在您的项目中开始使用IronOCR
最小工作流程(5 个步骤)
- 下载用于检测页面旋转的 C# 库
- 导入 PDF 文档和图像进行读取
- 使用
DetectPageOrientation方法检测所有页面的旋转方向 - 访问RotationAngle属性以校正页面旋转
- 访问HighConfidence属性以处理极端情况
如何检测文档中的页面旋转?
加载文档后,使用 DetectPageOrientation 方法识别每个页面的旋转。 此方法支持 0、90、180 和 270 度。 对于超出这些标准旋转的倾斜图像,请使用来自IronOCR 的图像校正过滤器 的 Deskew 方法。 然后使用检测到的角度将图像旋转回原始方向。 让我们处理一个示例 PDF。
using IronOcr;
using System;
using var input = new OcrInput();
// Load PDF document
input.LoadPdf("Clockwise90.pdf");
// Detect page rotation
var results = input.DetectPageOrientation();
// Ouput result
foreach(var result in results)
{
Console.WriteLine(result.PageNumber);
Console.WriteLine(result.HighConfidence);
Console.WriteLine(result.RotationAngle);
}using IronOcr;
using System;
using var input = new OcrInput();
// Load PDF document
input.LoadPdf("Clockwise90.pdf");
// Detect page rotation
var results = input.DetectPageOrientation();
// Ouput result
foreach(var result in results)
{
Console.WriteLine(result.PageNumber);
Console.WriteLine(result.HighConfidence);
Console.WriteLine(result.RotationAngle);
}Imports IronOcr
Imports System
Private input = New OcrInput()
' Load PDF document
input.LoadPdf("Clockwise90.pdf")
' Detect page rotation
Dim results = input.DetectPageOrientation()
' Ouput result
For Each result In results
Console.WriteLine(result.PageNumber)
Console.WriteLine(result.HighConfidence)
Console.WriteLine(result.RotationAngle)
Next resultImports IronOcr
Imports System
Private input = New OcrInput()
' Load PDF document
input.LoadPdf("Clockwise90.pdf")
' Detect page rotation
Dim results = input.DetectPageOrientation()
' Ouput result
For Each result In results
Console.WriteLine(result.PageNumber)
Console.WriteLine(result.HighConfidence)
Console.WriteLine(result.RotationAngle)
Next result检测结果意味什么?
PageNumber:页面的零起始索引。RotationAngle:旋转角度(以度为单位)。 结合Rotate方法使用以修正方向。HighConfidence:处理边缘案例时,确定方向结果的置信级别。
我何时应使用高置信值?
对于旋转检测可能不确定的模糊或低质量文档,HighConfidence 属性至关重要。 文本稀疏、布局异常或扫描质量较差的文档通常会降低置信度分数。 在这些情况下,检测前应实施额外验证或应用图像质量校正过滤器。
使用此值可对置信度较低的页面实施后备策略或人工审核。 例如,如果置信度低于 80%,则处理页面的多个方向并比较 OCR 结果,或标记为人工审核。 IronOCR 的计算机视觉功能有助于更准确地识别复杂文档中的文本区域。
如何校正检测到的旋转?
识别旋转角度后,在您的 OcrInput 对象上使用 Rotate 方法于 OCR 之前校正方向。 这样才能确保最佳的文本识别准确性。 若需完整的方向修正,请参见图像方向校正指南。 以下是校正过程:
// Apply rotation correction based on detection results
if (result.RotationAngle != 0)
{
input.Rotate(360 - result.RotationAngle); // Rotate back to 0°
}// Apply rotation correction based on detection results
if (result.RotationAngle != 0)
{
input.Rotate(360 - result.RotationAngle); // Rotate back to 0°
}' Apply rotation correction based on detection results
If result.RotationAngle <> 0 Then
input.Rotate(360 - result.RotationAngle) ' Rotate back to 0°
End If' Apply rotation correction based on detection results
If result.RotationAngle <> 0 Then
input.Rotate(360 - result.RotationAngle) ' Rotate back to 0°
End If对于需要额外预处理的文档,请考虑使用提供 OCR 处理前广泛文档准备方法的OcrInput 类。
如何自定义检测速度和准确性?
DetectPageOrientation 方法接受可选参数以控制检测细节和资源使用。 通过提供 OrientationDetectionMode 枚举,可以根据您的要求调整检测速度和准确性,第二个 maxDegreeOfParallelism 参数限制同时分析的页面数量。
以下是实施方法:
using IronOcr;
using System;
using var input = new OcrInput();
// Load PDF document
input.LoadPdf("Clockwise90.pdf");
// Detect page rotation with Fast mode
var results = input.DetectPageOrientation(OrientationDetectionMode.Fast);
// Ouput result
foreach(var result in results)
{
Console.WriteLine(result.PageNumber);
Console.WriteLine(result.HighConfidence);
Console.WriteLine(result.RotationAngle);
}using IronOcr;
using System;
using var input = new OcrInput();
// Load PDF document
input.LoadPdf("Clockwise90.pdf");
// Detect page rotation with Fast mode
var results = input.DetectPageOrientation(OrientationDetectionMode.Fast);
// Ouput result
foreach(var result in results)
{
Console.WriteLine(result.PageNumber);
Console.WriteLine(result.HighConfidence);
Console.WriteLine(result.RotationAngle);
}Imports IronOcr
Imports System
Using input As New OcrInput()
' Load PDF document
input.LoadPdf("Clockwise90.pdf")
' Detect page rotation with Fast mode
Dim results = input.DetectPageOrientation(OrientationDetectionMode.Fast)
' Output result
For Each result In results
Console.WriteLine(result.PageNumber)
Console.WriteLine(result.HighConfidence)
Console.WriteLine(result.RotationAngle)
Next
End UsingImports IronOcr
Imports System
Using input As New OcrInput()
' Load PDF document
input.LoadPdf("Clockwise90.pdf")
' Detect page rotation with Fast mode
Dim results = input.DetectPageOrientation(OrientationDetectionMode.Fast)
' Output result
For Each result In results
Console.WriteLine(result.PageNumber)
Console.WriteLine(result.HighConfidence)
Console.WriteLine(result.RotationAngle)
Next
End Using我应该选择哪种检测模式?
对于 OrientationDetectionMode,提供四种速度选项:
IronOcr.Extensions.AdvancedScan 包。 这些选项在 Windows x86 和 Mac ARM 上不可用。- 快速: 提供较低准确性的高速检测。 适用于对速度要求极高的草稿或批量处理。
DetectPageOrientation的默认设置。 使用多线程支持可高效处理成千上万页,并为每个工作线程重用一个引擎而不是为每页分配一个引擎,从而保持长文档的内存使用率低。 - 平衡: 平衡速度和准确性。 适用于生产任务。 使用AdvancedScan 扩展能力提高准确性,同时保持性能。
- 详细: 低速,高准确性。 最适合精确或关键任务,尤其是具有复杂布局或混合内容的文档。
- 极致详细: 最慢的速度,最高的准确性。 仅在 Detailed 不足或文本严重倾斜和扭曲时使用。
在方向检测期间如何限制内存?
对于多页输入,方向检测会同时分析多个页面,每个并发页面使用其自己的原生引擎。传递一个 maxDegreeOfParallelism 值以限制并发性并降低峰值内存消耗——在内存受限的服务器和容器中非常有用,并且可以镜像同时读取文档时可用的 MaxDegreeOfParallelism 控制。
using IronOcr;
using System.Collections.Generic;
using var input = new OcrInput();
input.LoadPdf("scanned.pdf");
// Cap orientation detection to 2 pages at a time. The default is one per CPU core.
IEnumerable<OcrPageOrientationResult> orientation =
input.DetectPageOrientation(OrientationDetectionMode.Fast, maxDegreeOfParallelism: 2);using IronOcr;
using System.Collections.Generic;
using var input = new OcrInput();
input.LoadPdf("scanned.pdf");
// Cap orientation detection to 2 pages at a time. The default is one per CPU core.
IEnumerable<OcrPageOrientationResult> orientation =
input.DetectPageOrientation(OrientationDetectionMode.Fast, maxDegreeOfParallelism: 2);Imports IronOcr
Imports System.Collections.Generic
Dim input As New OcrInput()
input.LoadPdf("scanned.pdf")
' Cap orientation detection to 2 pages at a time. The default is one per CPU core.
Dim orientation As IEnumerable(Of OcrPageOrientationResult) =
input.DetectPageOrientation(OrientationDetectionMode.Fast, maxDegreeOfParallelism:=2)Imports IronOcr
Imports System.Collections.Generic
Dim input As New OcrInput()
input.LoadPdf("scanned.pdf")
' Cap orientation detection to 2 pages at a time. The default is one per CPU core.
Dim orientation As IEnumerable(Of OcrPageOrientationResult) =
input.DetectPageOrientation(OrientationDetectionMode.Fast, maxDegreeOfParallelism:=2)Environment.ProcessorCount。 单参数 DetectPageOrientation 重载未更改并继续完全支持,因此现有代码无需修改即可继续工作。常见的性能考虑有哪些?
不同模式的性能差异很大。 快速模式针对高吞吐量处理进行了优化,而极致详细在速度上优先考虑准确性。 根据准确性要求和时间限制进行选择。 实现最佳性能:
- 图像分辨率: 更高的DPI 设置可提高准确性,但会增加处理时间。通常,150-300 DPI 足以进行旋转检测。
- 文档类型: 文本密集型文档处理速度更快且更准确,而稀疏布局则较慢。 使用过滤器向导在检测前优化图像质量。
- 资源使用: 处理大批量时监控内存使用,传递一个
maxDegreeOfParallelism值以限制一次分析的页面数量。 实施进度跟踪以提供反馈并管理系统资源。 - 并行处理: 对于批量操作,请使用 IronOCR 的多线程功能同时处理多个文档,同时保持准确性。 在内存受限时使用
MaxDegreeOfParallelism对其进行限制。
如何处理混合方向的文档?
对于混合方向的文档,使用 DetectPageOrientation 单独处理每一页,然后在 OCR 之前应用逐页旋转校正。 无论初始状态如何,都要确保正确的定位。 以下是一种有效的方法:
// Process each page with individual rotation detection
for (int i = 0; i < results.Count; i++)
{
var pageResult = results[i];
// Apply rotation only to pages that need it
if (pageResult.RotationAngle != 0 && pageResult.HighConfidence)
{
// Load the specific page as its own input and correct it
using var pageInput = new OcrInput();
pageInput.LoadPdfPage("doc.pdf", i);
pageInput.Rotate(360 - pageResult.RotationAngle);
}
}// Process each page with individual rotation detection
for (int i = 0; i < results.Count; i++)
{
var pageResult = results[i];
// Apply rotation only to pages that need it
if (pageResult.RotationAngle != 0 && pageResult.HighConfidence)
{
// Load the specific page as its own input and correct it
using var pageInput = new OcrInput();
pageInput.LoadPdfPage("doc.pdf", i);
pageInput.Rotate(360 - pageResult.RotationAngle);
}
}' Process each page with individual rotation detection
For i As Integer = 0 To results.Count - 1
Dim pageResult = results(i)
' Apply rotation only to pages that need it
If pageResult.RotationAngle <> 0 AndAlso pageResult.HighConfidence Then
' Load the specific page as its own input and correct it
Using pageInput = New OcrInput()
pageInput.LoadPdfPage("doc.pdf", i)
pageInput.Rotate(360 - pageResult.RotationAngle)
End Using
End If
Next i' Process each page with individual rotation detection
For i As Integer = 0 To results.Count - 1
Dim pageResult = results(i)
' Apply rotation only to pages that need it
If pageResult.RotationAngle <> 0 AndAlso pageResult.HighConfidence Then
' Load the specific page as its own input and correct it
Using pageInput = New OcrInput()
pageInput.LoadPdfPage("doc.pdf", i)
pageInput.Rotate(360 - pageResult.RotationAngle)
End Using
End If
Next i涉及扫描文件的复杂情况质量或多页 TIFF,请分别预处理每一页以获得最佳结果。
在处理混合格式输入时,OcrResult 类提供详细的页面信息,支持复杂的错误处理和质量控制工作流。 对于高吞吐量生产环境,探索快速 OCR 配置选项以平衡速度和准确性。
如果处理包含文本和条形码的文档,请使用 IronOCR 的OCR 与条形码和 QR 读取功能在一次通过中提取所有信息,提高效率。
常见问题
IronOCR如何检测页面旋转?
IronOCR使用DetectPageOrientation方法自动识别PDF和图像中页面的旋转角度,检测0°、90°、180°和270°的方向,以确保准确的文本提取。
IronOCR中RotationAngle属性的目的是什么?
IronOCR中的RotationAngle属性用于指示每个页面检测到的旋转角度。这使开发人员可以编程方式地纠正页面的方向,以便准确地渲染和提取文本。
IronOCR能处理非标准的页面旋转吗?
是的,对于超出标准0°、90°、180°和270°旋转的倾斜图像,IronOCR建议使用其图像校正过滤器中的Deskew方法,使图像正确定位。
IronOCR中的HighConfidence属性是什么?
IronOCR中的HighConfidence属性反映了所检测到方向的置信水平。对于有歧义或低质量扫描的文档,该属性特别有用,可能需要更多验证。
IronOCR如何提高检测速度和精度?
IronOCR为DetectPageOrientation提供了可选的OrientationDetectionMode参数,允许用户根据需要调整检测速度和精度,从Fast到ExtremeDetailed模式。
此功能对某些类型的文档是否最有效?
IronOCR 的 DetectPageOrientation 功能在文本密集的文档中表现最佳。对于文本较少或布局复杂的文档,可考虑在检测前应用图像质量校正过滤器,以获得最佳效果。
在检测大PDF文件的方向时,我如何限制内存使用?
传递一个maxDegreeOfParallelism值给DetectPageOrientation重载,例如input.DetectPageOrientation(OrientationDetectionMode.Fast, maxDegreeOfParallelism: 2)。这限制了同时分析的页面数量,因为每个并发页面使用其自己的本地引擎,直接限制峰值内存。非正值回退到Environment.ProcessorCount。
添加maxDegreeOfParallelism参数会破坏现有代码吗?
不会。单参数的DetectPageOrientation重载没有变化并且完全支持,因此现有代码无需修改即可继续工作。在Fast模式下,方向检测也更加节省内存,因为它每个工作线程重用一个引擎,而不是为每个页面分配一个,并且结果不变。
Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。