使用IRONBARCODE

使用条码阅读器SDK for .NET

乔尔迪·巴尔迪亚
乔尔迪·巴尔迪亚
2025年四月3日
分享:

条码扫描是许多应用程序的重要功能,从库存管理到零售和物流。 通过将条形码读取集成到您的.NET应用程序中,您可以简化数据捕获、自动化工作流程并提高效率。

IronBarcode 是一个强大的 .NET 库,使处理条形码变得简单。 使用此工具,您可以从图像、流和PDF文件中读取条形码,并生成二维码。 本文将向您展示如何在您的.NET应用程序中集成条形码扫描功能,重点是创建API或Web应用程序集成以展示条形码扫描功能。

最佳IronBarcode集成使用案例

IronBarcode 非常适合:

  • 库存管理系统 – 自动化条形码扫描以跟踪产品和库存水平。
  • 物流应用 – 处理用于运输、接收和包裹追踪的扫描条码图像。
  • 零售 POS 系统 – 验证条形码扫描以进行销售交易和价格查询。
  • 文档处理 – 从PDF发票、收据或身份证中提取条码数据。

如何在 .NET 中创建条形码读取器 SDK

要创建一个可以作为服务在您的应用程序中暴露的条码阅读器,您需要将IronBarcode集成到REST API或Web应用程序中。以下是使用ASP.NET Core实现这一目标的示例。

  1. 安装用于在C#中读取条形码的.NET库

  2. 创建一个可重用的条形码扫描类。

  3. 开发从不同来源读取条形码的方法。

  4. 将条形码图像读取集成到您的应用程序中。

  5. 测试和优化性能。

在我们开始之前

如果您还没有,请下载 IronBarcode用于您的项目。 确保您拥有适合您预期用途的正确许可证密钥。 请注意,通过公共API公开IronBarcode的功能或将其作为独立服务转售需要额外的许可(SDK、OEM或SaaS)。 在继续之前,请确保您了解许可事项。

创建条形码扫描器类

在将IronBarcode设置并安装到项目中后,您可以创建一个可重用的条形码扫描类,该类集成了IronBarcode的功能并将其作为API端点公开。

using IronBarCode;
using System.IO;
namespace BarcodeIntegration
{
    public class BarcodeScanner
    {
        static BarcodeScanner()
        {
            // Set the license key
            IronBarCode.License.LicenseKey = "Your-License-Key";
        }
        // Read a barcode from an image file
        public string ReadBarcodeFromImage(string imagePath)
        {
            try
            {
                var barcode = BarcodeReader.Read(imagePath);
                return barcode?.ToString() ?? "No Barcode Found";
            }
            catch (Exception ex)
            {
                return $"Error reading barcode: {ex.Message}";
            }
        }
        // Read a barcode from a stream (e.g., file upload or memory stream)
        public string ReadBarcodeFromStream(Stream inputStream)
        {
            try
            {
                var barcode = BarcodeReader.Read(inputStream);
                return barcode?.ToString() ?? "No barcode found";
            }
            catch (Exception ex)
            {
                return $"Error reading barcode: {ex.Message}";
            }
        }
        // Read a barcode from a PDF file
        public string ReadBarcodeFromPdf(string filePath)
        {
            try
            {
                var barcode = BarcodeReader.ReadPdf(filePath);
                return barcode?.ToString() ?? "No barcode found";
            }
            catch (Exception ex)
            {
                return $"Error reading barcode: {ex.Message}";
            }
        }
    }
}
using IronBarCode;
using System.IO;
namespace BarcodeIntegration
{
    public class BarcodeScanner
    {
        static BarcodeScanner()
        {
            // Set the license key
            IronBarCode.License.LicenseKey = "Your-License-Key";
        }
        // Read a barcode from an image file
        public string ReadBarcodeFromImage(string imagePath)
        {
            try
            {
                var barcode = BarcodeReader.Read(imagePath);
                return barcode?.ToString() ?? "No Barcode Found";
            }
            catch (Exception ex)
            {
                return $"Error reading barcode: {ex.Message}";
            }
        }
        // Read a barcode from a stream (e.g., file upload or memory stream)
        public string ReadBarcodeFromStream(Stream inputStream)
        {
            try
            {
                var barcode = BarcodeReader.Read(inputStream);
                return barcode?.ToString() ?? "No barcode found";
            }
            catch (Exception ex)
            {
                return $"Error reading barcode: {ex.Message}";
            }
        }
        // Read a barcode from a PDF file
        public string ReadBarcodeFromPdf(string filePath)
        {
            try
            {
                var barcode = BarcodeReader.ReadPdf(filePath);
                return barcode?.ToString() ?? "No barcode found";
            }
            catch (Exception ex)
            {
                return $"Error reading barcode: {ex.Message}";
            }
        }
    }
}
Imports IronBarCode
Imports System.IO
Namespace BarcodeIntegration
	Public Class BarcodeScanner
		Shared Sub New()
			' Set the license key
			IronBarCode.License.LicenseKey = "Your-License-Key"
		End Sub
		' Read a barcode from an image file
		Public Function ReadBarcodeFromImage(ByVal imagePath As String) As String
			Try
				Dim barcode = BarcodeReader.Read(imagePath)
				Return If(barcode?.ToString(), "No Barcode Found")
			Catch ex As Exception
				Return $"Error reading barcode: {ex.Message}"
			End Try
		End Function
		' Read a barcode from a stream (e.g., file upload or memory stream)
		Public Function ReadBarcodeFromStream(ByVal inputStream As Stream) As String
			Try
				Dim barcode = BarcodeReader.Read(inputStream)
				Return If(barcode?.ToString(), "No barcode found")
			Catch ex As Exception
				Return $"Error reading barcode: {ex.Message}"
			End Try
		End Function
		' Read a barcode from a PDF file
		Public Function ReadBarcodeFromPdf(ByVal filePath As String) As String
			Try
				Dim barcode = BarcodeReader.ReadPdf(filePath)
				Return If(barcode?.ToString(), "No barcode found")
			Catch ex As Exception
				Return $"Error reading barcode: {ex.Message}"
			End Try
		End Function
	End Class
