如何在C#中修正條碼方向 | IronBarcode

如何在 C# 中修復 BarCode 方向。

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

IronBarcode 可使用其內建的 AutoRotate 功能自動糾正條碼方向,無需手動旋轉影像即可偵測並讀取任何角度的條碼,即使是傾斜或旋轉的影像也能確保精確的條碼讀取。

條碼方向是指條碼在產品或文件上列印或顯示的角度。 它可以調節到各種角度,以適應不同的佈局和設計要求。 最常見的方向是水平方向,條碼從左到右排列,這是標準和最廣泛使用的格式。 任何非零取向度都會為庫檢測和檢索該值帶來挑戰。 IronBarcode 提供自動方向校正功能,可偵測條碼和二維碼的任何非零方向。

<!--說明:顯示各種 BarCode 方向角度 (0°、20°、45°、90°) 以及 AutoRotate 如何處理各種情況的圖表 -->

快速入門:一行完成自動旋轉影像校正

以下是如何輕鬆糾正方向:使用 IronBarcode 的 AutoRotate 選項(預設啟用)的一行代碼,即使圖像旋轉,也能準確讀取條碼。

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

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

    PM > Install-Package BarCode

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

    var result = IronBarCode.BarcodeReader.Read("rotatedImage.png", new IronBarCode.BarcodeReaderOptions { AutoRotate = true });
  3. 部署到您的生產環境進行測試

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

