如何使用 C# 的影像校正濾鏡來處理條碼

使用 C# 影像校正濾鏡來提升 BarCode 解碼效果

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

IronBarcode 提供內建的影像校正濾鏡,例如 SharpenFilterContrastFilter,可透過程式化方式增強模糊或不完美的 BarCode 影像,無需外部影像編輯軟體或重新擷取影像,即可提升讀取準確度。

並非每張圖像都完美無缺,而圖像品質不佳正是導致 IronBarcode 無法成功讀取 BarCode 的主要因素之一。 IronBarcode 無需重新擷取影像或使用外部影像增強軟體,而是提供內建濾鏡,可透過程式化方式提升影像品質。 這些篩選器有助於 IronBarcode 讀取複雜的圖像,並提升整體準確度。

繼續閱讀以了解 IronBarcode 中可用的影像修正濾鏡、它們對影像的影響,以及如何套用這些濾鏡。 如需更全面的BarCode讀取技術,請參閱我們的《BarCode讀取教學》

快速入門:套用銳化與對比濾鏡以提升BarCode讀取效果

只需一個步驟,即可透過 ImageFilterCollectionBarcodeReaderOptions 中套用 IronBarcode 的 SharpenFilterContrastFilter。 這項功能能提升BARCODE掃描效能,且設定步驟極簡,完全無需使用外部工具。

  1. using NuGet 套件管理員安裝 https://www.nuget.org/packages/BarCode

    PM > Install-Package BarCode
  2. 請複製並執行此程式碼片段。

    BarcodeResults results = IronBarCode.BarcodeReader.Read("input.png", new IronBarCode.BarcodeReaderOptions { ImageFilters = new IronBarCode.ImageFilterCollection() { new IronBarCode.SharpenFilter(3.5f), new IronBarCode.ContrastFilter(2.0f) } });
  3. 部署至您的生產環境進行測試

    立即透過免費試用,在您的專案中開始使用 IronBarcode

    arrow pointer

如何套用影像濾鏡來提升BarCode讀取效果?

若要套用篩選器,請實例化 ImageFilterCollection 類別,並分別建立各篩選器的實例。 接著將該物件指派給 ImageFilters 物件的 BarcodeReaderOptions 屬性。 請將選項物件連同範例圖片一併傳入 Read 方法中。 如需進階安裝選項,請參閱我們的 NuGet 套件指南

請使用下圖作為範例圖片。

Blurred barcode with number 4900203187590 showing poor image quality before filtering enhancement

這張圖片看起來相當模糊。 不過,亮度尚可,且黑白兩色仍可辨識。 因此,請至少套用 SharpenFilterContrastFilter 來提升 BARCODE 可讀性。 請參閱以下程式碼片段,了解如何對圖片套用濾鏡、讀取影像,並在主控台顯示結果。

:path=/static-assets/barcode/content-code-examples/how-to/image-correction-apply-filter.cs
using IronBarCode;
using System;

BarcodeReaderOptions options = new BarcodeReaderOptions()
{
    // Choose which filters are to be applied (in order)
    ImageFilters = new ImageFilterCollection()
    {
        new SharpenFilter(3.5f),
        new ContrastFilter(2)
    },
};

// Apply options and read the barcode
BarcodeResults results = BarcodeReader.Read("sample.png", options);

// Write the result value to console
foreach (BarcodeResult result in results)
{
    Console.WriteLine(result.Text);
}
Imports IronBarCode
Imports System

Private options As New BarcodeReaderOptions() With {
	.ImageFilters = New ImageFilterCollection() From {
		New SharpenFilter(3.5F),
		New ContrastFilter(2)
	}
}

' Apply options and read the barcode
Private results As BarcodeResults = BarcodeReader.Read("sample.png", options)

' Write the result value to console
For Each result As BarcodeResult In results
	Console.WriteLine(result.Text)
Next result
$vbLabelText   $csharpLabel

上述程式碼片段會套用濾鏡、讀取BARCODE,並將經過濾鏡處理的影像匯出至磁碟。 以下為樣本圖像與過濾後圖像的對比。

Blurry barcode image with number 4902030187590 demonstrating poor image quality
Barcode with improved readability after applying image filters, showing clear vertical lines and number 4902030187590

