使用IRONBARCODE 使用.NET條碼閱讀器SDK Jordi Bardia 更新日期:8月 5, 2025 Download IronBarcode NuGet 下載 DLL 下載 Start Free Trial Copy for LLMs Copy for LLMs Copy page as Markdown for LLMs Open in ChatGPT Ask ChatGPT about this page Open in Gemini Ask Gemini about this page Open in Grok Ask Grok about this page Open in Perplexity Ask Perplexity about this page Share Share on Facebook Share on X (Twitter) Share on LinkedIn Copy URL Email article 條碼掃描是許多應用程式的重要功能,從庫存管理到零售和物流皆是如此。 通過將條碼讀取集成到您的 .NET 應用程式中,您可以簡化資料擷取、自動化工作流程並提高效率。 IronBarcode 是一個強大的 .NET 函式庫,使得使用條碼變得簡單。 利用此工具,您可以從影像、流和 PDF 文件中讀取條碼,還可以生成 QR 碼。 本文將向您展示如何將條碼掃描功能集成到您的 .NET 應用程式中,重點在於創建一個 API 或 Web 應用程式集成,從而展現條碼掃描功能。 IronBarcode 集成的最佳用途 IronBarcode 非常適合於: 庫存管理系統 – 自動化條碼掃描以跟蹤產品和庫存水平。 物流應用程式 – 處理掃描的條碼影像以便運輸、接收和包裹追蹤。 零售 POS 系統 – 驗證銷售交易和價格查詢的條碼掃描。 文件處理 – 從 PDF 發票、收據或身份證中提取條碼資料。 如何在 .NET 中創建條碼閱讀器 SDK 要創建一個可以作為服務公開的條碼閱讀器,您需要將 IronBarcode 集成到 REST API 或 Web 應用程式中。以下是使用 ASP.NET Core 創建此類應用程式的範例。 安裝用於在 C# 中讀取條碼的 .NET 函式庫 創建一個可重用的條碼掃描類。 開發從不同來源讀取條碼的方法。 將條碼影像讀取集成到您的應用程式中。 測試並優化效能。 開始之前 如果您還沒有,請下載 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"; } // Method to read a barcode from an image file public string ReadBarcodeFromImage(string imagePath) { try { // Try to read the barcode from the given image path var barcode = BarcodeReader.Read(imagePath); return barcode?.ToString() ?? "No Barcode Found"; // Return the barcode string or indicate no barcode was found } catch (Exception ex) { // Return an error message if an exception occurs return $"Error reading barcode: {ex.Message}"; } } // Method to read a barcode from a stream (e.g., file upload or memory stream) public string ReadBarcodeFromStream(Stream inputStream) { try { // Try to read the barcode from the given stream var barcode = BarcodeReader.Read(inputStream); return barcode?.ToString() ?? "No barcode found"; } catch (Exception ex) { return $"Error reading barcode: {ex.Message}"; } } // Method to read a barcode from a PDF file public string ReadBarcodeFromPdf(string filePath) { try { // Try to read the barcode from the given PDF file path 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"; } // Method to read a barcode from an image file public string ReadBarcodeFromImage(string imagePath) { try { // Try to read the barcode from the given image path var barcode = BarcodeReader.Read(imagePath); return barcode?.ToString() ?? "No Barcode Found"; // Return the barcode string or indicate no barcode was found } catch (Exception ex) { // Return an error message if an exception occurs return $"Error reading barcode: {ex.Message}"; } } // Method to read a barcode from a stream (e.g., file upload or memory stream) public string ReadBarcodeFromStream(Stream inputStream) { try { // Try to read the barcode from the given stream var barcode = BarcodeReader.Read(inputStream); return barcode?.ToString() ?? "No barcode found"; } catch (Exception ex) { return $"Error reading barcode: {ex.Message}"; } } // Method to read a barcode from a PDF file public string ReadBarcodeFromPdf(string filePath) { try { // Try to read the barcode from the given PDF file path 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 ' Method to read a barcode from an image file Public Function ReadBarcodeFromImage(ByVal imagePath As String) As String Try ' Try to read the barcode from the given image path Dim barcode = BarcodeReader.Read(imagePath) Return If(barcode?.ToString(), "No Barcode Found") ' Return the barcode string or indicate no barcode was found Catch ex As Exception ' Return an error message if an exception occurs Return $"Error reading barcode: {ex.Message}" End Try End Function ' Method to read a barcode from a stream (e.g., file upload or memory stream) Public Function ReadBarcodeFromStream(ByVal inputStream As Stream) As String Try ' Try to read the barcode from the given stream 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 ' Method to read a barcode from a PDF file Public Function ReadBarcodeFromPdf(ByVal filePath As String) As String Try ' Try to read the barcode from the given PDF file path 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 BarcodeIntegration; [ApiController] [Route("api/barcode")] public class BarcodeController : ControllerBase { private readonly BarcodeScanner _barcodeScanner; public BarcodeController() { // Initialize the BarcodeScanner class _barcodeScanner = new BarcodeScanner(); } // POST endpoint to read barcode from an uploaded image [HttpPost("read-from-image")] public IActionResult ReadFromImage(IFormFile file) { using var stream = file.OpenReadStream(); var result = _barcodeScanner.ReadBarcodeFromStream(stream); return Ok(new { Barcode = result }); // Return the barcode reading result } } using Microsoft.AspNetCore.Mvc; using System.IO; using Microsoft.AspNetCore.Http; using BarcodeIntegration; [ApiController] [Route("api/barcode")] public class BarcodeController : ControllerBase { private readonly BarcodeScanner _barcodeScanner; public BarcodeController() { // Initialize the BarcodeScanner class _barcodeScanner = new BarcodeScanner(); } // POST endpoint to read barcode from an uploaded image [HttpPost("read-from-image")] public IActionResult ReadFromImage(IFormFile file) { using var stream = file.OpenReadStream(); var result = _barcodeScanner.ReadBarcodeFromStream(stream); return Ok(new { Barcode = result }); // Return the barcode reading result } } Imports Microsoft.AspNetCore.Mvc Imports System.IO Imports Microsoft.AspNetCore.Http Imports BarcodeIntegration <ApiController> <Route("api/barcode")> Public Class BarcodeController Inherits ControllerBase Private ReadOnly _barcodeScanner As BarcodeScanner Public Sub New() ' Initialize the BarcodeScanner class _barcodeScanner = New BarcodeScanner() End Sub ' POST endpoint to read barcode from an uploaded image <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}) ' Return the barcode reading result End Function End Class $vbLabelText $csharpLabel Swagger UI 輸出 此 API 揭露一個 POST 端點,可以上傳條碼影像,並且 API 將返回條碼數據。 這允許其他系統或前端應用程序與您的條碼掃描器進行交互,將其作為一項服務。 進階功能 為了進一步增強 SDK,可考慮增加以下內容: 支持多種條碼類型: 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 錯誤處理: 擴展錯誤處理和日志記錄,以為您的 SDK 使用者提供更好的診斷。 批處理:如果您想從一系列影像中處理多個條碼,可以創建接收影像或流數據列數組的方法。 定制化:提供條碼檢測參數的配置選項,例如精確度、速度和支持的條碼類型。 授權考量 如前所述,IronBarcode SDK 是用於集成到內部應用程式中的,通過 API 揭露它需要額外的授權。 在將 IronBarcode 作為服務的一部分公開之前,如公共 API,您必須獲得必要的授權(SDK、OEM 或 SaaS)。 請勿將 IronBarcode 作為獨立 SDK 轉售或者通過公共 API 揭露,而不確保您的授權涵蓋該用法。 立即試用 IronBarcode 體驗 IronBarcode 的創新力量。 試用我們的免費試用版並發現無縫的條碼生成、讀取和編輯功能,適用於您的 .NET 應用程式。 具有高階功能、卓越的性能和使用者友好的介面,IronBarcode 是為您所有條碼需求提供的終極解決方案。 立即開始免費試用,提升您的專案。 常見問題解答 如何將條碼讀取器集成到 .NET 應用程式中? 您可以使用 IronBarcode 庫將條碼讀取器集成到 .NET 應用程式中。首先,安裝 IronBarcode,然後創建條碼掃描類,並實現從圖像、流和 PDF 閱讀條碼的方法。最後,測試並優化您的設置。 如何將條碼閱讀功能公開為 REST API? 要將條碼閱讀功能公開為 REST API,使用 ASP.NET Core 創建一個 Web 應用程式。整合 IronBarcode 庫,開發 BarcodeScanner 類,並定義使用 ReadBarcodeFromImage 和 ReadBarcodeFromStream 等方法讀取條碼的 API 端點。 使用 .NET 條碼庫可以讀取哪些條碼類型? 如 IronBarcode 的 .NET 條碼庫可以讀取各種條碼類型,包括 QR 碼,Code 128,UPC,和 EAN。您可以通過設置檢測參數來配置庫同時檢測多種條碼類型。 如何在 .NET 中處理讀取條碼時的錯誤? 可以通過在您的條碼掃描方法中實施強大的錯誤處理來處理條碼閱讀中的錯誤,使用 IronBarcode。確保捕捉異常並提供有意義的反饋或重試機制,提升條碼閱讀過程的可靠性。 使用 .NET 條碼庫建構公共 API 的許可要求是什麼? 在公共 API 中使用 IronBarcode 時,必須確保適當的許可權。這包括獲取 SDK、OEM 或 SaaS 許可,因為將庫的功能作為獨立服務或公共 API 曝光需要額外的許可。 我可以使用 .NET 庫批量處理多個條碼掃描嗎? 可以,您可以使用 IronBarcode 批量處理多個條碼掃描。該庫允許您在一個操作中讀取多個條碼,這在有效處理大量圖像或文件時特別有用。 是否有 .NET 條碼庫的試用版本? 有的,IronBarcode 提供免費試用版,讓您能夠探索其在 .NET 應用程式中生成、閱讀和編輯條碼的能力。這個試用版可以幫助您在做購買決定前評估該庫。 Jordi Bardia 立即與工程團隊聊天 軟體工程師 Jordi 在 Python、C# 和 C++ 上最得心應手,當他不在 Iron Software 展現技術時,便在做遊戲編程。在分担产品测测试,产品开发和研究的责任时,Jordi 为持续的产品改进增值。他说这种多样化的经验使他受到挑战并保持参与, 而这也是他与 Iron Software 中工作一大乐趣。Jordi 在佛罗里达州迈阿密长大,曾在佛罗里达大学学习计算机科学和统计学。 相關文章 發表日期 10月 19, 2025 如何使用VB.NET在Crystal Reports中打印條碼 在VB.NET中使用IronBarcode SDK在Crystal Reports中生成和打印條碼的分步教程,確保可靠的條碼集成。 閱讀更多 發表日期 9月 29, 2025 IronBarcode對比.NET中的開源條碼閱讀器 了解如何使用IronBarcode在C#中讀取條碼 閱讀更多 發表日期 9月 29, 2025 如何在ASP.NET應用程式中掃描條碼 了解如何在ASP.NET中使用IronBarcode掃描條碼 閱讀更多 如何使用IronBarcode創建MAUI條碼掃描器如何使用IronBarcode讀取多個...
發表日期 10月 19, 2025 如何使用VB.NET在Crystal Reports中打印條碼 在VB.NET中使用IronBarcode SDK在Crystal Reports中生成和打印條碼的分步教程,確保可靠的條碼集成。 閱讀更多