如何使用C#與IronOCR偵測頁面旋轉
IronOCR 的 DetectPageOrientation 方法能自動識別 PDF 文件和圖像中的頁面旋轉角度(0°、90°、180°、270°)。 它為每頁返回一個 RotationAngle 屬性,從而使程式化的方向校正成為可能,並提供信心水準以便準確提取文本。
頁面旋轉檢測可辨識文件頁面是否已順時針旋轉或逆時針旋轉 0、90、180 或 270 度。 此資訊可確保頁面以正確的方向顯示或處理,以達到精確的呈現和文字擷取。
快速入門:使用DetectPageOrientation 識別頁面旋轉
此範例演示了如何使用 IronOCR 的 DetectPageOrientation 在 PDF 上訪問 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對於需要額外預處理的文件,考慮使用 OcrInput 類,其中提供了在 OCR 處理前的廣泛文件準備方法。
如何自定義檢測速度和準確性?
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對於涉及 掃描文件 的複雜情況,這些文件具有不同的品質或 多頁 TIFFs,請個別預處理每一頁以獲得最佳結果。
處理混合格式的輸入時,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 參數,允許使用者根據需要調整檢測速度和準確性,範圍從快速到極端詳細模式。
此功能是否最適用於某些類型的文件?
IronOCR 的 DetectPageOrientation 功能在處理文字密集的文件時表現最佳。對於文字極少或版面複雜的文件,可考慮在偵測前套用影像品質修正濾鏡,以獲得最佳結果。
如何在檢測大型PDF的方向時限制記憶體使用?
將 maxDegreeOfParallelism 值傳遞給 DetectPageOrientation 重載,例如 input.DetectPageOrientation(OrientationDetectionMode.Fast, maxDegreeOfParallelism: 2)。這限制了同時分析的頁數,因為每個並行頁使用其自己的本地引擎,因此直接限制了最高記憶體。非正值則回退到 Environment.ProcessorCount。
添加 maxDegreeOfParallelism 參數是否會破壞現有程式碼?
不會。單參數的 DetectPageOrientation 重載未改變,並且仍然完全支持,因此現有程式碼可以繼續運行而無需修改。在快速模式下,因為每個工作執行緒重用一個引擎而不是每頁分配一個引擎,方向檢測也更加高效,結果不變。
Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。