如何在 C# 中修正 BarCode 方向
IronBarcode 透過內建的 AutoRotate 功能自動校正 BarCode 方向,該功能能偵測並讀取任何角度的 BarCode,無需手動旋轉影像,確保即使面對傾斜或旋轉的影像,仍能準確讀取 BarCode。
BarCode方向是指BarCode在產品或文件上列印或顯示時的角度。 可依不同角度調整,以符合各種版面配置與設計需求。 最常見的排列方向為水平排列,即BARCODE由左至右對齊,此為標準且最廣泛使用的格式。 任何非零的方位角度都會對函式庫的偵測與取值功能構成挑戰。 IronBarcode 提供自動方向校正功能,可偵測 BARCODE 與 QR 碼的任何非標準方向。
快速入門:一行代碼完成圖片自動旋轉修正
修正方向有多簡單?只需一行代碼,利用 IronBarcode 的 AutoRotate 選項(預設啟用),即使圖像旋轉,也能精確讀取 BARCODE。
簡化工作流程(5 個步驟)
- 下載 C# 函式庫以修正 BarCode 方向
- 將
AutoRotate屬性設定為true - 匯入目標 BarCode 與 QR 碼
- 使用讀取選項讀取 BarCode 和 QR 碼
- 擷取生成的BarCode值
如何在我的應用程式中修正BarCode方向?
若要套用自動方向修正功能,請將 BarcodeReaderOptions 中的 AutoRotate 屬性設定為 true。 此屬性預設設為 true,因此您無需進行任何操作。 讀取任何非零向的BarCode圖像應能立即運作。
AutoRotate 功能在處理各種 BARCODE 格式時特別實用,包括 QR 碼、Data Matrix 以及傳統的線性 BARCODE。 無論是從圖像中讀取 BarCode,還是從 PDF 文件中掃描,方向校正功能都能確保結果的可靠性。
讓我們以下方圖片作為範例。 Download the following 20° rotation and 45° rotation sample images.
20° 旋轉
45° 旋轉
要實作自動旋轉功能,我需要哪些程式碼?
: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)
AutoRotate 功能運用先進的機器學習演算法,可自動偵測 BARCODE 的方向。 這在處理單一影像中的多個BarCode,或處理方向各異的批次影像時,尤為實用。
處理不同的旋轉角度
IronBarcode 的方向校正功能可無縫處理各種旋轉角度。 以下範例展示如何讀取不同旋轉角度的BARCODE:
using IronBarCode;
using System;
using System.Co/llections.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.Co/llections.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}");
}
}
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($"Rotation Applied: {results(0).WasRotated}")
End If
Next
效能考量
雖然 AutoRotate 預設為啟用狀態,但了解其對效能的影響有助於優化您的 BARCODE 讀取工作流程。 此功能可與 IronBarcode 的讀取速度選項高效配合,讓您能根據應用程式的需求,在準確度與效能之間取得平衡。
對於需要高速處理的應用程式,您可以將 AutoRotate 與其他優化技術結合使用:
var fastReadOptions = new BarcodeReaderOptions
{
AutoRotate = true,
Speed = ReadingSpeed.Faster,
// Specify expected barcode types to improve performance
ExpectBarcodeTypes = BarcodeEncoding.QRCode | BarcodeEncoding.Co/de128,
// 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.Co/de128,
// Define crop region if barcode location is predictable
CropArea = new System.Drawing.Rectangle(100, 100, 300, 300)
};
Imports System.Drawing
Dim fastReadOptions As 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)
}
與影像修正功能的整合
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);
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)
方向修正的最佳實踐
-
預設行為:由於
AutoRotate預設為啟用狀態,除非您先前已將其停用,或希望確保其處於有效狀態,否則通常無需明確設定。 -
與裁切區域結合使用:若使用裁切區域來提升效能,請確保裁切區域足夠大,足以容納旋轉後的BARCODE。
-
多執行緒處理:
AutoRotate具備執行緒安全性,並能與非同步及多執行緒操作完美配合,使其非常適合用於大量 BarCode 處理的應用。 - 格式相關考量:雖然
AutoRotate適用於所有受支援的 BARCODE 格式,但某些格式(如 PDF417 和 Data Matrix)可能需要額外的格式特定選項。
在許多情況下,僅修正旋轉角度可能不足,還需要套用濾鏡。 請參閱以下文章,了解如何使用影像濾鏡:"如何使用影像修正濾鏡"。
常見問題
如何在我的C#應用程式中修復旋轉的條碼圖像?
IronBarcode使用內建的AutoRotate功能自動修復旋轉的條碼圖像。只需在BarcodeReaderOptions中將AutoRotate設為true(預設已啟用),程式庫將在任何角度下自動檢測和讀取條碼,無需手動旋轉。
哪些條碼方向可以自動修正?
IronBarcode的AutoRotate功能可以檢測並修正任何非零角度的方向,包括20°、45°、90°、180°和270°旋轉。此功能支持包括QR Code、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設計為可擴展且多功能,使其適合小型專案和需要強大條碼解決方案的大型企業應用。

