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

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

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

IronBarcode自動使用其內建的AutoRotate功能修正條碼方向,該功能能夠偵測並讀取任何角度的條碼,無需手動旋轉圖像,確保即使在傾斜或旋轉的圖像中也能正確讀取條碼。

條碼方向指的是條碼在產品或文件上印刷或顯示的角度。 它可以調整為各種角度,以滿足不同的佈局和設計需求。 最常見的方向是水平,條碼從左到右對齊,這是標準且最廣泛使用的格式。 任何非零的方向角度對於程式庫來說都是一個偵測和檢索值的挑戰。 IronBarcode提供自動方向修正來偵測任何非零度的條碼和QR碼方向。

快速入門:一行程式碼自動旋轉圖像修正

以下介紹如何輕鬆修正方向:使用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

如何在我的應用程式中修正條碼方向?

若要應用自動方向修正,請將BarcodeReaderOptions中設為true。 此屬性預設為true,因此您不需要做任何事情。 讀取任何非零方向條碼圖像應該開箱即用。

AutoRotate功能在處理各種條碼格式(包括QR碼、Data Matrix和傳統線性條碼)時特別有用。 無論您是從圖像中讀取條碼還是從PDF文件中掃描條碼,方向修正都能確保可靠的結果。

讓我們以下面的圖像作為我們的樣本。 下載以下20°旋轉45°旋轉的樣本圖像。

順時針旋轉 20 度的條碼,顯示傾斜的垂直條紋,用於方向測試
Barcode rotated 45 degrees showing diagonal orientation requiring correction

我需要什麼程式碼來實施AutoRotate?

: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);
Imports IronBarCode
Imports System

Private myOptionsExample As New BarcodeReaderOptions() With {.AutoRotate = True}

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

' Print out the value
Console.WriteLine(results(0).Value)
$vbLabelText   $csharpLabel

AutoRotate功能利用先進的機器學習算法自動偵測條碼方向。 這在處理單個圖像中的多個條碼或處理具有不同方向的圖像批次時特別有價值。

處理不同的旋轉角度

IronBarcode的方向修正無縫處理不同的旋轉角度。 這是一個讀取不同旋轉角度條碼的範例:

:path=/static-assets/barcode/content-code-examples/how-to/image-orientation-correction-3.cs
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($"Barcode Rotation: {results[0].Rotation}");
    }
}
Imports IronBarCode
Imports System
Imports System.Collections.Generic

' Process multiple rotated images
Dim rotatedImages As New List(Of String) From {"rotate20.png", "rotate45.png", "rotate90.png"}
Dim options As New BarcodeReaderOptions With {
    .AutoRotate = True,
    ' Combine with other reading optimizations
    .Speed = ReadingSpeed.Balanced,
    .ExpectMultipleBarcodes = False
}

For Each imagePath In rotatedImages
    Dim results = BarcodeReader.Read(imagePath, options)
    If results.Length > 0 Then
        Console.WriteLine($"Image: {imagePath} - Barcode Value: {results(0).Value}")
        Console.WriteLine($"Barcode Type: {results(0).BarcodeType}")
        Console.WriteLine($"Barcode Rotation: {results(0).Rotation}")
    End If
Next
$vbLabelText   $csharpLabel

性能考量

雖然AutoRotate預設為啟用,但了解其性能影響有助於優化您的條碼讀取工作流程。 該功能與IronBarcode的讀取速度選項一起有效運作,讓您根據應用程式的需求在準確性和性能之間取得平衡。

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

:path=/static-assets/barcode/content-code-examples/how-to/image-orientation-correction-4.cs
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)
};
Imports System.Drawing

Dim fastReadOptions = New BarcodeReaderOptions With {
    .AutoRotate = True,
    .Speed = ReadingSpeed.Faster,
    ' Specify expected barcode types to improve performance
    .ExpectBarcodeTypes = BarcodeEncoding.QRCode Or BarcodeEncoding.Code128,
    ' Define crop region if barcode location is predictable
    .CropArea = New Rectangle(100, 100, 300, 300)
}
$vbLabelText   $csharpLabel

