如何在 C# 中檢測頁面旋轉以進行 OCR | IronOCR

如何使用 IronOCR 在 C# 中偵測頁面旋轉

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

IronOCR 的 DetectPageOrientation 方法可自動識別 PDF 文件和影像中的頁面旋轉角 (0°、90°、180°、270°)。 它會回傳每個頁面的 RotationAngle 屬性,使程式化的方向修正與信心分數能夠準確地擷取文字。

頁面旋轉檢測可辨識文件頁面是否已順時針旋轉或逆時針旋轉 0、90、180 或 270 度。 此資訊可確保頁面以正確的方向顯示或處理,以達到精確的呈現和文字擷取。

快速入門:使用 DetectPageOrientation 來辨識頁面旋轉

<! -- 引言實作示意圖 --> <!--說明:說明程式碼概念的圖表或截圖 -->

本範例示範在 PDF 上使用 IronOCR 的 DetectPageOrientation 來存取 RotationAngle 屬性。 它以最少的程式碼提供快速的頁面旋轉檢測與修正。

Nuget Icon立即開始使用 NuGet 建立 PDF 檔案:

  1. 使用 NuGet 套件管理器安裝 IronOCR

    PM > Install-Package IronOcr

  2. 複製並運行這段程式碼。

    var rotationResults = new IronOcr.OcrInput().LoadPdf("doc.pdf").DetectPageOrientation();
    Console.WriteLine(rotationResults.First().RotationAngle);
  3. 部署到您的生產環境進行測試

    立即開始在您的專案中使用 IronOCR,免費試用!
    arrow pointer


如何在我的文件中檢測頁面旋轉?

<! -- 螢幕截圖示範如何偵測文件中的頁面旋轉? in IronPDF-->譯文 <!--說明:顯示逐步過程的截圖 -->

載入文件後,使用 DetectPageOrientation 方法來識別每個頁面的旋轉。 此方法支援 0、90、180 及 270 度。 對於這些標準旋轉之外的歪斜影像,請使用 IronOCR 的影像修正濾鏡中的 Deskew 方法。 然後使用偵測到的角度將影像旋轉回原始方向。 讓我們使用 範例 PDF

