在生产环境中测试,无水印。
随时随地满足您的需求。
获得30天的全功能产品。
几分钟内就能启动并运行。
在您的产品试用期间,全面访问我们的支持工程团队。
条码扫描是许多应用程序的重要功能,从库存管理到零售和物流。 通过将条形码读取集成到您的.NET应用程序中,您可以简化数据捕获、自动化工作流程并提高效率。
IronBarcode 是一个强大的 .NET 库,使处理条形码变得简单。 使用此工具,您可以从图像、流和PDF文件中读取条形码,并生成二维码。 本文将向您展示如何在您的.NET应用程序中集成条形码扫描功能,重点是创建API或Web应用程序集成以展示条形码扫描功能。
IronBarcode 非常适合:
要创建一个可以作为服务在您的应用程序中暴露的条码阅读器,您需要将IronBarcode集成到REST API或Web应用程序中。以下是使用ASP.NET Core实现这一目标的示例。
创建一个可重用的条形码扫描类。
开发从不同来源读取条形码的方法。
将条形码图像读取集成到您的应用程序中。
如果您还没有,请下载 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
此 BarcodeScanner 类抽象了 IronBarcode 的功能,使其易于集成到任何 .NET 应用程序中。 让我们来看一下这里包含的不同方法的详细分析:
以下每种方法都尝试从不同类型的输入中读取条形码:
ReadBarcodeFromPdf(string filePath):从 PDF 文件中读取条形码。
每个方法都会尝试读取条形码数据,并返回条形码字符串或错误信息(如果没有找到条形码或发生异常)。
要允许外部应用程序使用您的条码扫描功能,您可以使用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
从Pixabay添加上传
或拖放图像到此处
添加图片替代文本
从Pixabay添加上传
或拖放图像到此处
添加图片替代文本
此 API 提供一个 POST 端点,可以在其中上传条形码图像,API 将返回条形码数据。 这允许其他系统或前端应用程序与您的条形码扫描仪作为服务进行交互。
为了进一步增强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
错误处理:扩展错误处理和日志记录,为您的SDK用户提供更好的诊断。
批量处理:如果您想从一组图像中处理多个条形码,您可以创建接受图像数组或流的方法。
如前所述,IronBarcode SDK 旨在集成到您的内部应用程序中,通过 API 公开它需要额外的许可。 在将IronBarcode作为服务的一部分公开之前(例如公开API),您必须获得必要的许可(SDK、OEM或SaaS)。
不要将IronBarcode作为独立SDK进行转售,也不要通过面向公众的API公开它,除非确保您的许可证涵盖此类使用。
体验IronBarcode的创新力量。 试用我们的免费试用版,发现无缝的条形码生成、读取和编辑功能,适用于您的.NET应用程序。 凭借高级功能、卓越性能和用户友好的界面,IronBarcode是满足您所有条码需求的终极解决方案。 立即开始免费试用,提升您的项目。