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

如何在 C# 中修復條碼方向

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

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

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

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

校正方向非常簡單:只需一行程式碼,使用 IronBarcode 的 AutoRotate 選項(預設為啟用),即使影像旋轉,也能準確讀取條碼。

  1. 使用NuGet套件管理器安裝https://www.nuget.org/packages/BarCode

    PM > Install-Package BarCode
  2. 複製並運行這段程式碼。

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

    今天就在您的專案中開始使用免費試用IronBarcode

    arrow pointer

如何解決應用程式中條碼方向的問題?

若要套用自動方向校正,請將AutoRotate屬性設為 true。 此屬性預設為 true,因此您無需進行任何操作。 讀取任何非零方向的條碼影像都應該可以正常工作。

自動旋轉功能在處理各種條碼格式(包括二維碼、資料矩陣和傳統線性條碼)時特別有用。 無論您是從影像中讀取條碼還是從 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

我需要編寫哪些程式碼來實現自動旋轉功能?

:path=/static-assets/barcode/content-code-examples/how-to/image-orientation-correct-autorotate.cs
using IronBarCode;
using System;

BarcodeReaderOptions myOptionsExample = new BarcodeReaderOptions()
{
    // Turn on auto rotation in ML detection
    AutoRotate = true,
};

var results = BarcodeReader.Read("rotate20.png", myOptionsExample);

// Print out the value
Console.WriteLine(results[0].Value);
$vbLabelText   $csharpLabel

自動旋轉功能利用先進的機器學習演算法自動偵測條碼方向。 當處理單一影像中的多個條碼或處理方向各異的大量影像時,這一點尤其有價值。

使用不同的旋轉角度

IronBarcode 的方向校正功能可以無縫處理各種旋轉角度。 以下範例示範如何從不同旋轉角度讀取條碼:

using IronBarCode;
using System;
using System.Collections.Generic;

// Process multiple rotated images
var rotatedImages = new List<string> { "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}");
    }
}
using IronBarCode;
using System;
using System.Collections.Generic;

// Process multiple rotated images
var rotatedImages = new List<string> { "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}");
    }
}
$vbLabelText   $csharpLabel

性能考量

雖然 AutoRotate 預設啟用,但了解其效能影響有助於最佳化條碼讀取工作流程。 此功能可與 IronBarcode 的讀取速度選項有效搭配使用,讓您可以根據應用程式的需求平衡準確性和效能。

對於需要高速處理的應用,您可以將 AutoRotate 與其他最佳化技術結合使用:

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)
};
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)
};
$vbLabelText   $csharpLabel

與影像校正功能集成

AutoRotate 與 IronBarcode 的影像校正濾鏡完美相容。 處理品質差且旋轉過的影像時,您可以套用多種校正方法:

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);
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);
$vbLabelText   $csharpLabel

定向矯正的最佳實踐

1.預設行為:由於 AutoRotate 預設啟用,因此通常不需要明確設置,除非您之前已停用它或想要確保它處於活動狀態。

2.與作物區域結合:當使用作物區域來提高性能時,確保作物區域足夠大,可以容納旋轉的條碼。

3.多執行緒處理:AutoRotate 是執行緒安全的,並且能夠很好地處理非同步和多執行緒操作,因此適用於大批量條碼處理應用程式。

4.格式特定考量:雖然 AutoRotate 適用於所有支援的條碼格式,但某些格式(如 PDF417 和 Data Matrix)可能需要額外的格式特定選項。

在許多情況下,校正旋轉可能不夠,需要使用濾鏡。 請閱讀以下文章"如何使用影像校正濾鏡"來學習如何使用影像濾鏡。

常見問題解答

如何在我的 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
技術作家

Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。

除了開發之外,Curtis 對物聯網 (IoT) 有著濃厚的興趣,探索將硬體和軟體結合的創新方式。在閒暇時間,他喜愛遊戲並構建 Discord 機器人,結合科技與創意的樂趣。

準備好開始了嗎?
Nuget 下載 2,121,847 | 版本: 2026.3 剛剛發布
Still Scrolling Icon

還在捲動嗎?

想要快速證明? PM > Install-Package BarCode
執行範例 看您的字串變成 BarCode。