End Namespace
$vbLabelText   $csharpLabel

此 BarcodeScanner 类抽象了 IronBarcode 的功能,使其易于集成到任何 .NET 应用程序中。 让我们来看一下这里包含的不同方法的详细分析:

读取条形码的方法

以下每种方法都尝试从不同类型的输入中读取条形码:

  • ReadBarcodeFromImage(string imagePath):从图像文件中读取条形码。
  • ReadBarcodeFromStream(Stream inputStream):从输入流(例如文件上传或内存流)读取条形码。
  • ReadBarcodeFromPdf(string filePath):从 PDF 文件中读取条形码。

    每个方法都会尝试读取条形码数据,并返回条形码字符串或错误信息(如果没有找到条形码或发生异常)。

通过REST API公开条形码读取

要允许外部应用程序使用您的条码扫描功能,您可以使用ASP.NET Core将其作为REST API公开。 以下是您可能进行此操作的示例:

using Microsoft.AspNetCore.Mvc;
using System.IO;
using Microsoft.AspNetCore.Http;
using BarcoderReader;
[ApiController]
[Route("api/barcode")]
public class BarcodeController : ControllerBase
{
    private readonly BarcodeScanner _barcodeScanner;
    public BarcodeController()
    {
        _barcodeScanner = new BarcodeScanner();
    }
    [HttpPost("read-from-image")]
    public IActionResult ReadFromImage(IFormFile file)
    {
        using var stream = file.OpenReadStream();
        var result = _barcodeScanner.ReadBarcodeFromStream(stream);
        return Ok(new { Barcode = result });
    }
}
using Microsoft.AspNetCore.Mvc;
using System.IO;
using Microsoft.AspNetCore.Http;
using BarcoderReader;
[ApiController]
[Route("api/barcode")]
public class BarcodeController : ControllerBase
{
    private readonly BarcodeScanner _barcodeScanner;
    public BarcodeController()
    {
        _barcodeScanner = new BarcodeScanner();
    }
    [HttpPost("read-from-image")]
    public IActionResult ReadFromImage(IFormFile file)
    {
        using var stream = file.OpenReadStream();
        var result = _barcodeScanner.ReadBarcodeFromStream(stream);
        return Ok(new { Barcode = result });
    }
}
Imports Microsoft.AspNetCore.Mvc
Imports System.IO
Imports Microsoft.AspNetCore.Http
Imports BarcoderReader
<ApiController>
<Route("api/barcode")>
Public Class BarcodeController
	Inherits ControllerBase

	Private ReadOnly _barcodeScanner As BarcodeScanner
	Public Sub New()
		_barcodeScanner = New BarcodeScanner()
	End Sub
	<HttpPost("read-from-image")>
	Public Function ReadFromImage(ByVal file As IFormFile) As IActionResult
		Dim stream = file.OpenReadStream()
		Dim result = _barcodeScanner.ReadBarcodeFromStream(stream)
		Return Ok(New With {Key .Barcode = result})
	End Function
