How to use Image Correction Filters For Barcode Using C#

使用 C## 图像校正过滤器改进条形码解码

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

IronBarcode提供了内置的图像校正过滤器如ContrastFilter,可以通过程序增强模糊或不完美的条码图像,提高读取准确性,而无需外部图像编辑软件或重新捕捉图像。

并非每张图像都是完美的,图像质量差是妨碍 IronBarcode 成功读取条码的主要因素之一。 IronBarcode 不需要重新捕捉图像或使用外部图像增强软件,而是提供内置过滤器,以编程方式提高图像质量。 这些过滤器可帮助 IronBarcode 读取困难的图像并提高整体准确性。

继续阅读,了解 IronBarcode 中可用的图像校正过滤器、它们对图像的影响以及如何应用它们。 有关更全面的条形码阅读技巧,请查看我们的阅读条形码教程

快速入门:应用锐化和对比度滤镜以改善条码读取

只需一步,就可以使用IronBarcode的BarcodeReaderOptions中。 这可以提高条形码扫描的效率,只需最少的设置和对外部工具的零需求。

  1. 使用 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

如何应用图像过滤器来改进条形码读取?

要应用过滤器,实例化ImageFilterCollection类,并分别创建每个过滤器的实例。 然后将对象分配给BarcodeReaderOptions属性。 将选项对象与样本图像一起传递到Read方法中。 有关高级安装选项,请访问我们的 NuGet 软件包指南

请使用下图作为样本图片。

带有数字 4900203187590 的模糊条形码,展示滤镜增强前较差的图像质量

图片看起来很模糊。 但是亮度尚可接受,白色和黑色也能区分。 因此,请至少应用ContrastFilter来提高条码的可读性。 请参考下面的代码片段,对图片应用过滤器、读取图片并在控制台上显示结果。

: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

上面的代码片段应用过滤器,读取条形码,并将过滤后的图像导出到磁盘。 样本与过滤后图片的对比如下所示。

带有数字 4902030187590 的模糊条形码图像,展示较差的图像质量
应用图像滤镜后可读性提高的条形码,显示清晰的垂直线条和数字 4902030187590

IronBarcode 提供哪些图像校正过滤器?

IronBarcode 提供多种图像过滤器,专门用于图像校正。 这些过滤器有助于读取不完美的条形码图像并提高读取精度。 但是,要了解这些过滤器的工作原理,以选择合适的过滤器避免因使用过多过滤器或使用错误过滤器而产生性能问题。 可用的过滤器包括

  • AdaptiveThresholdFilter
  • BinaryThresholdFilter
  • BrightnessFilter
  • ContrastFilter
  • InvertFilter
  • SharpenFilter
  • ErodeFilter
  • DilateFilter
  • HistogramEqualizationFilter
  • 模糊过滤器
    • GaussianBlurFilter
    • BilateralFilter
    • MedianBlurFilter

应用过滤器的顺序取决于它们在ImageFilterCollection中的位置。 有关这些过滤器的详细 API 文档,请访问我们的 API Reference

自适应阈值过滤器如何工作?

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

以下是使用不同值进行筛选后的输出结果。

垂直线条显示不同的自适应阈值滤镜输出,带有实线和虚线图案
低质量条形码图像,显示 UPC 编号 902030187590,存在明显的视觉失真

构造函数接受用于配置的附加参数:

  • Upper:用于阈值分割的上部(白色)颜色。
  • Lower:用于阈值分割的下部(黑色)颜色。
  • Threshold:用于二值化的阈值限制(0.0-1.0)。
  • Rectangle:应用处理器的矩形区域。

如上面的输出图像所示,图像经过二值化处理,只有两种颜色。 虽然在条形码读取方面似乎仍不理想,但需要结合使用过滤器。 请尝试使用参数灵敏度以达到最佳效果。

二进制阈值过滤器如何工作?

BinaryThresholdFilter通过在指定阈值处分割像素来过滤图像,比较色彩组件的亮度。 类似于AdaptiveThresholdFilter,若使用不当,此过滤器可能引入新的或不需要的噪声。 不过,IronBarcode 为过滤器属性设置了默认值。

类似于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

以下是对示例图片应用过滤器后的输出示例。

二值阈值滤镜输出的三个示例,显示稀疏、点状和密集的垂直线条图案
使用 0.9 二值化阈值滤镜处理的条形码图像,显示黑白对比

观察上面的输出图像,样本已被二值化为黑白两色。 然而,由于消除了条形码条,并引入了新的噪声,该过滤器显然不适合该图像。 如需处理条形码疑难情况,请参阅我们的未识别条形码疑难解答指南

如何调整图像亮度以更好地读取条形码?