IronBarcode 提供哪些影像校正濾鏡?

IronBarcode 提供多種專為影像校正設計的影像濾鏡。 這些濾鏡有助於讀取不完美的BarCode影像,並提升讀取準確度。 然而,請理解這些篩選器的運作原理,以便選擇合適的篩選器,並避免因使用過多篩選器或選用錯誤的篩選器而導致效能問題。 可用的篩選條件包括:

  • AdaptiveThresholdFilter
  • BinaryThresholdFilter
  • BrightnessFilter
  • ContrastFilter
  • InvertFilter
  • SharpenFilter
  • ErodeFilter
  • DilateFilter
  • HistogramEqualizationFilter
  • 模糊濾鏡
    • GaussianBlurFilter
    • BilateralFilter
    • MedianBlurFilter

篩選器的應用順序取決於其在 ImageFilterCollection 內的排列位置。 如需這些篩選器的詳細 API 文件,請參閱我們的 API 參考指南

自適應閾值濾波器如何運作?

AdaptiveThresholdFilter 是 IronBarcode 中的一種濾鏡,它對影像應用 Bradley 自適應閾值技術,能自動決定將影像二值化的閾值。 此濾鏡非常適合用於光線不均勻且背景亮度層次多變的影像。

:path=/static-assets/barcode/content-code-examples/how-to/image-correction-adaptive-threshold.cs
using IronBarCode;

BarcodeReaderOptions options = new BarcodeReaderOptions()
{
    // Choose which filters are to be applied (in order)
    ImageFilters = new ImageFilterCollection(true) {
        new AdaptiveThresholdFilter(0.9f),
    },
};

// Apply options and read the barcode
BarcodeResults results = BarcodeReader.Read("sample.png", options);

// Export file to disk
results.ExportFilterImagesToDisk("adaptiveThreshold_0.9.png");
Imports IronBarCode

Private options As New BarcodeReaderOptions() With {
	.ImageFilters = New ImageFilterCollection(True) From {New AdaptiveThresholdFilter(0.9F)}
}

' Apply options and read the barcode
Private results As BarcodeResults = BarcodeReader.Read("sample.png", options)

' Export file to disk
results.ExportFilterImagesToDisk("adaptiveThreshold_0.9.png")
$vbLabelText   $csharpLabel

以下是套用不同數值過濾器後的輸出結果。

Vertical lines showing different adaptive threshold filter outputs with solid and dashed patterns
Low-quality barcode image showing UPC number 902030187590 with significant visual distortion

建構函式接受額外的參數以進行設定:

  • Upper:閾值處理的上界(白色)顏色。
  • Lower:閾值處理的較低(黑色)色階。
  • Threshold:二值化閾值範圍(0.0-1.0)。
  • Rectangle:應用處理器的矩形區域。

如上方的輸出圖所示,該圖像已進行二值化處理,僅保留黑白兩色。 雖然它似乎仍不適合用於BarCode讀取,但需要組合使用各種濾鏡。 請嘗試調整參數敏感度以獲得最佳結果。

二進位閾值濾波器如何運作?

BinaryThresholdFilter 透過在指定閾值處分割像素,並比較色彩成分的亮度,來對影像進行濾色處理。 與 AdaptiveThresholdFilter 類似,若未正確使用此篩選器,可能會引入新的或不必要的雜訊。 然而,IronBarcode 已為篩選器屬性設定了預設值。

AdaptiveThresholdFilter 類似,BinaryThresholdFilter 接受相同的額外參數進行配置:

  • Upper:閾值處理的上界(白色)顏色。
  • Lower:閾值處理的較低(黑色)色階。
  • Threshold:二值化閾值範圍(0.0-1.0)。
  • Rectangle:應用處理器的矩形區域。
:path=/static-assets/barcode/content-code-examples/how-to/image-correction-binary-threshold.cs
using IronBarCode;

BarcodeReaderOptions options = new BarcodeReaderOptions()
{
    // Choose which filters are to be applied (in order)
    ImageFilters = new ImageFilterCollection(true) {
        new BinaryThresholdFilter(0.9f)
    },
};

// Apply options and read the barcode
BarcodeResults results = BarcodeReader.Read("sample.png", options);