End Class
$vbLabelText   $csharpLabel

Swagger UI

使用Barcode Reader SDK for .NET:图1

从Pixabay添加上传

或拖放图像到此处

添加图片替代文本

输出

使用Barcode Reader SDK for .NET: 图2

从Pixabay添加上传

或拖放图像到此处

添加图片替代文本

此 API 提供一个 POST 端点,可以在其中上传条形码图像,API 将返回条形码数据。 这允许其他系统或前端应用程序与您的条形码扫描仪作为服务进行交互。

高级功能

为了进一步增强SDK,考虑添加:

  1. 支持多种条形码类型:IronBarcode支持读取多种条形码的功能。 您可以添加选项以配置您的SDK以一次接受多个条形码。 例如
public string ReadBarcodeFromImage(string imagePath)
{
    try
    {
        BarcodeReaderOptions options = new BarcodeReaderOptions()
        {
            ExpectMultipleBarcodes = true,
            ExpectBarcodeTypes = BarcodeEncoding.AllOneDimensional,
        };
        foreach (var barcode in BarcodeReader.Read(imagePath, options))
        {
            return barcode.ToString();
        }
        return "No barcode found";
    }
    catch (Exception ex)
    {
        return $"Error reading barcode: {ex.Message}";
    }
}
public string ReadBarcodeFromImage(string imagePath)
{
    try
    {
        BarcodeReaderOptions options = new BarcodeReaderOptions()
        {
            ExpectMultipleBarcodes = true,
            ExpectBarcodeTypes = BarcodeEncoding.AllOneDimensional,
        };
        foreach (var barcode in BarcodeReader.Read(imagePath, options))
        {
            return barcode.ToString();
        }
        return "No barcode found";
    }
    catch (Exception ex)
    {
        return $"Error reading barcode: {ex.Message}";
    }
}
Public Function ReadBarcodeFromImage(ByVal imagePath As String) As String
	Try
		Dim options As New BarcodeReaderOptions() With {
			.ExpectMultipleBarcodes = True,
			.ExpectBarcodeTypes = BarcodeEncoding.AllOneDimensional
		}
		For Each barcode In BarcodeReader.Read(imagePath, options)
			Return barcode.ToString()
		Next barcode
		Return "No barcode found"
	Catch ex As Exception
		Return $"Error reading barcode: {ex.Message}"
	End Try
End Function
$vbLabelText   $csharpLabel
  1. 错误处理:扩展错误处理和日志记录,为您的SDK用户提供更好的诊断。

  2. 批量处理:如果您想从一组图像中处理多个条形码,您可以创建接受图像数组或流的方法。

  3. 自定义:提供条形码检测参数的配置选项,例如准确性、速度和支持的条形码类型。

许可注意事项

如前所述,IronBarcode SDK 旨在集成到您的内部应用程序中,通过 API 公开它需要额外的许可。 在将IronBarcode作为服务的一部分公开之前(例如公开API),您必须获得必要的许可(SDK、OEM或SaaS)。

不要将IronBarcode作为独立SDK进行转售,也不要通过面向公众的API公开它,除非确保您的许可证涵盖此类使用。

立即免费试用IronBarcode

体验IronBarcode的创新力量。 试用我们的免费试用版,发现无缝的条形码生成、读取和编辑功能,适用于您的.NET应用程序。 凭借高级功能、卓越性能和用户友好的界面,IronBarcode是满足您所有条码需求的终极解决方案。 立即开始免费试用,提升您的项目。

乔尔迪·巴尔迪亚
乔尔迪·巴尔迪亚
软件工程师
Jordi 最擅长 Python、C# 和 C++,当他不在 Iron Software 运用技能时,他会进行游戏编程。作为产品测试、产品开发和研究的负责人之一,Jordi 为持续的产品改进增添了极大的价值。多样化的经验让他充满挑战和参与感,他说这是他在 Iron Software 工作中最喜欢的方面之一。Jordi 在佛罗里达州迈阿密长大,并在佛罗里达大学学习计算机科学和统计学。
下一步 >
使用IronBarcode读取多个条形码:现场演示回顾