如何在我的應用程式中修復 BarCode 方向? 若要套用自動方向修正,請將 `BarcodeReaderOptions` 中的 **`AutoRotate`** 屬性設定為 true。 此屬性預設為 true,因此您無需進行任何操作。 讀取任何非零方向的條碼影像都應該可以正常工作。 AutoRotate 功能在處理 [ 多種條碼格式](https://ironsoftware.com/csharp/barcode/get-started/supported-barcode-formats/)(包括 QR code、Data Matrix 和傳統的線性條碼)時尤其有用。 無論您是[從影像讀取 BarCode](https://ironsoftware.com/csharp/barcode/how-to/read-barcodes-from-images/),或是[從 PDF 文件掃描](https://ironsoftware.com/csharp/barcode/how-to/read-barcodes-from-pdf/),方向修正都能確保可靠的結果。 讓我們以下圖為例。 Download the following 20° rotation and 45° rotation sample images.
Barcode rotated 20 degrees clockwise showing vertical stripes at an angle for orientation testing
Barcode rotated 45 degrees showing diagonal orientation requiring correction

實施 AutoRotate 需要哪些程式碼? ```csharp :path=/static-assets/barcode/content-code-examples/how-to/image-orientation-correct-autorotate.cs ``` AutoRotate 功能利用先進的機器學習演算法自動偵測條碼方向。 這在處理單一影像中的 [ 多個 BarCode](https://ironsoftware.com/csharp/barcode/how-to/read-multiple-barcodes/) 或處理不同方向的影像批次時特別有價值。 ### 使用不同的旋轉角度工作 IronBarcode 的方向修正功能可無縫處理各種旋轉角度。 以下是一個示範以不同旋轉角度讀取 BarCode 的範例: ```csharp using IronBarCode; using System; using System.Collections.Generic; // Process multiple rotated images var rotatedImages = new List{ "rotate20.png", "rotate45.png", "rotate90.png" }; var options = new BarcodeReaderOptions { AutoRotate = true, // Combine with other reading optimizations Speed = ReadingSpeed.Balanced, ExpectMultipleBarcodes = false }; foreach (var imagePath in rotatedImages) { var results = BarcodeReader.Read(imagePath, options); if (results.Length > 0) { Console.WriteLine($"Image: {imagePath} - Barcode Value: {results[0].Value}"); Console.WriteLine($"Barcode Type: {results[0].BarcodeType}"); Console.WriteLine($"Rotation Applied: {results[0].WasRotated}"); } } ``` ### 效能考量 雖然 `AutoRotate` 是預設啟用的,但瞭解其對效能的影響有助於優化您的 BarCode 讀取工作流程。 該功能可與 IronBarcode 的讀取速度選項有效配合,讓您可以根據應用程式的需求,在精確度與效能之間取得平衡。 對於需要高速處理的應用程式,您可以結合 `AutoRotate` 與其他最佳化技術: ```csharp var fastReadOptions = new BarcodeReaderOptions { AutoRotate = true, Speed = ReadingSpeed.Faster, // Specify expected barcode types to improve performance ExpectBarcodeTypes = BarcodeEncoding.QRCode | BarcodeEncoding.Code128, // Define crop region if barcode location is predictable CropArea = new System.Drawing.Rectangle(100, 100, 300, 300) }; ``` ### 與影像校正功能整合 `AutoRotate` 可與 IronBarcode 的影像修正篩選器完美搭配。 在處理同時經過旋轉的劣質影像時,您可以套用多重修正: ```csharp var advancedOptions = new BarcodeReaderOptions { AutoRotate = true, // Apply additional image corrections ImageFilters = new ImageFilterCollection { new AdaptiveThresholdFilter(), new BrightnessFilter(1.2f), new ContrastFilter(1.5f) } }; var results = BarcodeReader.Read("low-quality-rotated-barcode.jpg", advancedOptions); ``` ### 方向修正的最佳實務 1.**預設行為**:由於`AutoRotate`是預設啟用的,您通常不需要明確地設定它,除非您之前已將它停用或想要確保它是啟用的。 2.**與裁切區域結合**:當使用裁切區域來改善效能時,請確保裁切區域的大小足以容納旋轉的 BarCode。 3.**多執行緒處理**:AutoRotate 是線程安全的,並能很好地與同步和多執行緒作業搭配使用,因此適用於大量的 BarCode 處理應用程式。 4.**特定格式的注意事項**:雖然 `AutoRotate` 可與所有支援的條碼格式搭配使用,但某些格式(如 PDF417 和 Data Matrix)可能會受益於額外的特定格式選項。 在許多情況下,校正旋轉可能不夠,需要使用濾鏡。 在以下文章中學習如何使用圖片濾鏡:"[如何使用影像修正濾鏡](https://ironsoftware.com/csharp/barcode/how-to/image-correction/)"。

常見問題解答

如何在我的 C# 應用程式中修復旋轉的 BarCode 影像?

IronBarcode 使用其内置的 AutoRotate 功能自动修复旋转的条码图像。只需在 BarcodeReaderOptions 中將 AutoRotate 設定為 true(預設為啟用),該函式庫便可偵測並讀取任何角度的條碼,而無需手動旋轉。

哪些 BarCode 方向可以自動修正?

IronBarcode 的 AutoRotate 功能可以檢測和修正任何非零度的方向,包括 20°、45°、90°、180° 和 270°旋轉。該功能適用於各種條碼格式,包括 QR 條碼、數據矩陣和傳統線性條碼。

我需要編寫特殊的程式碼來處理傾斜的 BarCode 嗎?

不需要特殊的程式碼。IronBarcode 的 AutoRotate(自動旋轉)屬性預設為啟用,因此方向校正開箱即用。您只需要一行代碼: var result = IronBarcode.BarcodeReader.Read("rotatedImage.png");

方位校正可以處理 PDF 文件嗎?

是的,IronBarcode 的 AutoRotate 功能在從 PDF 文件以及圖像掃描條碼時能無縫工作。無論來源格式為何,方向修正功能都能確保可靠的結果。

自動方向檢測採用什麼技術?

IronBarcode 使用先進的機器學習演算法來自動偵測條碼方向。這種智慧型方法可確保即使從傾斜或旋轉的影像中也能準確讀取條碼,而無需人工干预。

柯蒂斯·週
技術撰稿人

Curtis Chau擁有卡爾頓大學電腦科學學士學位,專長於前端開發,精通Node.js、TypeScript、JavaScript和React。他熱衷於打造直覺美觀的使用者介面,喜歡使用現代框架,並擅長撰寫結構清晰、視覺效果出色的使用者手冊。

除了開發工作之外,柯蒂斯對物聯網 (IoT) 也抱有濃厚的興趣,致力於探索硬體和軟體整合的創新方法。閒暇時,他喜歡玩遊戲和製作 Discord 機器人,將他對科技的熱愛與創造力結合。

準備好開始了嗎?
Nuget 下載 2,035,202 | 版本: 2025.12 剛剛發布