// Export file to disk
results.ExportFilterImagesToDisk("binaryThreshold_0.9.png");
Imports IronBarCode

Private options As New BarcodeReaderOptions() With {
	.ImageFilters = New ImageFilterCollection(True) From {New BinaryThresholdFilter(0.9F)}
}

' Apply options and read the barcode
Private results As BarcodeResults = BarcodeReader.Read("sample.png", options)

' Export file to disk
results.ExportFilterImagesToDisk("binaryThreshold_0.9.png")
$vbLabelText   $csharpLabel

以下是對範例圖片套用濾鏡後的樣本輸出。

Three examples of binary threshold filter outputs showing sparse, dotted, and dense vertical line patterns
Barcode image processed with 0.9 binary threshold filter showing black and white contrast

觀察上方的輸出圖像,該範例已二值化為黑白圖像。 然而,此濾鏡顯然不適用於這張圖片,因為BARCODE的條紋被消除,且產生了新的雜訊。 若需處理複雜的BarCode情境,請參閱我們的"無法辨識BarCode"疑難排解指南

如何調整影像亮度以提升BarCode讀取效果?

BrightnessFilter 是 IronBarcode 圖像濾鏡集合中的另一項重要濾鏡。 顧名思義,此濾鏡用於調整BarCode影像的亮度。 此建構函式的輸入參數會調整輸出影像的亮度。 預設值為 1,此設定將保持圖片不變。 數值 0 會產生完全黑色的圖像,而大於 1 的數值則會使圖像變亮。

:path=/static-assets/barcode/content-code-examples/how-to/image-correction-brightness.cs
using IronBarCode;

BarcodeReaderOptions options = new BarcodeReaderOptions()
{
    // Choose which filters are to be applied (in order)
    ImageFilters = new ImageFilterCollection(true) {
        new BrightnessFilter(1.5f),
    },
};

// Apply options and read the barcode
BarcodeResults results = BarcodeReader.Read("sample.png", options);

// Export file to disk
results.ExportFilterImagesToDisk("brightness_1.5.png");
Imports IronBarCode

Private options As New BarcodeReaderOptions() With {
	.ImageFilters = New ImageFilterCollection(True) From {New BrightnessFilter(1.5F)}
}

' Apply options and read the barcode
Private results As BarcodeResults = BarcodeReader.Read("sample.png", options)

' Export file to disk
results.ExportFilterImagesToDisk("brightness_1.5.png")
$vbLabelText   $csharpLabel

以下是將此濾鏡套用至範例輸入後的輸出圖像。

Blurry UPC barcode sample showing default brightness level before filter enhancement
Blurry barcode with product number 4902030187590, demonstrating low brightness or poor image quality

如何使用對比度濾鏡來增強BarCode影像?

ContrastFilter 用於調整影像的對比度。 圖像對比度是指圖像中各元素之間色彩強度的差異。 提高對比度可增強細節的清晰度,使影像顯得鮮明生動;反之,降低對比度則會使影像呈現更柔和、低調的視覺效果。 有關BarCode自訂的更多詳細資訊,請參閱我們的BarCode樣式自訂指南。

預設值為 1,此設定將保持圖片不變。 數值 0 會產生完全灰色的影像,而大於 1 的數值則會提高影像的對比度。

:path=/static-assets/barcode/content-code-examples/how-to/image-correction-contrast.cs
using IronBarCode;

BarcodeReaderOptions options = new BarcodeReaderOptions()
{
    // Choose which filters are to be applied (in order)
    ImageFilters = new ImageFilterCollection(true) {
        new ContrastFilter(1.5f),
    },
};

// Apply options and read the barcode
BarcodeResults results = BarcodeReader.Read("sample.png", options);

// Export file to disk
results.ExportFilterImagesToDisk("contrast_1.5.png");
Imports IronBarCode

Private options As New BarcodeReaderOptions() With {
	.ImageFilters = New ImageFilterCollection(True) From {New ContrastFilter(1.5F)}
}

' Apply options and read the barcode
Private results As BarcodeResults = BarcodeReader.Read("sample.png", options)

' Export file to disk
results.ExportFilterImagesToDisk("contrast_1.5.png")
$vbLabelText   $csharpLabel

