在C#中快速且轻松地读取Code 39条形码
IronBarcode 通过使用BarcodeReaderOptions类并指定BarcodeEncoding.Code39简化了在C#中读取标准和扩展的Code 39条形码的过程,并在需要时启用UseCode39ExtendedMode以支持完整的ASCII字符。
Code 39 是一种通用的条形码格式,广泛应用于库存、物流和工业领域。 Code 39 BarCode 的长度可以变化,因此可以灵活地用于不同的用例。
原始的标准 Code 39 对大写字母(A-Z)、数字(0-9)和几个特殊字符(空格、-、$、+、% 和 .)进行编码。 这对基本 ID 非常有效,但现代应用程序通常需要对所有 128 个 ASCII 字符进行编码。 Code 39 扩展规范满足了这一需求。
本指南演示了如何使用 IronBarcode 读取标准和扩展的 Code 39 变体。 无论您是要构建库存管理系统、跟踪货运还是处理工业条码,IronBarcode 都能提供可靠的 Code 39 读取功能。 要全面了解条形码读取功能,请查看我们的综合条形码快速入门指南。
快速入门:在C#中读取Code 39条形码
使用IronBarcode的BarcodeReader从图像中解码Code 39条形码,只需一行代码。 立即开始 - 指定编码类型,传递您的图像,并读取结果。
-
1使用 IronBarcode 安装 NuGet Package Manager
-
2复制并运行这段代码。
using System.Linq; IronBarCode.BarcodeReader.Read("code39.png", new IronBarCode.BarcodeReaderOptions { ExpectBarcodeTypes = IronBarCode.BarcodeEncoding.Code39 }).First().ToString();Dim result As String = IronBarCode.BarcodeReader.Read("code39.png", New IronBarCode.BarcodeReaderOptions With {.ExpectBarcodeTypes = IronBarCode.BarcodeEncoding.Code39}).First().ToString()Dim result As String = IronBarCode.BarcodeReader.Read("code39.png", New IronBarCode.BarcodeReaderOptions With {.ExpectBarcodeTypes = IronBarCode.BarcodeEncoding.Code39}).First().ToString() -
3部署到您的生产环境中进行测试
今天就用免费试用在您的项目中开始使用IronBarcode
如何在 C# 中读取 Code 39 条形码
- 下载IronBarcode C#库以读取Code39条形码
- 初始化一个新的
BarcodeReaderOptions - 在选项中指定
BarcodeEncoding.Code39 - 使用
Read读取 Code 39 条形码 - 验证结果并打印到控制台
如何读取标准Code 39条形码?
使用 IronBarcode 读取 Code 39 条形码非常简单。 首先,初始化一个新的BarcodeReaderOptions并将条形码类型指定为BarcodeEncoding.Code39。 这样可以准确地告诉读取器要查找的条形码格式,从而优化读取器。
接下来,使用Read方法读取条形码,将条形码图像和选项作为参数传递。 然后遍历结果集合,并将每个 BarCode 的字符串值打印到控制台。 对于更高级的配置,请参阅我们关于条形码读取器设置的详细指南。
标准Code 39条形码看起来是什么样的?
此图像包含标准 Code 39 条形码。 请注意下面的条形码是如何以条形和人类可读文本的形式显示其编码值的。 这种双重表示法是工业和物流应用中典型的 Code 39 BarCode。

读取标准Code 39需要什么代码?
using IronBarCode;
using System;
BarcodeReaderOptions options = new BarcodeReaderOptions()
{
// Tell the reader to only look for Code 39.
ExpectBarcodeTypes = BarcodeEncoding.Code39
};
// Read barcode(s) from the image file using the specified options
var results = BarcodeReader.Read("code39.png", options);
// Loop through each BarcodeResult found in the image
foreach (var result in results)
{
// Print the decoded string value of the standard Code 39 barcode
Console.WriteLine(result.ToString());
}using IronBarCode;
using System;
BarcodeReaderOptions options = new BarcodeReaderOptions()
{
// Tell the reader to only look for Code 39.
ExpectBarcodeTypes = BarcodeEncoding.Code39
};
// Read barcode(s) from the image file using the specified options
var results = BarcodeReader.Read("code39.png", options);
// Loop through each BarcodeResult found in the image
foreach (var result in results)
{
// Print the decoded string value of the standard Code 39 barcode
Console.WriteLine(result.ToString());
}Imports IronBarCode
Imports System
Dim options As New BarcodeReaderOptions() With {
.ExpectBarcodeTypes = BarcodeEncoding.Code39
}
' Read barcode(s) from the image file using the specified options
Dim results = BarcodeReader.Read("code39.png", options)
' Loop through each BarcodeResult found in the image
For Each result In results
' Print the decoded string value of the standard Code 39 barcode
Console.WriteLine(result.ToString())
NextImports IronBarCode
Imports System
Dim options As New BarcodeReaderOptions() With {
.ExpectBarcodeTypes = BarcodeEncoding.Code39
}
' Read barcode(s) from the image file using the specified options
Dim results = BarcodeReader.Read("code39.png", options)
' Loop through each BarcodeResult found in the image
For Each result In results
' Print the decoded string value of the standard Code 39 barcode
Console.WriteLine(result.ToString())
Next指定预期的 BarCode 类型可显著提高读取性能。 IronBarcode 不会浪费时间寻找其他条码格式,这尤其有利于批量处理大型图像集。 通过我们的读取速度选项指南了解更多关于优化条形码读取性能的信息。
我应该期待什么输出结果?

