使用 C# 图像校正过滤器改进条形码解码
IronBarcode提供内置的图像校正滤镜,如 SharpenFilter 和 ContrastFilter,可通过编程方式增强模糊或不完美的条形码图像,提高读取精度,而无需外部图像编辑软件或重新采集图像。
并非每张图像都是完美的,图像质量差是妨碍 IronBarcode 成功读取条码的主要因素之一。 IronBarcode 不需要重新捕捉图像或使用外部图像增强软件,而是提供内置过滤器,以编程方式提高图像质量。 这些过滤器可帮助 IronBarcode 读取困难的图像并提高整体准确性。
继续阅读,了解 IronBarcode 中可用的图像校正过滤器、它们对图像的影响以及如何应用它们。 有关更全面的条形码阅读技巧,请查看我们的阅读条形码教程。
快速入门:应用锐化和对比度滤镜来改善条形码读取效果
只需一步,即可应用 IronBarcode 的 SharpenFilter 和 ContrastFilter,使用 ImageFilterCollection 在 BarcodeReaderOptions 中。 这可以提高条形码扫描的效率,只需最少的设置和对外部工具的零需求。
-
使用 NuGet 包管理器安装 https://www.nuget.org/packages/BarCode
PM > Install-Package BarCode -
复制并运行这段代码。
BarcodeResults results = IronBarCode.BarcodeReader.Read("input.png", new IronBarCode.BarcodeReaderOptions { ImageFilters = new IronBarCode.ImageFilterCollection() { new IronBarCode.SharpenFilter(3.5f), new IronBarCode.ContrastFilter(2.0f) } }); -
部署到您的生产环境中进行测试
通过免费试用立即在您的项目中开始使用IronBarcode
最小工作流程(5 个步骤)
- 下载 C# 库以使用图像校正滤镜
- 探索所有可用的图像校正滤镜
- 用自定义值配置每个过滤器
- 对不完美的图像样本应用过滤器
- 从增强图像中读取 BarCode 值
如何应用图像过滤器来改进条形码读取?
要应用过滤器,请实例化 ImageFilterCollection 类,并分别创建每个过滤器的实例。 然后将该对象分配给 ImageFilters 对象的 BarcodeReaderOptions 属性。 将选项对象连同示例图像一起传递给 Read 方法。 有关高级安装选项,请访问我们的 NuGet 软件包指南。
请使用下图作为样本图片。
图片样本
图片看起来很模糊。 但是亮度尚可接受,白色和黑色也能区分。 因此,至少应用 SharpenFilter 和 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
上面的代码片段应用过滤器,读取条形码,并将过滤后的图像导出到磁盘。 样本与过滤后图片的对比如下所示。
图片样本
筛选样本
IronBarcode 提供哪些图像校正过滤器?
IronBarcode 提供多种图像过滤器,专门用于图像校正。 这些过滤器有助于读取不完美的条形码图像并提高读取精度。 但是,要了解这些过滤器的工作原理,以选择合适的过滤器,避免因使用过多过滤器或使用错误过滤器而产生性能问题。 可用的过滤器包括
AdaptiveThresholdFilterBinaryThresholdFilterBrightnessFilterContrastFilterInvertFilterSharpenFilterErodeFilterDilateFilterHistogramEqualizationFilter- 模糊过滤器
GaussianBlurFilterBilateralFilterMedianBlurFilter
应用筛选器的顺序基于它们在 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")
以下是使用不同值进行筛选后的输出结果。
默认值
0.9 价值
构造函数接受用于配置的附加参数:
Upper: 阈值处理的上部(白色)颜色。Lower: 阈值处理的较低(黑色)颜色。Threshold: 二值化阈值限制 (0.0-1.0)。Rectangle: 要应用处理器的矩形区域。
如上面的输出图像所示,图像经过二值化处理,只有黑和白两种颜色。 虽然在条形码读取方面似乎仍不理想,但需要结合使用过滤器。 请尝试使用参数灵敏度以达到最佳效果。
二进制阈值过滤器如何工作?
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")
以下是对示例图片应用过滤器后的输出示例。
默认值
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")
以下是对样本输入应用该过滤器后的输出图像。
默认值
1.5 价值
如何使用对比度过滤器来增强条形码图像?
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")
将该过滤器应用到示例输入中会生成下图。
默认值
1.5 价值
何时应使用反相过滤器?
这种滤镜可以反转图像内部的颜色,使颜色相反,如白变黑、黑变白。它在读取带背景色的 BarCode 图像时特别有用。 与 二进制阈值过滤器不同,该过滤器可直接反转颜色,而无需指定灵敏度。 此外,该过滤器可与 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")
下面的输出图像是将该过滤器应用于样本输入图像的结果。
原始图片
反转
如何使用锐化过滤器修复模糊的 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")
The image below is the sharpened version of the sample input image.
默认值
0.5 价值
将上面的图片与原始图片相比,显得更加清晰,有助于使用 IronBarcode 读取条形码。 在大多数情况下, SharpenFilter总是与 ImageFilterCollection 类中的其他过滤器一起应用。
Erode 过滤器有哪些用途?
ErodeFilter可去除微小的白噪声,并通过去除形状边缘附近的像素来加粗条形码条。该过滤器最适用于条形码背景有大量白色斑点的场景,或者条形码图像分辨率过低或模糊,导致条形码合并的情况。 ErodeFilter可使条形图变粗,同时去除背景中的白色斑点。 有关处理不完美图像的更多信息,请参阅我们的不完美条形码示例。
输入一个表示 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")
原始图片
应用 Erode 过滤器
比较上面的输入和输出图像,有些条形图明显变粗,这是因为输入了更大的内核尺寸进行过滤。 然而,整体画面中的白色斑点减少了。 根据侵蚀过滤器的性质,内核尺寸越大,如果应用得过于激烈,可能会擦除细条,如上图所示。 通过更改输入到 ErodeFilter 的内核大小值来测试和完善效果。
Dilate 过滤器如何帮助条码读取?
DilateFilter的功能与ErodeFilter相反,它通过在对象边界添加像素来扩大明亮区域(通常是背景)。 虽然该过滤器可通过填补小缝隙或增强低对比度区域来修复损坏或模糊的条形码,但请注意其对条形码条的效果与直觉不同。 由于扩张会放大明亮的空间,因此会间接稀释黑色条形码条(假设背景为白色)等暗色元素。 这使得过滤器在条形码条显得过粗或合并的情况下特别有效,但过度使用会使条形码过分变窄,从而降低扫描精度。
与上述类似,输入一个表示过滤器的整数,即可增强过滤器的效果。
在下面的示例中,请使用较大的内核尺寸来展示 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")
原始图片
应用扩展过滤器
如上图所示,积极使用 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")
原始图片
应用直方图均衡化滤波器
如上图所示,与原图相比,黑条明显变暗,空格明显变亮。
哪些模糊过滤器可帮助降低 BarCode 噪声?
高斯模糊过滤器如何降低图像噪音?
GaussianBlurFilter 将高斯模糊应用于图像。 这种滤波器通常可以减少图像中的噪点。 有关处理不完美条形码的综合指南,请参阅我们的图像方向校正教程。
滤波器的工作原理是使用高斯函数平均图像中的相邻像素值。 该方法依赖于两个可调整的因素:
- 内核:用于平均像素的矩阵。
- Sigma:控制模糊强度的值。
默认大小为 kernel 像素,默认值为 3x3 像素,产生适度的模糊效果。 增加 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")
将该过滤器应用到示例输入中会生成下图。
锐化图像
高斯模糊图像
何时使用双边过滤器?
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")
将该过滤器应用到示例输入中会生成下图。
锐化图像
双边图像
是什么让 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")
将该过滤器应用到示例输入中会生成下图。
锐化图像
MedianBlur 图像
如何在每个处理步骤中保存过滤后的图像?
在对 BarCode 应用多个过滤器时,查看每个过滤器方法后的输出可能会比较困难。 该功能允许在应用每个过滤器后,按照处理顺序保存过滤后的图像。 要启用此功能,首先将 true 传递给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")
过滤器按代码顺序应用,输出图像反映每次迭代的结果:
Sharpen-> 在Sharpen之后Sharpen+AdaptiveThreshold-> 在AdaptiveThreshold之后Sharpen+AdaptiveThreshold+Contrast-> 在Contrast之后
图片样本
锐化之后
自适应阈值之后
对比之后
除了 ImageFilters 属性之外,还要向 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 应用程序中提高图像质量,从而节省时间并避免依赖第三方软件。

