如何使用IronOCR在C#中檢測頁面旋轉
IronOCR的DetectPageOrientation方法會自動識別PDF文件和圖像中的頁面旋轉角度(0°、90°、180°、270°)。 它為每頁返回RotationAngle屬性,使程式能夠打擊方向矯正,並提供信心分數以準確提取文字。
頁面旋轉偵測識別文件頁面是順時針還是逆時針旋轉了0、90、180或270度。 此資訊確保頁面以正確的方向顯示或處理,以便準確呈現和提取文字。
DetectPageOrientation識別頁面旋轉@@--AH2EG--@@
此範例演示了在PDF上使用IronOCR的RotationAngle屬性。 它提供快速的頁面旋轉偵測和矯正,所需程式碼極少。
-
1Install IronOCR with NuGet Package Manager
-
2複製並運行這段程式碼片段。
var rotationResults = new IronOcr.OcrInput().LoadPdf("doc.pdf").DetectPageOrientation(); Console.WriteLine(rotationResults.First().RotationAngle);C# -
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);
}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 result偵測結果的意義是什麼?
PageNumber: 頁面的零基索引。RotationAngle: 旋轉角度(度數)。 使用Rotate方法進行方向矯正。HighConfidence: 用於處理邊緣情況的方向結果的信心水平。
什麼時候應該使用高信心值?
HighConfidence屬性對於旋轉偵測不確定的模糊或低質量文件尤為重要。 包含稀疏文字、不尋常的佈局或掃描質量差的文件通常會返回較低的信心水平。 在這些情況下,實施額外的驗證或在偵測之前應用圖像質量矯正過濾器。
使用此值為低信心頁面實施備用策略或人工審查。 例如,如果信心低於80%,則以多個方向處理該頁面並比較OCR結果,或標記進行人工審查。 IronOCR的計算機視覺功能有助於在挑戰性文件中更準確地識別文字區域。
如何矯正偵測到的旋轉?
識別旋轉角度後,請在您的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 Then
input.Rotate(360 - result.RotationAngle) ' Rotate back to 0°
End If對於需要額外預處理的文件,請考慮OcrInput類,其在OCR處理之前提供廣泛的文件準備方法。
我如何自訂偵測速度和準確性?
DetectPageOrientation方法接受可選參數來控制偵測細節。 通過提供OrientationDetectionMode枚舉,您可以根據需求調整偵測速度和準確性。
以下是如何實施:
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 Using我應該選擇哪種偵測模式?
四種速度選項可供OrientationDetectionMode選用:
IronOcr.Extensions.AdvancedScan包。 這些選項在Windows x86和Mac ARM上不可用。。 )}]
- 快速: 高速偵測但準確性較低。 適用於行稿或批量處理,那裡速度至關重要。 預設為
DetectPageOrientation。 使用多執行緒支持來有效處理數千頁。 - 平衡: 平衡的速度和準確性。 適合生產任務。 使用AdvancedScan擴展功能來提高準確性,同時保持性能。
- 詳細: 低速,高準確性。 最適合同樣準確或關鍵的任務,尤其是具有複雜佈局或混合內容的文件。
- 極其詳盡: 速度最慢,但準確性最高。 僅當詳細不夠或文字高度傾斜和失真時使用。
常見的性能考量是什麼?
性能在不同模式之間有很大差異。 快速模式每分鐘處理數百頁; 極其詳盡的模式每頁可能需要秒數。 根據準確性要求和時間限制選擇。 為了獲得最佳性能:
- 圖像解析度: 更高的DPI設置提高了準確性,但會增加處理時間。150-300 DPI通常足以轉動偵測。
- 文件型別: 文字密集的文件比稀疏的佈局處理得更快更準確。 在偵測之前使用過濾嚮導優化圖像質量。
- 資源使用: 處理大型批次時監控記憶體使用。 實施進度跟蹤以提供反饋並管理系統資源。
- 並行處理: 對於批量操作,使用IronOCR的多執行緒處理,同時保持準確性。
如何處理混合方向文件?
對於混合方向的文件,逐頁使用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)
{
// Correct the specific page
input.Pages[i].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
' Correct the specific page
input.Pages(i).Rotate(360 - pageResult.RotationAngle)
End If
Next對於涉及不同質量的掃描文件或多頁TIFF的復雜場景,逐頁預處理以獲得最佳效果。
處理混合格式輸入時,OcrResult類提供詳細的頁面資訊,從而實現複雜的錯誤處理和質量控制工作流。 對於高吞吐量的生產環境,探索快速OCR配置選項以平衡速度和準確性。
如果處理含有文字和條形碼的文件,使用IronOCR的OCR與條形碼和QR閱讀功能在一次通行中提取所有資訊,提高效率。
常見問題
什麼是頁面旋轉檢測,為什麼它很重要?
頁面旋轉檢測識別文件頁面是否已旋轉0°、90°、180°或270°度。這一點對IronOCR至關重要,可以確保頁面以正確的方向處理,從而能從PDF和圖像中準確地提取和渲染文字。
如何使用C#快速檢測PDF中的頁面旋轉?
使用IronOCR的DetectPageOrientation方法,僅需最少的程式碼:var rotationResults = new IronOcr.OcrInput().LoadPdf("doc.pdf").DetectPageOrientation(); 這將返回所有頁面的旋轉資訊,透過RotationAngle屬性即可存取。
可以檢測哪些旋轉角度?
IronOCR的DetectPageOrientation方法可以檢測標準的0°、90°、180°和270°度旋轉。對於超出這些標準旋轉的歪斜圖像,請使用IronOCR的去歪斜方法來進行校正。
DetectPageOrientation返回什麼資訊?
此方法為每頁返回三個主要屬性:PageNumber(零基索引)、RotationAngle(用於搭配IronOCR的Rotate方法的旋轉角度)和HighConfidence(處理邊界案例的信心水平)。
什麼時候應該使用HighConfidence屬性?
當處理模糊或低質量的文件時使用HighConfidence屬性,因為這些文件的旋轉檢測結果可能不確定。文字稀疏、佈局不尋常或掃描質量差的文件經常在IronOCR中返回較低的信心分數,這需要進一步驗證或使用圖像質量校正過濾器。
該功能適合於某些型別的文件嗎?
IronOCR的DetectPageOrientation功能在文字密集的文件中表現最佳。對於文字少或佈局複雜的文件,建議在檢測前應用圖像質量校正過濾器,以獲得最佳結果。
How do I limit memory usage when detecting orientation on large PDFs?
Pass a maxDegreeOfParallelism value to the DetectPageOrientation overload, for example input.DetectPageOrientation(OrientationDetectionMode.Fast, maxDegreeOfParallelism: 2). This caps how many pages are analysed at the same time, and because each concurrent page uses its own native engine, it directly caps peak memory. A non-positive value falls back to Environment.ProcessorCount.
Does adding the maxDegreeOfParallelism parameter break existing code?
No. The single-argument DetectPageOrientation overload is unchanged and remains fully supported, so existing code continues to work without modification. In Fast mode, orientation detection is also more memory efficient because it reuses one engine per worker thread rather than allocating one per page, and results are unchanged.

Curtis Chau擁有Carleton大學的電腦科學學士學位,專精於前端開發,擁有Node.js、TypeScript、JavaScript和React的專業知識。Curtis熱衷於建立直觀且美觀的使用者介面,喜愛使用現代框架並建立結構良好、視覺吸引力的手冊。