將此篩選器套用至範例輸入後,會產生下圖所示的結果。

Blurry barcode with number 4902030187590 demonstrating default contrast filter settings
Blurry barcode with number 4902030187590 demonstrating low contrast image quality

何時該使用反轉濾鏡?

此濾鏡會將圖片內的顏色反轉,使顏色互換,例如白色變成黑色,黑色變成白色。當讀取具有背景色的BARCODE圖像時,此功能特別有用。 與 BinaryThresholdFilter 不同,此濾鏡會直接反轉顏色,無需指定敏感度。 此外,此濾鏡可與 CropRectangle 配合使用,以指定圖像中需要反轉顏色的位置,而非將整張圖像的顏色全部反轉。 請參閱我們的裁切區域教學,進一步了解如何指定裁切區域。

:path=/static-assets/barcode/content-code-examples/how-to/image-correction-invert.cs
using IronBarCode;

BarcodeReaderOptions options = new BarcodeReaderOptions()
{
    // Choose which filters are to be applied (in order)
    ImageFilters = new ImageFilterCollection(true) {
        new InvertFilter(),
    },
};

// Apply options and read the barcode
BarcodeResults results = BarcodeReader.Read("sample.png", options);

// Export file to disk
results.ExportFilterImagesToDisk("invert.png");
Imports IronBarCode

Private options As New BarcodeReaderOptions() With {
	.ImageFilters = New ImageFilterCollection(True) From {New InvertFilter()}
}

' Apply options and read the barcode
Private results As BarcodeResults = BarcodeReader.Read("sample.png", options)

' Export file to disk
results.ExportFilterImagesToDisk("invert.png")
$vbLabelText   $csharpLabel

下方的輸出圖像,是將此濾鏡套用至範例輸入圖像後的結果。

Blurry UPC barcode showing number 480203187590 - original image before invert filter application
Blurry inverted barcode showing white bars on dark background with number sequence 4902030187590

如何使用"銳化"濾鏡修正模糊的BarCode影像?

IronBarcode 提供銳化濾鏡。 此濾鏡可增強影像的銳利度,對於處理模糊的影像非常有用。 透過在建立濾鏡物件時調整 Sigma 值,即可利用此濾鏡來調整影像的銳利度。 預設值為 3。提高 sigma 值可增強影像銳利度。 如需其他效能優化選項,請參閱我們的閱讀速度選項指南。

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

BarcodeReaderOptions options = new BarcodeReaderOptions()
{
    // Choose which filters are to be applied (in order)
    ImageFilters = new ImageFilterCollection(true) {
        new SharpenFilter(0.5f),
    },
};

// Apply options and read the barcode
BarcodeResults results = BarcodeReader.Read("sample.png", options);

// Export file to disk
results.ExportFilterImagesToDisk("sharpen_0.5.png");
Imports IronBarCode
Imports System

Private options As New BarcodeReaderOptions() With {
	.ImageFilters = New ImageFilterCollection(True) From {New SharpenFilter(0.5F)}
}

' Apply options and read the barcode
Private results As BarcodeResults = BarcodeReader.Read("sample.png", options)

' Export file to disk
results.ExportFilterImagesToDisk("sharpen_0.5.png")
$vbLabelText   $csharpLabel

The image below is the sharpened version of the sample input image.

Blurry barcode image demonstrating unsharpened quality before applying sharpen filter
Blurred barcode example showing effects of image quality degradation

將上圖與原始圖像比較,可見其畫質更為清晰,有助於透過 IronBarcode 進行條碼讀取。 在大多數情況下,SharpenFilter 總是與 ImageFilterCollection 類中的其他篩選器一同使用。

Erode 濾鏡的用途為何?

ErodeFilter 可透過移除圖形邊緣附近的像素,來消除微小的白色噪點並加粗 BarCode 條紋。此濾鏡最適用於 BarCode 背景有大量白色斑點,或 BarCode 影像解析度過低、過於模糊,導致 BarCode 條紋重疊的情況。 ErodeFilter 可使條紋更粗,同時去除背景中的白色斑點。 有關處理不完整圖像的更多資訊,請參閱我們的"不完整BarCode範例"

