如何使用IronOCR在 C# 中擷取 OCR辨識讀取結果與圖片轉文字輸出
IronOCR 的 Read 方法傳回一個 OcrResult 對象,其中包含 OCR辨識擷取的文字以及詳細的元數據,包括每個偵測到的元素的精確座標、尺寸、文字方向和層次結構(段落、行、單字、字元),實現完整的圖片轉文字工作流程。
OCR 結果包含有關檢測到的段落、行、單字和單字的全面資訊。
對於每個元素,它提供文字內容、精確的 X 和 Y 座標、尺寸(寬度和高度)、文字方向(從左到右或從上到下)以及在CropRectangle物件中的位置。
快速入門:從檢測到的第一個字檢索單字文字
使用 IronTesseract 的 Read 方法對圖像進行 OCR,並使用 單字s 集合提取第一個單字的文字。
最簡工作流程(5個步驟)
- 下載用於存取讀取結果的 C# 庫
- 準備目標影像和PDF文檔
- 使用`Read`方法對匯入的文件執行 OCR 操作。
- 取得結果的 X、Y 座標、寬度、高度和文字方向
- 檢查偵測到的段落、行、單字和字元比較結果
我可以從OCR結果中提取哪些數據?
結果值不僅包含提取的文本,還包含IronOCR在 PDF 和圖像文件中發現的頁面、段落、行、單字、字元和條碼資訊。 您可以使用 Read 方法從傳回的OcrResult 物件存取此資訊。
IronOCR 的綜合結果系統基於強大的Tesseract 5 引擎,為開發人員提供超越簡單文字辨識的結構化資料擷取功能。 無論是處理掃描文件、照片或螢幕截圖,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}")
如何從OCR辨識結果中取得文字內容?
OcrResult 物件以簡單、直觀的方式呈現提取的文本,允許開發人員直接使用它或將其整合到其他應用程式元件中。 層級結構反映了自然文件文字的組織方式,使得處理不同粒度層級的內容變得簡單易行。
對於需要多語言支援的應用, IronOCR可以無縫處理多語言文檔,在所有125 種支援的語言中保持相同的結構化結果格式。
以下程式碼範例透過循環列印文字來驗證結果。
:path=/static-assets/ocr/content-code-examples/how-to/read-results-output-text.cs
using IronOcr;
using System;
using static IronOcr.OcrResult;
// Instantiate IronTesseract
IronTesseract ocrTesseract = new IronTesseract();
// Add image
using var imageInput = new OcrImageInput("sampleText.png");
// Perform OCR
OcrResult ocrResult = ocrTesseract.Read(imageInput);
// Retrieve list of detected paragraphs
Paragraph[] paragraphs = ocrResult.Paragraphs;
// Loop through each paragraph in the array
Console.WriteLine("--- All Detected Paragraphs ---");
foreach (Paragraph paragraph in paragraphs)
{
// Print the text of the current paragraph
Console.WriteLine(paragraph.Text);
// Add a blank line for better separation (optional)
Console.WriteLine();
}
Imports IronOcr
Imports System
Imports IronOcr.OcrResult
' Instantiate IronTesseract
Dim ocrTesseract As New IronTesseract()
' Add image
Using imageInput As New OcrImageInput("sampleText.png")
' Perform OCR
Dim ocrResult As OcrResult = ocrTesseract.Read(imageInput)
' Retrieve list of detected paragraphs
Dim paragraphs As Paragraph() = ocrResult.Paragraphs
' Loop through each paragraph in the array
Console.WriteLine("--- All Detected Paragraphs ---")
For Each paragraph As Paragraph In paragraphs
' Print the text of the current paragraph
Console.WriteLine(paragraph.Text)
' Add a blank line for better separation (optional)
Console.WriteLine()
Next
End Using
輸出
控制台輸出顯示IronOCR逐行精確擷取段落文字。 該引擎能夠自動偵測段落邊界,因此非常適合處理包含多個文字區塊的複雜文件。
如何取得偵測到的文字的位置座標?
除了提取的文字外,OcrResult 還提供詳細的位置資料。 對於需要保持佈局保真度或從特定文件區域執行有針對性的文字提取的應用程式而言,這種空間資訊至關重要。 座標系採用從頁面左上角開始的標準像素測量值。
為了提高基於座標的操作的精確度,可以考慮使用OCR 區域定位來集中關注特定區域,或利用電腦視覺功能自動識別文字區域。
以下程式碼示範如何遍歷每個段落並將其座標(X 和 Y)列印到控制台。
:path=/static-assets/ocr/content-code-examples/how-to/read-results-output-text.cs
using IronOcr;
using System;
using static IronOcr.OcrResult;
// Instantiate IronTesseract
IronTesseract ocrTesseract = new IronTesseract();
// Add image
using var imageInput = new OcrImageInput("sampleText.png");
// Perform OCR
OcrResult ocrResult = ocrTesseract.Read(imageInput);
// Retrieve list of detected paragraphs
Paragraph[] paragraphs = ocrResult.Paragraphs;
// Loop through each paragraph in the array
Console.WriteLine("--- All Detected Paragraphs ---");
foreach (Paragraph paragraph in paragraphs)
{
// Print the text of the current paragraph
Console.WriteLine(paragraph.Text);
// Add a blank line for better separation (optional)
Console.WriteLine();
}
Imports IronOcr
Imports System
Imports IronOcr.OcrResult
' Instantiate IronTesseract
Dim ocrTesseract As New IronTesseract()
' Add image
Using imageInput As New OcrImageInput("sampleText.png")
' Perform OCR
Dim ocrResult As OcrResult = ocrTesseract.Read(imageInput)
' Retrieve list of detected paragraphs
Dim paragraphs As Paragraph() = ocrResult.Paragraphs
' Loop through each paragraph in the array
Console.WriteLine("--- All Detected Paragraphs ---")
For Each paragraph As Paragraph In paragraphs
' Print the text of the current paragraph
Console.WriteLine(paragraph.Text)
' Add a blank line for better separation (optional)
Console.WriteLine()
Next
End Using
輸出
輸出結果顯示了三組座標,分別對應於三個段落。這些座標可用於繪製邊界框、提取特定區域或維護文字元素之間的空間關係。
OCR結果中還有哪些特性?
除了文字和文字座標外, IronOCR還提供其他資訊。 對於每個文字元素(段落、行、單字和單字),都有以下資訊可供參考:
-文字:實際的文字字串。
- X :從頁面左邊緣到該位置的像素值。
- Y :從頁面頂部邊緣到頁面中心的距離(以像素為單位)。 -寬度:以像素為單位的寬度。 -高度:以像素為單位的高度。 -文字方向:文字的閱讀方向(從左到右或從上到下)。 -位置:一個矩形,以像素為單位顯示此文字在頁面上的位置。
這些屬性在實現以下功能時尤其有用:
- 文字高亮與註釋系統
- 自動表單欄位檢測
- 文檔轉換中的版面保留
- 用於資料擷取的空間文字分析
為了進行偵錯和視覺化,請使用高亮文字功能來直觀地驗證偵測到的區域的準確性。
段落、行、單字和字元之間有何異同?
IronOCR 的分層文字結構允許開發人員根據其特定用例在適當的細節層級上進行工作。 了解這些元素之間的差異有助於為您的應用程式選擇合適的粒度。
以下對比的是偵測到的段落、行、單字和字元。
段落 |
線 |
單字 |
特點 |
每個粒度等級都有不同的用途: -段落:最適合文件結構分析和批次文字擷取 -行:有助於保持閱讀順序和處理表格數據 -字詞:非常適合搜尋功能和文字分析 -字元:非常適合拼字檢查和精確的文字編輯應用程式
IronOCR能讀取條碼和二維碼嗎?
是的, IronOCR可以讀取條碼和二維碼。 雖然IronOCR的功能可能不如IronBarcode那麼強大,但它支援常見的條碼類型。若要啟用條碼偵測,請將Configuration.ReadBarCodes屬性設為true。 IronOCR 的整合功能使其成為處理包含文字和條碼的文件(例如發票、運輸標籤或產品目錄)的絕佳選擇。
此外,還可以從檢測到的條碼中提取有價值的信息,包括格式、值、坐標(x,y)、高度、寬度和位置,作為 IronSoftware.Drawing.Rectangle 物件。 IronDrawing中的Rectangle類別允許在文件上進行精確定位。
對於更高級的條碼讀取場景,請查看我們文件中的綜合條碼讀取範例。
: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
條碼檢測輸出是什麼樣的?
IronOCR中的條碼偵測功能與文字擷取功能無縫集成,提供包含文字內容和條碼資料的統一結果。 這種雙重功能對於需要提取和關聯兩種資訊類型的自動化文件處理工作流程來說非常有價值。
輸出結果展示了 IronOCR 能夠同時偵測多種條碼類型,提供格式標識(例如 QRCode 或 EAN8)、解碼值以及每個偵測到的程式碼的精確座標資訊。 這些全面的數據使開發人員能夠建立複雜的文件處理應用程序,從而有效率地處理混合內容類型。
常見問題解答
OcrResult 物件包含哪些資訊?
IronOCR 的 OcrResult 物件包含擷取的文字以及詳細的元資料,包括精確的 X/Y 座標、尺寸 (寬度與高度)、文字方向 (從左至右或從上至下),以及以段落、行、字詞和每個偵測元素的個別字元組織的層級結構。
如何從 OCR 結果中快速擷取第一個字?
您可以使用 IronOCR 的 Read 方法並存取 Words 集合來擷取第一個字的文字: `string wordText = new IronTesseract().Read("file.jpg").Words[0].Text;`.這可立即存取 OCR 結果中的個別字元。
OCR 結果中有哪些類型的座標資料?
IronOCR 為每個偵測到的元素 (段落、行、字和字元) 提供精確的 X 和 Y 座標,以及寬度和高度尺寸。此坐標資料可透過 CropRectangle 物件存取,以實現文字元素的精確位置追蹤。
除了文字內容之外,我還能擷取元資料嗎?
是的,IronOCR 可以擷取全面的元資料,包括 PDF 和影像文件中發現的頁面、段落、行、字、符號,甚至是條碼。OcrResult 物件可存取每個元素的文字方向、層次結構和空間資訊。
哪些文件類型可以處理 OCR 結果?
IronOCR 可以處理各種文件類型,包括掃描文件、照片、螢幕截圖、PDF 和影像檔案。讀取方法在這些格式中運作一致,返回相同結構的 OcrResult 物件,並包含完整的元資料。
擷取的文字在結果中如何組織?
IronOCR 以反映自然文件組織的層次結構來組織擷取的文字。OcrResult 物件會以不同的粒度層級呈現內容 - 從整頁到個別字元 - 讓您可以輕鬆地在適當的層級處理文字,以符合您的應用程式。