[{i:(此功能對於文字密集的文件效果最佳。

:path=/static-assets/ocr/content-code-examples/how-to/detect-page-rotation-detect-page-rotation.cs
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
$vbLabelText   $csharpLabel

檢測結果代表什麼意義?

  • 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
$vbLabelText   $csharpLabel

對於需要額外預處理的文件,可以考慮使用 OcrInput 類別,它在 OCR 處理之前提供了廣泛的文件準備方法。

如何自訂偵測速度和精確度?

<! -- 螢幕截圖示範如何自訂偵測速度和精確度? in IronPDF-->譯文 <!--說明:顯示逐步過程的截圖 -->

DetectPageOrientation 方法接受一個可選參數以控制偵測細節。 透過提供 OrientationDetectionMode 枚舉,您可以根據需求調整偵測速度和精確度。

以下是實作方法:

:path=/static-assets/ocr/content-code-examples/how-to/detect-page-rotation-detect-page-rotation-advanced.cs
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
$vbLabelText   $csharpLabel

我應該選擇哪一種偵測模式?

OrientationDetectionMode 提供四種速度選項:

[{w:(Balanced, Detailed, 和 ExtremeDetailed需要 IronOcr.Extensions.AdvancedScan 套件。 這些選項在 Windows x86 和 Mac ARM 上無法使用。

  • 快速:高速偵測,準確度較低。 非常適合用於速度極為重要的草稿或大量處理。 DetectPageOrientation 的預設值。 透過 多執行緒支援,有效率地處理數以千計的頁面。
  • 平衡:平衡速度和準確性。 適合生產任務。 使用 AdvancedScan 延伸功能,以提高精確度,同時維持效能。
  • 詳細:低速度、高精度。 最適合精確或關鍵的任務,尤其是版面複雜或內容混雜的文件。
  • ExtremeDetailed:速度最慢,精確度最高。 僅在 Detailed 不足或文字嚴重歪斜扭曲時使用。

哪些是常見的效能考量?

不同模式的效能差異很大。 快速模式每分鐘可處理數百頁; ExtremeDetailed 每頁可能需要數秒。 根據精確度要求和時間限制進行選擇。 為達到最佳效能:

1.影像解析度:較高的DPI設定可提高精確度,但會增加處理時間。150-300 DPI 通常已足以進行旋轉偵測。 2.文件類型:與稀疏的版面相比,文字密集的文件處理速度更快、更準確。 使用 Filter Wizard 在偵測前優化影像品質。 3.資源使用量:在處理大量批次時,監控記憶體使用量。 實施進度追蹤以提供回饋和管理系統資源。 4.平行處理:針對大量作業,可使用 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 (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
$vbLabelText   $csharpLabel

對於涉及不同品質的掃描文件多頁 TIFF 文件的複雜情況,請個別預先處理每一頁,以獲得最佳效果。

在處理混合格式輸入時,OcrResult Class 會提供詳細的頁面資訊,讓複雜的錯誤處理和品質控制工作流程得以實現。 對於高產量的生產環境,請探索 Fast OCR Configuration 選項,以平衡速度與精確度。

如果要處理同時包含文字和 BarCode 的文件,請使用 IronOCR 的 OCR with Barcode & QR Reading 功能,一次擷取所有資訊,提高效率。

常見問題解答

什麼是頁面輪轉檢測,為什麼它很重要?

頁面旋轉檢測可辨識文件頁面是否已旋轉 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 影像修正濾鏡中的 Deskew 方法。

DetectPageOrientation 會傳回哪些資訊?

該方法會為每個頁面傳回三個關鍵屬性:PageNumber (以零為基礎的索引)、RotationAngle (以度為單位的旋轉,用於 IronOCR 的 Rotate 方法),以及 HighConfidence (置信度,用於處理邊緣情況)。

何時應該使用 HighConfidence 屬性?

當處理含糊不清或低品質的文件時,使用 HighConfidence 屬性,因為在這些文件中,旋轉偵測可能無法確定。具有稀疏文字、不尋常佈局或掃描品質較差的文件通常會在 IronOCR 中返回較低的置信度分數,需要額外的驗證或影像品質修正篩選器。

此功能是否最適用於某些類型的文件?

IronOCR 的 DetectPageOrientation 功能在處理文字密集的文件時表現最佳。對於文字極少或版面複雜的文件,可考慮在偵測前套用影像品質修正濾鏡,以獲得最佳結果。

Curtis Chau
技術撰稿人

Curtis Chau 擁有電腦科學學士學位(卡爾頓大學),專長於前端開發,精通 Node.js、TypeScript、JavaScript 和 React。Curtis 對製作直覺且美觀的使用者介面充滿熱情,他喜歡使用現代化的架構,並製作結構良好且視覺上吸引人的手冊。

除了開發之外,Curtis 對物聯網 (IoT) 也有濃厚的興趣,他喜歡探索整合硬體與軟體的創新方式。在空閒時間,他喜歡玩遊戲和建立 Discord bots,將他對技術的熱愛與創意結合。

審核人
Jeff Fritz
Jeffrey T. Fritz
首席計畫經理 - .NET 社群團隊
Jeff 也是 .NET 和 Visual Studio 團隊的首席計畫經理。他是 .NET Conf 虛擬會議系列的執行製作人,並主持「Fritz and Friends」開發人直播串流,每週播出兩次,與觀眾一起討論技術和編寫程式碼。Jeff 為 Microsoft Build、Microsoft Ignite、.NET Conf 和 Microsoft MVP Summit 等大型 Microsoft 開發人員活動撰寫工作坊、簡報和規劃內容。
準備好開始了嗎?
Nuget 下載 5,384,824 | 版本: 2026.2 剛剛發布