透過在篩選器中輸入代表 kernelSize 的整數,可增強侵蝕效果。 核心尺寸越大,對輸入影像的影響就越強。 請注意,kernelSize 是一個方塊,在此範例中應為 5x5 核心。

舉例來說,請使用 ErodeFilter 並設定較大的核心尺寸,以展示濾波器的效果。

:path=/static-assets/barcode/content-code-examples/how-to/image-correction-erode.cs
using IronBarCode;

BarcodeReaderOptions options = new BarcodeReaderOptions()
{
    // Choose which filters are to be applied (in order)
    ImageFilters = new ImageFilterCollection(true) {
        new ErodeFilter(5),
    },
};

// Apply options and read the barcode
BarcodeResults results = BarcodeReader.Read("sample.png", options);

// Export file to disk
results.ExportFilterImagesToDisk("erodeFilter.jpg");
Imports IronBarCode

Dim options As New BarcodeReaderOptions() With {
    .ImageFilters = New ImageFilterCollection(True) From {
        New ErodeFilter(5)
    }
}

' Apply options and read the barcode
Dim results As BarcodeResults = BarcodeReader.Read("sample.png", options)

' Export file to disk
results.ExportFilterImagesToDisk("erodeFilter.jpg")
$vbLabelText   $csharpLabel
Blurry barcode showing number 4002030187590 - example for erode filter demonstration
Blurred barcode showing vertical black and white stripes with numerical code

比較上方的輸入與輸出圖像,由於輸入較大的核函數大小進行濾波,導致某些條紋明顯變粗。 然而,整體畫面中的白色斑點已減少。 基於侵蝕濾鏡的特性,若核尺寸過大且應用過於激進,可能會導致細長條狀物被抹除,如上圖所示。 透過變更輸入至 ErodeFilter 的核心大小值,來測試並調整效果。

擴張濾波器如何協助BarCode讀取?

DilateFilter 功能與 ErodeFilter 相反,其運作原理是透過在物件邊界添加像素,來擴展明亮區域(通常是背景)。 雖然此濾鏡可透過填補細微間隙或增強低對比度區域來修復受損或模糊的BARCODE,但請注意,其對BARCODE條紋的影響與直覺認知有所不同。 由於擴張會放大亮部區域,因此會間接使暗部元素(例如黑色BarCode條)變得更細(假設背景為白色)。 這使得該濾鏡在BarCode條紋過於粗寬或重疊的場景下特別有效,但過度使用可能會因條紋過度變窄而降低掃描準確度。

與上述類似,透過輸入代表 kernelSize 的整數作為篩選條件,可增強篩選效果。

針對下方的範例,請使用較大的核心大小,以展示 DilateFilter 的效果。

:path=/static-assets/barcode/content-code-examples/how-to/image-correction-dilate.cs
using IronBarCode;

BarcodeReaderOptions options = new BarcodeReaderOptions()
{
    // Choose which filters are to be applied (in order)
    ImageFilters = new ImageFilterCollection(true) {
        new DilateFilter(5),
    },
};

// Apply options and read the barcode
BarcodeResults results = BarcodeReader.Read("sample.png", options);

// Export file to disk
results.ExportFilterImagesToDisk("dilateFilter.jpg");
Imports IronBarCode

Dim options As New BarcodeReaderOptions() With {
    .ImageFilters = New ImageFilterCollection(True) From {
        New DilateFilter(5)
    }
}

' Apply options and read the barcode
Dim results As BarcodeResults = BarcodeReader.Read("sample.png", options)

' Export file to disk
results.ExportFilterImagesToDisk("dilateFilter.jpg")
$vbLabelText   $csharpLabel
Blurry barcode image showing number 4902030187590 - example for dilate filter processing
Blurred barcode with numerical sequence below vertical bars

如上圖所示,過度使用 DilateFilter 可能會破壞 BARCODE 結構,導致間距過近的 BARCODE 合併,並在 BARCODE 中產生靜區。 根據輸入圖片的特性,調整核大小(kernel size)的數值(增大或縮小),以測試並優化圖片的處理效果。

何時該使用 HistogramEqualization 濾鏡?