控制台输出显示了从我们的 Code 39 BarCode 中成功解码的值 "ABC-1234"。 退出代码 0 表示执行成功,无错误。 在生产应用程序中,针对可能无法识别 BarCode 的情况实施适当的错误处理。 如果您遇到问题,请查看我们的未识别条形码故障排除指南。
如何读取扩展Code 39条形码?
读取扩展Code 39条形码与标准Code 39的过程类似。关键区别是将UseCode39ExtendedMode属性设置为true。
此设置可指示 IronBarcode 解释特殊字符对(如 +T、%O),并将其解码为等效的全 ASCII 字符(如 t、!)。扩展 Code 39 使用双字符序列来表示标准字符集之外的字符。 这使得 BarCode 稍长,但可以对小写字母、附加标点符号和控制字符进行编码。
什么时候应该使用扩展的Code 39?
当应用程序需要对以下内容进行编码时,扩展 Code 39 是理想选择:
- 混合大小写文本(大写字母和小写字母)
- 特殊字符如 @、#、&、!、?
- 数据传输控制字符
- 完全支持 ASCII 字符集
常见的应用包括医疗保健系统、文档跟踪和需要丰富数据编码的高级库存管理。
扩展的Code 39条形码看起来是什么样的?
此图像包含扩展的 Code 39 条形码。 值Test-Data!包含小写字符和感叹号,这些字符仅在完整的ASCII集中可用,并需要扩展模式。

扩展Code 39需要什么代码?
using IronBarCode;
using System;
BarcodeReaderOptions options = new BarcodeReaderOptions()
{
// Enable extended Code 39 mode
UseCode39ExtendedMode = true,
// Specify that we are expecting Code 39 barcodes
ExpectBarcodeTypes = BarcodeEncoding.Code39
};
// Read barcode(s) from the extended code 39 image
var results = BarcodeReader.Read("code39extended.png", options);
// Loop through each BarcodeResult found in the image
foreach (var result in results)
{
// Print the fully decoded ASCII string (e.g., "Test-Data!")
Console.WriteLine(result.ToString());
}using IronBarCode;
using System;
BarcodeReaderOptions options = new BarcodeReaderOptions()
{
// Enable extended Code 39 mode
UseCode39ExtendedMode = true,
// Specify that we are expecting Code 39 barcodes
ExpectBarcodeTypes = BarcodeEncoding.Code39
};
// Read barcode(s) from the extended code 39 image
var results = BarcodeReader.Read("code39extended.png", options);
// Loop through each BarcodeResult found in the image
foreach (var result in results)
{
// Print the fully decoded ASCII string (e.g., "Test-Data!")
Console.WriteLine(result.ToString());
}Imports IronBarCode
Imports System
Dim options As New BarcodeReaderOptions() With {
.UseCode39ExtendedMode = True,
.ExpectBarcodeTypes = BarcodeEncoding.Code39
}
' Read barcode(s) from the extended code 39 image
Dim results = BarcodeReader.Read("code39extended.png", options)
' Loop through each BarcodeResult found in the image
For Each result In results
' Print the fully decoded ASCII string (e.g., "Test-Data!")
Console.WriteLine(result.ToString())
NextImports IronBarCode
Imports System
Dim options As New BarcodeReaderOptions() With {
.UseCode39ExtendedMode = True,
.ExpectBarcodeTypes = BarcodeEncoding.Code39
}
' Read barcode(s) from the extended code 39 image
Dim results = BarcodeReader.Read("code39extended.png", options)
' Loop through each BarcodeResult found in the image
For Each result In results
' Print the fully decoded ASCII string (e.g., "Test-Data!")
Console.WriteLine(result.ToString())
Next从扩展模式中我应该期待什么输出结果?

