使用 IRONBARCODE

使用 Barcode Reader SDK for .NET

喬迪·巴迪亞
喬迪·巴迪亞
2025年4月3日
分享:

條碼掃描是許多應用程式的重要功能,從庫存管理到零售和物流。 透過將條碼讀取整合到您的 .NET 應用程式中,您可以簡化資料擷取、流程自動化,並提高效率。

IronBarcode 是一個強大的 .NET 函式庫,使條碼工作變得簡單。 使用這個工具,您可以從圖像、流和 PDF 文件中讀取條碼,還可以生成 QR 碼。 這篇文章將向您展示如何將條碼掃描整合到您的.NET應用程式中,著重於建立API或網頁應用程式整合以提供條碼掃描功能。

IronBarcode 整合的最佳使用案例

IronBarcode 非常適合:

  • 庫存管理系統 – 自動化條碼掃描以追蹤產品和庫存水平。
  • 物流應用 – 處理掃描的條碼圖像以便運輸、接收和包裹追踪。
  • 零售 POS 系統 – 驗證銷售交易和價格查詢的條碼掃描。
  • 文件處理 – 從 PDF 發票、收據或身份證中提取條碼數據。

如何在 .NET 中建立條碼讀取 SDK

要創建可以作為服務暴露在您的應用程式中的條碼閱讀器,您需要將IronBarcode整合到REST API或Web App中。以下是如何使用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添加上傳

或將圖片拖放到此處

新增圖片替代文字

輸出

使用條碼閱讀器 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 發揮技能時,他會進行遊戲編程。他負責產品測試、產品開發和研究,為持續產品改進增添了巨大的價值。多樣化的經驗使他感到挑戰和投入,他說這是與 Iron Software 合作的最喜歡的方面之一。Jordi 在佛羅里達州邁阿密長大,並在佛羅里達大學學習計算機科學和統計學。
下一個 >
如何使用IronBarcode讀取多個條碼:現場演示回顧