HistogramEqualizationFilter 透過重新分配像素亮度來增強影像對比度,從而提升清晰度。 此功能最常應用於BARCODE對比度較低的情況,例如褪色或模糊的圖像,或是光線不均勻的圖像,例如深色陰影或強光眩光。 透過分析影像直方圖(即像素亮度的分佈),它會透過拉伸亮度範圍來增強對比度,從而重新分配像素值,使暗部像素更暗,亮部像素更亮。

:path=/static-assets/barcode/content-code-examples/how-to/image-correction-histogram-equalization-filter.cs
using IronBarCode;

BarcodeReaderOptions options = new BarcodeReaderOptions()
{
    // Choose which filters are to be applied (in order)
    ImageFilters = new ImageFilterCollection(true) {
        new HistogramEqualizationFilter(),
    },
};

// Apply options and read the barcode
BarcodeResults results = BarcodeReader.Read("sample.png", options);

// Export file to disk
results.ExportFilterImagesToDisk("histogramEqualizationFilter.jpg");
Imports IronBarCode

Dim options As New BarcodeReaderOptions() With {
    .ImageFilters = New ImageFilterCollection(True) From {
        New HistogramEqualizationFilter()
    }
}

' Apply options and read the barcode
Dim results As BarcodeResults = BarcodeReader.Read("sample.png", options)

' Export file to disk
results.ExportFilterImagesToDisk("histogramEqualizationFilter.jpg")
$vbLabelText   $csharpLabel
Blurry barcode with number 4902030187590 used as test image for histogram equalization filter
Barcode with vertical black and white stripes showing number 4902030187590

如上圖所示,相較於原始圖像,黑色條紋明顯更深,而空白區域則明顯更亮。

哪些模糊濾鏡有助於減少BarCode雜訊?

高斯模糊濾鏡如何降低影像雜訊?

GaussianBlurFilter 會對影像套用高斯模糊效果。 此濾鏡通常用於降低影像中的雜訊。 如需處理不完整BarCode的完整指南,請參閱我們的影像方向校正教學

此濾鏡的運作原理是利用高斯函數,對影像中相鄰像素的值進行平均運算。 此方法依賴於兩個可調整的參數:

  • 核 (Kernel):用於計算像素平均值的矩陣。
  • Sigma:控制模糊強度的數值。

預設的 kernel 大小為 3x3 像素,預設的 Sigma 值為 3.0,會產生適度的模糊效果。 增加 Sigma 的數值會產生更強烈的模糊效果。 您亦可自訂 kernel 來控制模糊濾鏡進行平均運算的鄰域大小。

:path=/static-assets/barcode/content-code-examples/how-to/image-correction-gaussianblur.cs
using IronBarCode;

BarcodeReaderOptions myOptionsExample = new BarcodeReaderOptions()
{
    // Choose which filters are to be applied (in order)
    ImageFilters = new ImageFilterCollection(true) {
        new GaussianBlurFilter(3, 3, 3.0f),
    },
};

// Apply options and read the barcode
BarcodeResults results = BarcodeReader.Read("sharpen.webp", myOptionsExample);

// Export file to disk
results.ExportFilterImagesToDisk("gaussianBlur.png");
Imports IronBarCode

Private myOptionsExample As New BarcodeReaderOptions() With {
	.ImageFilters = New ImageFilterCollection(True) From {New GaussianBlurFilter(3, 3, 3.0F)}
}

' Apply options and read the barcode
Private results As BarcodeResults = BarcodeReader.Read("sharpen.webp", myOptionsExample)

' Export file to disk
results.ExportFilterImagesToDisk("gaussianBlur.png")
$vbLabelText   $csharpLabel

將此篩選器套用至範例輸入後,會產生下圖所示的結果。

Blurry barcode with number 4902030187590 demonstrating poor image quality
Barcode image with Gaussian blur filter applied, showing blurred vertical lines and distorted numbers

何時該使用雙向過濾器?

BilateralFilter 可在保留邊緣細節的同時,對影像進行平滑處理。 與僅對所有像素進行均勻模糊處理的簡單模糊技術不同,雙邊濾波器同時考量了色彩差異與像素距離,使其在保留邊緣的平滑處理方面格外有效。