BrightnessFilter是IronBarcode中图像过滤器集合中的另一个重要过滤器。 顾名思义,该过滤器可调整 BarCode 图像的亮度。 该构造函数的输入会改变输出图像中的 Amount 亮度。 默认值为 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
带有产品编号 4902030187590 的模糊条形码,展示低亮度或较差的图像质量

如何使用对比度过滤器来增强条形码图像?

ContrastFilter调整图像的对比度水平。 图像对比度是指图像中不同元素之间颜色强度的差异。 提高对比度可以增强细节的可视性,使图像显得生动、醒目,而降低对比度则会使图像显得更加柔和、沉稳。 有关条形码定制的详细信息,请参阅我们的条形码样式定制指南。

默认值为 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

将该过滤器应用到示例输入中会生成下图。

带有数字 4902030187590 的模糊条形码,展示默认对比度滤镜设置
带有数字 4902030187590 的模糊条形码,展示低对比度图像质量

何时应使用反相过滤器?

这种滤镜可以反转图像内部的颜色,使颜色相反,如白变黑、黑变白。它在读取带背景色的 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

下面的输出图像是将该过滤器应用于样本输入图像的结果。

显示数字 480203187590 的模糊 UPC 条形码 - 应用反色滤镜前的原始图像
模糊的反色条形码,在深色背景上显示白色条形,带有数字序列 4902030187590

如何使用锐化过滤器修复模糊的 BarCode 图像?

IronBarcode 提供了一个锐化过滤器。 该过滤器可增强图像的清晰度,在处理模糊图像时非常有用。 通过在实例化滤镜对象时调整 Sigma 值来操纵该滤镜以调整图像的清晰度。 默认值为 3,增加西格玛值可提高图像清晰度。 有关其他性能优化选项,请查看我们的阅读速度选项指南。

: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

下图是示例输入图像的 锐化版本。

模糊的条形码图像,展示应用锐化滤镜前未经锐化的质量
模糊的条形码示例,展示图像质量下降的效果

将上面的图片与原始图片相比,显得更加清晰,有助于使用 IronBarcode 读取条形码。 大多数情况下,ImageFilterCollection类中的其他过滤器一起应用。

Erode 过滤器有哪些用途?

ErodeFilter通过删除形状边缘附近的像素来去除小白噪声并加厚条码条。当条码背景有很多白色斑点或条码图像分辨率过低或模糊时,这个过滤器最适合使用,导致条合并。 ErodeFilter使条码条加厚,同时去除背景中的白色斑点。 有关处理不完美图像的更多信息,请参阅我们的不完美条形码示例

通过为过滤器输入代表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
显示数字 4002030187590 的模糊条形码 - 腐蚀滤镜演示示例
模糊的条形码,显示黑白垂直条纹和数字代码

比较上面的输入和输出图像,有些条形图明显变粗,这是因为输入了更大的内核尺寸进行过滤。 然而,整体画面中的白色斑点减少了。 根据侵蚀过滤器的性质,内核尺寸越大,如果应用得过于激烈,可能会擦除细条,如上图所示。 通过更改内核大小值输入到ErodeFilter来测试和细化效果。

Dilate 过滤器如何帮助条码读取?

ErodeFilter的反向,通过向对象边界添加像素来扩展明亮区域(通常是背景)。 虽然该过滤器可通过填补小缝隙或增强低对比度区域来修复损坏或模糊的条形码,但请注意其对条形码条的效果与直觉不同。 由于扩张会放大明亮的空间,因此会间接稀释黑色条形码条(假设背景为白色)等暗色元素。 这使得过滤器在条形码条显得过粗或合并的情况下特别有效,但过度使用会使条形码过分变窄,从而降低扫描精度。

与上面类似,通过为过滤器输入表示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
显示数字 4902030187590 的模糊条形码图像 - 膨胀滤镜处理示例
模糊的条形码,垂直条形下方带有数字序列

如上图所示,过度使用DilateFilter可能会破坏条码结构,合并紧密间隔的条并在条码中创建安静区域。 根据输入图像的不同,通过改变内核大小值的大小来测试和完善图像效果。

何时应使用直方图均衡化筛选器?

HistogramEqualizationFilter通过重新分配像素强度来改善图像对比度,以提高清晰度。 它最常用于条形码对比度较低的情况,如褪色或冲淡的图像,或者光照不均匀的图像,如暗影或强光。 通过分析图像直方图(即像素亮度分布图),它可以重新分配像素值,通过拉伸强度范围来增强对比度,使暗像素变暗,亮像素变亮。

