使用 C## 图像校正过滤器改进条形码解码
IronBarcode 提供了内置的图像校正滤镜(如 SharpenFilter 和 ContrastFilter),可通过编程方式增强模糊或不完美的 BarCode 图像,从而提高读取准确性,而无需使用外部图像编辑软件或重新捕获图像。
并非每张图像都是完美的,图像质量差是妨碍 IronBarcode 成功读取条码的主要因素之一。 IronBarcode 不需要重新捕捉图像或使用外部图像增强软件,而是提供内置过滤器,以编程方式提高图像质量。 这些过滤器可帮助 IronBarcode 读取困难的图像并提高整体准确性。
继续阅读,了解 IronBarcode 中可用的图像校正过滤器、它们对图像的影响以及如何应用它们。 有关更全面的条形码阅读技巧,请查看我们的阅读条形码教程。
快速入门:应用锐化和对比度滤镜以改善条码读取
只需一步,即可使用 ImageFilterCollection 在 BarcodeReaderOptions 中应用 IronBarcode 的 SharpenFilter 和 ContrastFilter。 这可以提高条形码扫描的效率,只需最少的设置和对外部工具的零需求。
-
使用 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 以提高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
上面的代码片段应用过滤器,读取条形码,并将过滤后的图像导出到磁盘。 样本与过滤后图片的对比如下所示。
图片样本
筛选样本
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 图像时特别有用。 与 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")
下面的输出图像是将该过滤器应用于样本输入图像的结果。
原始图片
反转
如何使用锐化过滤器修复模糊的 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 可去除细微的白色噪点,并通过消除形状边缘附近的像素来加粗 BARCODE 条纹。该滤镜最适用于 BARCODE 背景中存在大量白色斑点,或 BARCODE 图像分辨率过低、模糊导致条纹重叠的情况。 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 的逆运算,其工作原理是通过向对象边界添加像素,从而扩展明亮区域(通常是背景)。 虽然该过滤器可通过填补小缝隙或增强低对比度区域来修复损坏或模糊的条形码,但请注意其对条形码条的效果与直觉不同。 由于扩张会放大明亮的空间,因此会间接稀释黑色条形码条(假设背景为白色)等暗色元素。 这使得过滤器在条形码条显得过粗或合并的情况下特别有效,但过度使用会使条形码过分变窄,从而降低扫描精度。
与上述方法类似,通过输入代表 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")
原始图片
应用扩展过滤器
如上图所示,过度使用 DilateFilter 可能会破坏 BARCODE 结构,导致间距过近的条纹合并,并在 BARCODE 中产生静区。 根据输入图像的不同,通过改变内核大小值的大小来测试和完善图像效果。
何时应使用直方图均衡化筛选器?
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 值为 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")
将该过滤器应用到示例输入中会生成下图。
锐化图像
高斯模糊图像
何时使用双边过滤器?
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 应用程序中提高图像质量,从而节省时间并避免依赖第三方软件。
在条形码操作中使用IronBarcode的好处是什么?
IronBarcode提供了易于集成、对多种条形码格式的支持、高质量的图像生成和强大的读取能力等好处,使其成为C#条形码操作的综合工具。
IronBarcode是否提供支持自定义条形码外观的功能?
是的,IronBarcode为条形码外观提供广泛的自定义选项,包括颜色、大小和文本注释,允许运您将条形码设计成符合您特定的设计要求。
IronBarcode如何帮助提高业务流程的效率?
IronBarcode通过快速准确的条码生成和读取提高了业务流程效率,减少了手动数据输入错误,并改善了库存和资产跟踪。
在项目中实现IronBarcode需要哪些编程技能?
了解C#编程的基础知识就足以在项目中实现IronBarcode,因为它提供了简单的方法和全面的文档来指导开发人员。
IronBarcode适合小项目和大型企业应用吗?
IronBarcode被设计为可扩展且多功能,适合需要强大条码解决方案的小项目和大型企业应用。