此方法依賴於三個可調整的參數:

  • NeighborhoodDiameter:像素鄰域的直徑(預設值:5)。
  • SigmaColor:決定色彩差異影響程度的色彩影響係數(預設值:75.0)。
  • SigmaSpace:決定距離影響範圍的空間影響範圍(預設值:75.0)。
:path=/static-assets/barcode/content-code-examples/how-to/image-correction-bilateral.cs
using IronBarCode;

BarcodeReaderOptions myOptionsExample = new BarcodeReaderOptions()
{
    // Choose which filters are to be applied (in order)
    ImageFilters = new ImageFilterCollection(true) {
        new BilateralFilter(5, 75, 75),
    },
};

// Apply options and read the barcode
BarcodeResults results = BarcodeReader.Read("sharpen.webp", myOptionsExample);

// Export file to disk
results.ExportFilterImagesToDisk("bilateral.png");
Imports IronBarCode

Private myOptionsExample As New BarcodeReaderOptions() With {
	.ImageFilters = New ImageFilterCollection(True) From {New BilateralFilter(5, 75, 75)}
}

' Apply options and read the barcode
Private results As BarcodeResults = BarcodeReader.Read("sharpen.webp", myOptionsExample)

' Export file to disk
results.ExportFilterImagesToDisk("bilateral.png")
$vbLabelText   $csharpLabel

將此篩選器套用至範例輸入後,會產生下圖所示的結果。

Blurred barcode demonstrating poor image quality with vertical lines and numbers 4902030187590
Blurred barcode with numbers 4902030187590 demonstrating poor image quality

中值模糊濾波器在降噪方面有何獨特之處?

MedianBlurFilter 透過將每個像素的值替換為周邊像素的中位數,來降低影像中的雜訊。 此濾鏡特別擅長在去除雜訊的同時保留邊緣細節。若要進一步了解BarCode讀取設定,請參閱我們的BarCode讀取器設定指南

  • KernelSize:用於計算中位數的鄰域大小(必須為奇數,預設值:5)。
:path=/static-assets/barcode/content-code-examples/how-to/image-correction-medianblur.cs
using IronBarCode;

BarcodeReaderOptions myOptionsExample = new BarcodeReaderOptions()
{
    // Choose which filters are to be applied (in order)
    ImageFilters = new ImageFilterCollection(true) {
        new MedianBlurFilter(5),
    },
};

// Apply options and read the barcode
BarcodeResults results = BarcodeReader.Read("sharpen.webp", myOptionsExample);

// Export file to disk
results.ExportFilterImagesToDisk("medianBlur.png");
Imports IronBarCode

Private myOptionsExample As New BarcodeReaderOptions() With {
	.ImageFilters = New ImageFilterCollection(True) From {New MedianBlurFilter(5)}
}

' Apply options and read the barcode
Private results As BarcodeResults = BarcodeReader.Read("sharpen.webp", myOptionsExample)

' Export file to disk
results.ExportFilterImagesToDisk("medianBlur.png")
$vbLabelText   $csharpLabel

將此篩選器套用至範例輸入後,會產生下圖所示的結果。

Blurry barcode example showing poor image quality with digital artifacts and reduced readability
Barcode with median blur filter applied showing blurred vertical lines and number 4902030187590

如何在每個處理步驟中節省已過濾的圖片?

當對BARCODE套用多個篩選器時,可能難以在每次篩選器方法執行後檢視輸出結果。 此功能允許在應用每個濾鏡後,依照處理順序儲存經過濾鏡處理的影像。 若要啟用此功能,請先將 true 傳遞給 ImageFilterCollection 的建構函式。 接著使用 ExportFilterImagesToDisk 方法提供輸出圖片的路徑與名稱。 如需更多關於儲存BARCODE的範例,請參閱我們的"將BARCODE轉換為圖片"範例

:path=/static-assets/barcode/content-code-examples/how-to/image-correction-save-iterations.cs
using IronBarCode;

BarcodeReaderOptions myOptionsExample = new BarcodeReaderOptions()
{
    // Choose which filters are to be applied (in order)
    ImageFilters = new ImageFilterCollection(true) {
        new SharpenFilter(3.5f),
        new AdaptiveThresholdFilter(0.5f),
        new ContrastFilter(2)
    },
};