與圖像修正功能整合

AutoRotate與IronBarcode的圖像修正過濾器無縫結合。 當處理品質不佳且同時旋轉的圖像時,您可以應用多重修正:

:path=/static-assets/barcode/content-code-examples/how-to/image-orientation-correction-5.cs
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);
Imports System

Dim advancedOptions As New BarcodeReaderOptions With {
    .AutoRotate = True,
    ' Apply additional image corrections
    .ImageFilters = New ImageFilterCollection From {
        New AdaptiveThresholdFilter(),
        New BrightnessFilter(1.2F),
        New ContrastFilter(1.5F)
    }
}

Dim results = BarcodeReader.Read("low-quality-rotated-barcode.jpg", advancedOptions)
$vbLabelText   $csharpLabel

方向修正的最佳實踐

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

  2. 結合裁剪區域:在使用裁剪區域提高性能的同時,確保裁剪區域足夠大以容納旋轉的條碼。

  3. 多執行緒處理AutoRotate是執行緒安全的,並且可以與異步和多執行緒操作良好工作,使其適合高容量條碼處理應用程式。

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

在許多情況下,僅僅修正旋轉可能不夠,需要使用過濾器。 在以下文章中學習如何使用圖像過濾器:"如何使用圖像修正過濾器"。

常見問題

如何在我的C#應用程式中修正旋轉的條碼圖像?

IronBarcode自動修正旋轉的條碼圖像,利用其內建的AutoRotate功能。只需在BarcodeReaderOptions中設置AutoRotate為true(預設啟用),程式庫將無需手動旋轉,即可在任何角度檢測和讀取條碼。

可以自動修正哪些條碼方向?

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

我需要寫特別的程式碼來處理傾斜的條碼嗎?

不需要寫特別的程式碼。IronBarcode的AutoRotate屬性預設啟用,因此方向修正開箱即用。您只需一行程式碼:var result = IronBarCode.BarcodeReader.Read("rotatedImage.png");

方向修正可以應用於PDF文件嗎?

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

什麼技術支持自動方位檢測?

IronBarcode使用先進的機器學習算法來自動檢測條碼方向。這種智能方法無需手動干預,即可確保從傾斜或旋轉的圖像中準確讀取條碼。

使用IronBarcode進行條碼操作有什麼好處?

IronBarcode提供了如易於整合、支持多種條碼格式、高品質圖像生成和強大讀取能力等好處,使其成為C#中條碼操作的全面工具。

IronBarcode是否提供自定義條碼外觀的支持?

是的,IronBarcode提供了廣泛的條碼外觀自定義選項,包括顏色、大小和文字註釋,讓您可以根據具體設計需求定制條碼。

IronBarcode如何幫助改善業務流程效率?

IronBarcode通過使條碼生成和讀取快速且準確來提高業務流程效率,減少手動資料輸入錯誤,並改善庫存和資產追蹤。

將IronBarcode實現於專案中需要什麼程式設計技能?

基本的C#程式設計知識足以將IronBarcode實現於專案中,因為它提供了簡單的方法和全面的文件來指導開發者。

IronBarcode適合於小型專案和大型企業應用嗎?

IronBarcode設計為可擴展且多功能,使其適合小型專案和需要強大條碼解決方案的大型企業應用。

Curtis Chau
技術作家

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

除了開發,Curtis對物聯網(IoT)有濃厚的興趣,探索創新的方法來整合硬體和軟體。在空閒時間,他喜歡玩遊戲和建立Discord機器人,結合他對技術的熱愛與創造力。

準備好開始了嗎?
Nuget 下載 2,317,217 | 版本: 2026.7 剛剛發布
Still Scrolling Icon

還在滾動嗎?

想快速驗證嗎? PM > Install-Package BarCode
運行範例觀看您的字串成為條碼。