在生產環境中測試,無水印。
在任何需要的地方都能運行。
獲得 30 天的全功能產品。
在幾分鐘內上手運行。
試用產品期間完全訪問我們的支援工程團隊
條碼掃描是許多應用程式的重要功能,從庫存管理到零售和物流。 透過將條碼讀取整合到您的 .NET 應用程式中,您可以簡化資料擷取、流程自動化,並提高效率。
IronBarcode 是一個強大的 .NET 函式庫,使條碼工作變得簡單。 使用這個工具,您可以從圖像、流和 PDF 文件中讀取條碼,還可以生成 QR 碼。 這篇文章將向您展示如何將條碼掃描整合到您的.NET應用程式中,著重於建立API或網頁應用程式整合以提供條碼掃描功能。
IronBarcode 非常適合:
要創建可以作為服務暴露在您的應用程式中的條碼閱讀器,您需要將IronBarcode整合到REST API或Web App中。以下是如何使用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 擁有先進的功能、卓越的性能和使用者友好的介面,是滿足您所有條碼需求的終極解決方案。 立即開始您的免費試用,提升您的專案。