// Apply options and read the barcode
BarcodeResults results = BarcodeReader.Read("sample.webp", myOptionsExample);

// Export file to disk
results.ExportFilterImagesToDisk("filteredImage.png");
Imports IronBarCode

Private myOptionsExample As New BarcodeReaderOptions() With {
	.ImageFilters = New ImageFilterCollection(True) From {
		New SharpenFilter(3.5F),
		New AdaptiveThresholdFilter(0.5F),
		New ContrastFilter(2)
	}
}

' Apply options and read the barcode
Private results As BarcodeResults = BarcodeReader.Read("sample.webp", myOptionsExample)

' Export file to disk
results.ExportFilterImagesToDisk("filteredImage.png")
$vbLabelText   $csharpLabel

濾鏡會依照程式碼的順序套用,輸出圖像則反映每次迭代的結果:

  • Sharpen -> Sharpen 之後
  • Sharpen + AdaptiveThreshold -> AdaptiveThreshold 之後
  • Sharpen + AdaptiveThreshold + Contrast -> Contrast 之後
Blurry barcode with number 4902030187590
Blurry UPC barcode showing number 4902030187590
Degraded barcode example showing poor image quality with number 9020301875905
Heavily pixelated barcode with UPC number 902030187590

除了 ImageFilters 屬性外,請在 BarcodeReaderOptions 中新增其他屬性,以使閱讀更為精確; 如需更多資訊,請參閱這篇文章

常見問題

什麼是影像校正濾鏡,為什麼需要用於條碼讀取?

IronBarcode 中的影像校正濾鏡是內建工具,用於程式化地增強模糊或不完美的條碼影像。它們是必需的,因為劣質的影像品質是阻礙成功條碼掃描的主要因素之一。IronBarcode 提供的濾鏡如 SharpenFilter 和 ContrastFilter 在不需要外部影像編輯軟體或重新捕捉影像的情況下,提高了閱讀的準確性。

如何應用影像校正濾鏡來改善條碼掃描?

要在 IronBarcode 中應用濾鏡,需創建一個 ImageFilterCollection 實例並將個別濾鏡實例添加進去。然後將此集合分配給 BarcodeReaderOptions 的 ImageFilters 屬性,並將其傳遞給 Read 方法。例如:new BarcodeReaderOptions { ImageFilters = new ImageFilterCollection() { new SharpenFilter(3.5f), new ContrastFilter(2.0f) } }。

哪些影像濾鏡適合模糊條碼影像?

對於模糊的條碼影像,IronBarcode 建議至少使用 SharpenFilter 和 ContrastFilter。SharpenFilter 增強模糊影像中的邊緣定義,而 ContrastFilter 改善亮暗區域之間的區分。這些濾鏡配合使用,使條碼更易於讀取而無需外部影像處理。

我可以自定義影像校正濾鏡的強度嗎?

是的,IronBarcode 允許您使用自定義值配置每個濾鏡。例如,SharpenFilter 接受一個浮點參數(如 3.5f)以控制銳化強度,而 ContrastFilter 接受一個參數(如 2.0f)以調整對比度。此自定義有助於針對不同影像條件優化濾鏡效果。

我需要外部影像編輯工具來增強條碼影像嗎?

不需要,IronBarcode 提供內建的影像校正濾鏡,消除了對外部影像編輯工具的需求。這些程式化的濾鏡,如 SharpenFilter 和 ContrastFilter,可以直接在您的 .NET 應用程式中增強影像品質,節省時間並避免依賴第三方軟體。

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

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

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

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

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

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

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

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

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

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

Hairil Hasyimi Bin Omar
軟體工程師
如同所有傑出的工程師,Hairil 是一位熱衷學習的人。他正不斷精進自己在 C#、Python 和 Java 方面的知識,並運用這些知識為 Iron Software 的團隊成員創造價值。Hairil 從馬來西亞馬拉科技大學(Universiti Teknologi MARA)加入 Iron Software 團隊,他在該校取得化學與製程工程學士學位。
準備好開始了嗎?
Nuget 下載 2,240,258 | 版本: 2026.5 just released
Still Scrolling Icon

還在捲動嗎?

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