: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
带有数字 4902030187590 的模糊条形码,用作直方图均衡化滤镜的测试图像
带有黑白垂直条纹的条形码,显示数字 4902030187590

如上图所示,与原图相比,黑条明显变暗,空格明显变亮。

哪些模糊过滤器可帮助降低 BarCode 噪声?

高斯模糊过滤器如何降低图像噪音?

GaussianBlurFilter对图像应用高斯模糊。 这种滤波器通常可以减少图像中的噪点。 有关处理不完美条形码的综合指南,请参阅我们的图像方向校正教程

滤波器的工作原理是使用高斯函数平均图像中的相邻像素值。 该方法依赖于两个可调整的因素:

  • 内核:用于平均像素的矩阵。
  • 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

将该过滤器应用到示例输入中会生成下图。

带有数字 4902030187590 的模糊条形码,展示较差的图像质量
应用高斯模糊滤镜的条形码图像,显示模糊的垂直线条和扭曲的数字

何时使用双边过滤器?

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

将该过滤器应用到示例输入中会生成下图。

模糊的条形码,展示较差的图像质量,带有垂直线条和数字 4902030187590
带有数字 4902030187590 的模糊条形码,展示较差的图像质量

是什么让 MedianBlur 滤波器在降噪方面与众不同?

MedianBlurFilter通过将每个像素的值替换为周围像素的中值来减少图像中的噪声。 该过滤器尤其擅长在去除噪声的同时保留边缘。要探索有关条码读取设置的更多信息,请访问我们的条码阅读器设置指南

  • 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

将该过滤器应用到示例输入中会生成下图。

模糊的条形码示例,展示带有数字伪影和可读性降低的较差图像质量
应用中值模糊滤镜的条形码,显示模糊的垂直线条和数字 4902030187590

如何在每个处理步骤中保存过滤后的图像?

在对 BarCode 应用多个过滤器时,查看每个过滤器方法后的输出可能会比较困难。 该功能允许在应用每个过滤器后,按照处理顺序保存过滤后的图像。 要启用此功能,首先将ImageFilterCollection构造函数。 然后使用ExportFilterImagesToDisk方法为输出图像提供路径和名称。 有关保存 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
带有数字 4902030187590 的模糊条形码
Blurry UPC barcode showing number 4902030187590
退化的条形码示例,展示带有数字 9020301875905 的较差图像质量
带有 UPC 编号 902030187590 的严重像素化条形码

除了BarcodeReaderOptions添加其他属性,以便更精确地读取; 请参阅此文章以获取更多信息。

常见问题解答

什么是图像校正过滤器,为什么条形码读取需要它们?

IronBarcode 中的图像校正过滤器是内置工具,可通过编程增强模糊或不完美的条码图像。图像质量差是妨碍成功读取条码的主要因素之一,因此图像校正过滤器是必不可少的。IronBarcode 提供的 SharpenFilter 和 ContrastFilter 等过滤器可在不需要外部图像编辑软件或重新捕获图像的情况下提高读取准确性。

如何应用图像校正过滤器来改进 BarCode 扫描?

要在 IronBarcode 中应用过滤器,需要创建一个 ImageFilterCollection 实例,并向其中添加单个过滤器实例。然后将此集合分配给 BarcodeReaderOptions 的 ImageFilters 属性,并将其传递给读取方法。例如:new BarcodeReaderOptions { ImageFilters = new ImageFilterCollection() { new SharpenFilter(3.5f), new ContrastFilter(2.0f) }.}.

建议使用哪些图像过滤器来处理模糊的 BarCode 图像?

对于模糊的条码图像,IronBarcode 建议至少使用 SharpenFilter 和 ContrastFilter。SharpenFilter 可增强模糊图像的边缘清晰度,而 ContrastFilter 则可改善明暗区域之间的区别。这些滤镜共同作用,使 BarCode 在不进行外部图像处理的情况下更加易读。

能否自定义图像校正滤镜的强度?

是的,IronBarcode 允许您用自定义值配置每个过滤器。例如,锐化过滤器(SharpenFilter)接受一个浮点参数(如 3.5f)来控制锐化强度,对比度过滤器(ContrastFilter)接受一个参数(如 2.0f)来调整对比度水平。这种自定义功能有助于针对不同的图像条件优化滤镜效果。

我是否需要外部图像编辑工具来增强 BarCode 图像?

不,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 从马来西亚的玛拉工业大学加入 Iron Software 团队,获得化学与工艺工程学士学位。
准备开始了吗?
Nuget 下载 2,317,217 | 版本: 2026.7 刚刚发布
Still Scrolling Icon

还在滚动吗?

想快速获得证据? PM > Install-Package BarCode
运行示例 观看您的字符串变成 BarCode。