如何修正C#中的條碼方向
IronBarcode自動校正條碼的方向,使用其內建的AutoRotate功能,該功能可以在不同角度檢測和讀取條碼,而不需要手動旋轉圖片,確保即使從傾斜或旋轉的圖片中也能準確讀取條碼。
條碼方向是指條碼在產品或文件上列印或顯示的角度,可依不同的版面配置和設計需求加以調整。最常見的是水平方向,也就是條碼由左至右排列,這也是標準且最廣泛使用的格式。任何非零的旋轉角度都會增加程式庫偵測條碼及擷取其值的難度。IronBarcode 提供自動方向校正功能,可偵測具有任何非零旋轉角度的條碼和 QR Code。
快速入門:用一行程式碼自動旋轉並校正影像以下是一行使用IronBarcode的AutoRotate選項的程式碼,這選項預設為啟用狀態,即使圖片旋轉也能準確讀取條碼。
-
1使用 IronBarcode 安裝 NuGet Package Manager
-
2複製並運行這段程式碼。
var result = IronBarCode.BarcodeReader.Read("rotatedImage.png", new IronBarCode.BarcodeReaderOptions { AutoRotate = true });Dim result = IronBarCode.BarcodeReader.Read("rotatedImage.png", New IronBarCode.BarcodeReaderOptions With {.AutoRotate = True})Dim result = IronBarCode.BarcodeReader.Read("rotatedImage.png", New IronBarCode.BarcodeReaderOptions With {.AutoRotate = True}) -
3部署到您的生產環境進行測試
立即在您的項目中開始使用 IronBarcode,並使用 免費試用
最小工作流程(5 個步驟)
- 下載C#程式庫以修正條碼方向
- 將
AutoRotate屬性設定為 true - 匯入目標條碼和 QR Code
- 使用讀取選項讀取條碼和 QR Code
- 檢索生成的條碼值
如何在我的應用程式中修正條碼方向?
若要套用自動方向校正,請將 BarcodeReaderOptions 中的 AutoRotate 屬性設定為 true。此屬性預設為 true,因此通常無須額外設定,即可直接讀取具有非零旋轉角度的條碼影像。
AutoRotate這項功能對於處理各種條碼格式特別有用,包括QR Code、Data Matrix和傳統線性條碼。 無論您是從圖片中讀取條碼還是從PDF文件中掃描,方向校正都能確保可靠的結果。
讓我們以下圖為例。 下載以下20°旋轉和45°旋轉的樣本圖片。

旋轉20°

45°旋轉
實現自動旋轉需要什麼程式碼?
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);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)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這項功能利用先進的機器學習演算法自動檢測條碼方向。 這在處理單個圖像中多個條碼或批次處理多種方向的圖像時特別有用。
處理不同旋轉角度
IronBarcode的方向校正能處理各種旋轉角度。 以下是一個示範以不同旋轉角度讀取 BarCode 的範例:
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.Count > 0)
{
Console.WriteLine($"Image: {imagePath} - Barcode Value: {results[0].Value}");
Console.WriteLine($"Barcode Type: {results[0].BarcodeType}");
Console.WriteLine($"Barcode Rotation: {results[0].Rotation}");
}
}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.Count > 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 = New List(Of String) From {"rotate20.png", "rotate45.png", "rotate90.png"}
Dim options = 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.Count > 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
NextImports IronBarCode
Imports System
Imports System.Collections.Generic
' Process multiple rotated images
Dim rotatedImages = New List(Of String) From {"rotate20.png", "rotate45.png", "rotate90.png"}
Dim options = 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.Count > 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效能考量
雖然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)
};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 System.Drawing.Rectangle(100, 100, 300, 300)
}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 System.Drawing.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)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預設為啟用,因此通常不必明確設定,除非您先前曾停用此功能,或希望確認其已啟用。 - 搭配裁切區域: 使用裁切區域提升效能時,請確保區域夠大,能完整容納旋轉後的條碼。
- 多執行緒處理:
AutoRotate是執行緒安全的,並且很好地支援非同步和多執行緒操作,使其適合於高容量條碼處理應用。 - 格式特定考量: 雖然
AutoRotate適用於所有支援的條碼格式,但某些格式如PDF417和Data Matrix可能會受益於額外的格式特定選項。
在許多情況下,校正旋轉可能不夠,需要使用濾鏡。 學習如何在以下文章中使用圖片濾鏡:"如何使用圖片校正濾鏡。"
常見問題
IronBarcode 的 AutoRotate 功能是什麼?
IronBarcode 的 AutoRotate 功能會自動校正條碼的方向,允許在任何角度下正確讀取條碼,而不需要手動旋轉圖像。這確保了即使在傾斜或旋轉的影像中,圖像也能被正確處理。
我如何在我的 C# 應用程式中實現 AutoRotate 功能?
要實現 AutoRotate 功能,只需將 BarcodeReaderOptions 中的 AutoRotate 屬性設為 true。預設情況下,此屬性是啟用的,因此通常不需要額外步驟。
IronBarcode 能處理不同的條碼旋轉角度嗎?
是的,IronBarcode 可以處理各種旋轉角度。它會自動偵測並校正條碼和 QR 碼的方向,即使是旋轉的圖像,也能確保可靠的結果。
如何使用 IronBarcode 在單一圖片中讀取多個條碼?
IronBarcode 可以透過在 BarcodeReaderOptions 中設置 ExpectMultipleBarcodes 選項來讀取單一圖片中的多個條碼。這與 AutoRotate 及其他讀取優化搭配良好,提供高效的批量圖像處理。
AutoRotate 會影響 IronBarcode 條碼讀取的效能嗎?
AutoRotate 功能設計為能有效與 IronBarcode 的讀取速度選項搭配。您可以根據需求結合 AutoRotate 與最佳化技術來平衡準確性與效能,例如設置預期的條碼類型或定義裁剪區域。
IronBarcode 的 AutoRotate 功能是否是安全的?
是的,AutoRotate 功能是線程安全的,並且與非同步及多線程操作兼容,適合高容量條碼處理應用。
使用裁剪區域與 AutoRotate 功能時,應考量什麼?
當使用裁剪區域與 AutoRotate 時,確保裁剪區域足夠大以容納旋轉的條碼。這有助於在不影響條碼偵測準確性的情況下提高效能。
IronBarcode 能從品質不佳的圖像中改善條碼讀取嗎?
是的,IronBarcode 提供影像校正功能,例如自適應閾值、亮度和對比度濾波器,這些功能可與 AutoRotate 搭配使用,以改善品質不佳和旋轉圖像的讀取。
IronBarcode 如何在 AutoRotate 功能下處理不同的條碼格式?
IronBarcode 的 AutoRotate 功能支援各種條碼格式,包括 QR 碼、數據矩陣和傳統的線性條碼。對於某些格式,如 PDF417,額外的格式特定選項可能會增強可讀性。
使用 IronBarcode 是否需要手動啟用 AutoRotate 功能?
通常不需要手動啟用 AutoRotate 功能,因為預設是啟用的。您只需要在先前禁用了這個功能或想確保其啟用時才需明確設定。
Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。