高级Code 39读取技术
处理多个条形码
IronBarcode 可自动检测和读取单个图像中的多个 Code 39 条形码。 Read方法返回一个结果集合,允许您单独处理每个条形码。 对于处理整页条形码或复杂文档的应用程序,请查看我们的读取多个条形码指南。
处理低质量图像
Code 39 BarCode 有时会在不太理想的条件下出现,例如打印褪色、角度倾斜或扫描分辨率较低。 IronBarcode包括强大的图像校正滤镜,可以显著提高读取准确性:
BarcodeReaderOptions options = new BarcodeReaderOptions()
{
ExpectBarcodeTypes = BarcodeEncoding.Code39,
UseCode39ExtendedMode = true,
// Apply image correction filters
ImageFilters = new ImageFilterCollection() {
new SharpenFilter(),
new ContrastFilter(),
new BrightnessFilter()
}
};BarcodeReaderOptions options = new BarcodeReaderOptions()
{
ExpectBarcodeTypes = BarcodeEncoding.Code39,
UseCode39ExtendedMode = true,
// Apply image correction filters
ImageFilters = new ImageFilterCollection() {
new SharpenFilter(),
new ContrastFilter(),
new BrightnessFilter()
}
};Imports System
Dim options As New BarcodeReaderOptions() With {
.ExpectBarcodeTypes = BarcodeEncoding.Code39,
.UseCode39ExtendedMode = True,
' Apply image correction filters
.ImageFilters = New ImageFilterCollection() From {
New SharpenFilter(),
New ContrastFilter(),
New BrightnessFilter()
}
}Imports System
Dim options As New BarcodeReaderOptions() With {
.ExpectBarcodeTypes = BarcodeEncoding.Code39,
.UseCode39ExtendedMode = True,
' Apply image correction filters
.ImageFilters = New ImageFilterCollection() From {
New SharpenFilter(),
New ContrastFilter(),
New BrightnessFilter()
}
}性能优化
对于大批量条形码读取应用,可以考虑这些优化策略:
- 指定确切的条形码类型 - 始终设定
ExpectBarcodeTypes以避免不必要的扫描 - 使用适当的读取速度 - 根据需要平衡速度和准确性
- 并行处理图像 - 利用多线程进行批量处理
- 预处理图像 - 仅在必要时应用校正以保持性能
总结
IronBarcode 简化了 C# 中的 Code 39 条码读取,无论是使用标准格式还是扩展格式。 关键步骤如下
有了这些基础知识,您就可以将 Code 39 条码读取功能集成到您的 .NET 应用程序中了。 有关完整的API文档和其他条形码格式,请访问我们的全面API参考。 若要获取特定于Code 39的实践示例,请查看我们的专用Code 39教程。
常见问题
如何使用C#读取标准的Code 39条形码?
要在C#中读取标准的Code 39条形码,使用IronBarcode库。初始化BarcodeReaderOptions,指定BarcodeEncoding.Code39,并使用带有条形码图像的Read方法。
标准和扩展Code 39条形码有什么区别?
标准的Code 39编码大写字母、数字和一些特殊字符。扩展的Code 39通过使用+T或%O这样的字符对来表示标准集合之外的字符,以支持完整的ASCII集。
Code 39条形码的常见使用场景是什么?
由于其可变长度和在编码基本ID方面的灵活性,Code 39条形码常用于库存、物流和工业应用。
IronBarcode如何处理质量不佳的条形码图像?
IronBarcode包括图像校正滤镜,如锐化和对比度调整,以提高质量不佳条形码图像的读取准确性。
在C#中读取扩展的Code 39条形码需要做什么?
要读取扩展的Code 39条形码,在IronBarcode的BarcodeReaderOptions中启用UseCode39ExtendedMode以允许解码完整的ASCII字符集。
如何在高负载应用中优化条形码读取性能?
对于高负载应用,设置ExpectBarcodeTypes以限制扫描到Code 39,使用适当的读取速度,并并行处理图像以提高性能。
IronBarcode能从单个图像读取多个条形码吗?
是的,IronBarcode可以通过Read方法检测并从单个图像中读取多个Code 39条形码,该方法返回结果集合。
在读取扩展的Code 39条形码时应期望得到什么输出?
在读取扩展的Code 39条形码时,期望控制台显示完整解码的ASCII字符串,可能包括小写字母和特殊字符。
为什么我需要使用扩展的Code 39条形码?
扩展的Code 39在编码混合大小写文本、特殊字符或控制字符时非常有用,使其非常适合需要详细数据编码的应用,如医疗或文档追踪。
`BarcodeReaderOptions`在IronBarcode中的作用是什么?
BarcodeReaderOptions允许您指定条形码类型和扩展模式等设置,通过配置预期输入并启用额外功能优化读取过程。
Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。