使用IRONBARCODE 如何在ASP.NET應用程式中掃描條碼 Jordi Bardia 發表日期:9月 29, 2025 下載 IronBarcode NuGet 下載 DLL 下載 開始免費試用 法學碩士副本 法學碩士副本 將頁面複製為 Markdown 格式,用於 LLMs 在 ChatGPT 中打開 請向 ChatGPT 諮詢此頁面 在雙子座打開 請向 Gemini 詢問此頁面 在雙子座打開 請向 Gemini 詢問此頁面 打開困惑 向 Perplexity 詢問有關此頁面的信息 分享 在 Facebook 上分享 分享到 X(Twitter) 在 LinkedIn 上分享 複製連結 電子郵件文章 條碼掃描已成為現代網路應用程式中不可或缺的功能,為從庫存管理系統到文件處理工作流程等各個方面提供支援。 無論您是在倉庫中追蹤產品、在活動中處理門票,還是將紙本文件數位化,在您的 ASP.NET Web 應用程式中實施可靠的條碼掃描都可以顯著提高效率並減少錯誤。 IronBarcode 已成為 ASP.NET 和 Web 開發人員的首選 C# 條碼讀取器庫,為讀取和產生條碼提供了強大而簡單的解決方案。 與其他需要複雜配置或難以處理真實影像的 ASP.NET 條碼掃描器庫不同,IronBarcode 能夠可靠、自信地提供準確的條碼掃描結果。 其跨平台相容性確保您的 Web 應用程式無論部署在 Windows、Linux 還是雲端容器上都能無縫運行,而其機器學習驅動的條碼檢測功能,即使是最具挑戰性的條碼影像,也能透過將其轉換為機器可讀格式來自信地處理。 如何在 ASP.NET 中將 IronBarcode 設為條碼讀取器? 在 .NET 專案中開始使用 IronBarcode 只需幾分鐘。 該程式庫同時支援 ASP.NET Core 和傳統的 ASP.NET MVC 應用程序,使其能夠靈活應用於各種類型的專案。 首先,使用 NuGet 套件管理器控制台安裝 IronBarcode: Install-Package BarCode 或者,您也可以透過 Visual Studio 的 NuGet 套件管理器 UI 進行安裝,方法是搜尋"IronBarCode"並按一下"安裝"。 該軟體包會自動管理所有依賴項,確保順利整合。 有關詳細的安裝指導,請查看IronBarcode 安裝指南。 安裝完成後,將必要的 using 語句新增至您的 C# 條碼讀取器檔案: using IronBarCode; using IronBarCode; Imports IronBarCode $vbLabelText $csharpLabel 透過簡單的匯入操作,您可以存取 IronBarcode 全面的條碼讀取和產生功能。 該程式庫支援 30 多種條碼格式,包括 QR 碼產生、Code 128、Code 39、Data Matrix 和 PDF417,幾乎涵蓋了生產環境中所有條碼類型。 根據微軟關於 ASP.NET 的文檔,正確的套件管理對於維護安全且高效的 Web 應用程式至關重要。 如何實現文件上傳條碼掃描? 在 ASP.NET Web 應用程式中,最常見的條碼掃描場景是使用者在 Web 瀏覽器中上傳包含條碼的圖像檔案。 此方案非常適合處理發票、貨運標籤或任何帶有嵌入式條碼的文件。 在 ASP.NET 視圖中使用div元素建立一個簡單的 HTML 表單: <form method="post" enctype="multipart/form-data"> <div class="form-group"> <label for="barcodeFile">Select Barcode Image:</label> <input type="file" name="barcodeFile" id="barcodeFile" accept="image/*,.pdf" class="form-control" /> </div> <button type="submit" class="btn btn-primary">Scan Barcode</button> </form> <div id="results"> @ViewBag.BarcodeResult </div> <form method="post" enctype="multipart/form-data"> <div class="form-group"> <label for="barcodeFile">Select Barcode Image:</label> <input type="file" name="barcodeFile" id="barcodeFile" accept="image/*,.pdf" class="form-control" /> </div> <button type="submit" class="btn btn-primary">Scan Barcode</button> </form> <div id="results"> @ViewBag.BarcodeResult </div> IRON VB CONVERTER ERROR developers@ironsoftware.com $vbLabelText $csharpLabel 現在實作後端控制器,使用 ASP.NET 條碼讀取器處理上傳的檔案: [HttpPost] public async Task<IActionResult> ScanBarcode(IFormFile barcodeFile) { if (barcodeFile != null && barcodeFile.Length > 0) { using (var stream = new MemoryStream()) { await barcodeFile.CopyToAsync(stream); stream.Position = 0; // Read barcode from the uploaded image var results = BarcodeReader.Read(stream); if (results.Any()) { ViewBag.BarcodeResult = string.Join(", ", results.Select(r => $"{r.BarcodeType}: {r.Text}")); } else { ViewBag.BarcodeResult = "No barcodes found in the image."; } } } return View(); } [HttpPost] public async Task<IActionResult> ScanBarcode(IFormFile barcodeFile) { if (barcodeFile != null && barcodeFile.Length > 0) { using (var stream = new MemoryStream()) { await barcodeFile.CopyToAsync(stream); stream.Position = 0; // Read barcode from the uploaded image var results = BarcodeReader.Read(stream); if (results.Any()) { ViewBag.BarcodeResult = string.Join(", ", results.Select(r => $"{r.BarcodeType}: {r.Text}")); } else { ViewBag.BarcodeResult = "No barcodes found in the image."; } } } return View(); } IRON VB CONVERTER ERROR developers@ironsoftware.com $vbLabelText $csharpLabel 該實作透過將上傳的文件複製到記憶體流來處理該文件,然後使用 IronBarcode 的 BarcodeReader.Read 方法從圖像中提取所有條碼。 此方法可自動偵測條碼格式並傳回詳細結果,包括條碼類型和解碼文字。 IronBarcode 可以處理各種影像格式,包括 JPEG、PNG、GIF、TIFF、BMP 甚至PDF 文檔,而無需編寫特定格式的處理代碼。 這種多功能性使其成為Stack Overflow 條碼實現主題中討論的文檔處理場景的理想選擇。 範例影像 如何在 ASP.NET 應用程式中掃描條碼:圖 8 - 已準備好掃描的 Code128 條碼 輸出 如何在 ASP.NET 應用程式中掃描條碼:圖 5 - 帶有條碼類型和文字的程式碼輸出 如何建立用於條碼或二維碼掃描的 REST API? 現代 ASP.NET Web 應用程式通常需要透過 REST API 公開條碼掃描功能,以便能夠與行動應用程式、SPA 或第三方服務整合。 以下是如何使用 ASP.NET Core 建立強大的條碼掃描器 API: [ApiController] [Route("api/[controller]")] public class BarcodeController : ControllerBase { [HttpPost("scan")] public IActionResult ScanBarcode([FromBody] BarcodeRequest request) { try { // Convert base64 string to byte array byte[] imageBytes = Convert.FromBase64String(request.ImageBase64); // Read barcodes from the image var results = BarcodeReader.Read(imageBytes); var response = results.Select(r => new { type = r.BarcodeType.ToString(), value = r.Text, position = new { x = r.Points.Select(b => b.X).Min(), y= r.Points.Select(b => b.Y).Min(), r.Width, r.Height } }).ToList(); return Ok(new { success = true, barcodes = response }); } catch (Exception ex) { return BadRequest(new { success = false, error = ex.Message }); } } } public class BarcodeRequest { public string ImageBase64 { get; set; } } [ApiController] [Route("api/[controller]")] public class BarcodeController : ControllerBase { [HttpPost("scan")] public IActionResult ScanBarcode([FromBody] BarcodeRequest request) { try { // Convert base64 string to byte array byte[] imageBytes = Convert.FromBase64String(request.ImageBase64); // Read barcodes from the image var results = BarcodeReader.Read(imageBytes); var response = results.Select(r => new { type = r.BarcodeType.ToString(), value = r.Text, position = new { x = r.Points.Select(b => b.X).Min(), y= r.Points.Select(b => b.Y).Min(), r.Width, r.Height } }).ToList(); return Ok(new { success = true, barcodes = response }); } catch (Exception ex) { return BadRequest(new { success = false, error = ex.Message }); } } } public class BarcodeRequest { public string ImageBase64 { get; set; } } IRON VB CONVERTER ERROR developers@ironsoftware.com $vbLabelText $csharpLabel 此條碼讀取器 API 端點接受 base64 編碼的映像,這是一種用於透過 HTTP 傳輸影像的標準格式。回應包含條碼值及其類型。 該實現遵循RESTful 最佳實踐,確保與任何前端框架無縫整合。 以下 JavaScript 程式碼用於從 JavaScript 用戶端呼叫此 API: async function scanBarcode(imageFile) { const base64 = await convertToBase64(imageFile); const response = await fetch('/api/barcode/scan', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ imageBase64: base64 }) }); const result = await response.json(); console.log('Scanned barcodes:', result.barcodes); } async function convertToBase64(file) { return new Promise((resolve, reject) => { const reader = new FileReader(); reader.onload = () => { // Remove the data URL prefix to get only the base64 string const base64 = reader.result.split(',')[1]; resolve(base64); }; reader.onerror = error => reject(error); reader.readAsDataURL(file); }); } async function scanBarcode(imageFile) { const base64 = await convertToBase64(imageFile); const response = await fetch('/api/barcode/scan', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ imageBase64: base64 }) }); const result = await response.json(); console.log('Scanned barcodes:', result.barcodes); } async function convertToBase64(file) { return new Promise((resolve, reject) => { const reader = new FileReader(); reader.onload = () => { // Remove the data URL prefix to get only the base64 string const base64 = reader.result.split(',')[1]; resolve(base64); }; reader.onerror = error => reject(error); reader.readAsDataURL(file); }); } IRON VB CONVERTER ERROR developers@ironsoftware.com $vbLabelText $csharpLabel 這種 API 方法可以與現代 JavaScript 框架、行動應用程式無縫集成,並支援條碼掃描需要非同步或批次操作的場景。 範例輸入 如何在 ASP.NET 應用程式中掃描條碼:圖 6 - 多個條碼 輸出 如何在 ASP.NET 應用程式中掃描條碼:圖 7 - API 回應 如何處理複雜的條碼影像? 在 ASP.NET Web 應用程式中,實際的條碼掃描經常會遇到影像不完美的情況:例如,拍攝角度不當、光線不足或條碼部分損壞等。 IronBarcode憑藉其先進的影像處理能力,在這些場景中表現出色: var options = new BarcodeReaderOptions { // Balance speed vs accuracy Speed = ReadingSpeed.Balanced, // Specify expected barcode types for better performance ExpectBarcodeTypes = BarcodeEncoding.QRCode | BarcodeEncoding.Code128, // Enable automatic rotation correction AutoRotate = true, // Apply image filters for clarity ImageFilters = new ImageFilterCollection { new SharpenFilter(), new ContrastFilter(1.5f) }, // Use multiple threads for faster processing Multithreaded = true }; var results = BarcodeReader.Read("challenging-image.jpg", options); var options = new BarcodeReaderOptions { // Balance speed vs accuracy Speed = ReadingSpeed.Balanced, // Specify expected barcode types for better performance ExpectBarcodeTypes = BarcodeEncoding.QRCode | BarcodeEncoding.Code128, // Enable automatic rotation correction AutoRotate = true, // Apply image filters for clarity ImageFilters = new ImageFilterCollection { new SharpenFilter(), new ContrastFilter(1.5f) }, // Use multiple threads for faster processing Multithreaded = true }; var results = BarcodeReader.Read("challenging-image.jpg", options); IRON VB CONVERTER ERROR developers@ironsoftware.com $vbLabelText $csharpLabel BarcodeReaderOptions類別提供了對條碼掃描過程的精細控制。 將"自動旋轉"設定為"真"可以處理以任何角度拍攝的影像,而影像濾鏡可以增強模糊或低對比度條碼的清晰度。 Speed 屬性可讓您根據特定的 ASP.NET 應用程式要求,在處理速度和準確性之間取得平衡。 對於大批量處理,啟用多執行緒可以利用所有可用的 CPU 核心,從而顯著提高效能。 這種方法符合 .NET 應用程式中影像處理的行業最佳實踐。 結論與最佳實務 在 ASP.NET Web 應用程式中使用 IronBarcode 實作條碼掃描,可以將可能很複雜的任務轉換為簡單、易於維護的程式碼。 該庫能夠處理多種格式、處理不完美的圖像、解碼條碼,並在各個平台上提供一致的結果,使其成為企業應用程式的寶貴工具。 對於生產環境部署,請記住要實施適當的錯誤處理,驗證上傳檔案的安全性,並考慮快取經常掃描的條碼。 IronBarcode 的跨平台支援可確保您的條碼掃描器解決方案能夠在 Docker 容器和雲端環境中無縫運行,從而提供現代應用程式所需的靈活性。 瀏覽完整的API 文檔,了解批次處理和PDF 條碼提取等高級功能。 準備好利用專業的條碼掃描功能徹底革新您的 ASP.NET 應用程式了嗎? 立即開始免費試用,在生產環境中充分發揮 IronBarcode 的全部潛力。 常見問題解答 在 ASP.NET 應用程式中,條碼掃描的主要用途是什麼? ASP.NET 應用程式中的 BarCode 掃描主要用於加強庫存管理系統、處理活動門票以及將紙張文件數位化,從而提高效率並減少錯誤。 IronBarcode 如何促進 ASP.NET 中的條碼掃描? IronBarcode 簡化了 ASP.NET 中的條碼掃描流程,它提供可靠高效的元件,可以輕鬆地整合到 Web 應用程式中,讓開發人員快速實現掃描功能。 使用 IronBarcode 可以掃描哪些類型的 BarCode? IronBarcode 支援掃描多種條碼格式,包括傳統的線性條碼和現代的 2D 條碼,確保與不同應用程式的相容性。 IronBarcode 可以處理文件處理的條碼掃描嗎? 是的,IronBarcode 非常適合文件處理工作流程,它可以通過掃描嵌入的 BarCode 來數位化和組織紙質文件。 IronBarcode 適用於庫存管理系統嗎? IronBarcode 是庫存管理系統的絕佳選擇,因為它可以透過掃描條碼有效追蹤產品,從而簡化作業並減少錯誤。 整合 IronBarcode 如何改善活動門票處理? 透過整合 IronBarcode,活動門票處理變得無懈可擊,因為它可以快速掃描門票 BarCode,方便在活動中進行快速準確的入場管理。 在 ASP.NET 專案中使用 IronBarcode 有哪些優點? 在 ASP.NET 專案中使用 IronBarcode 具有多項優點,包括易於整合、支援多種條碼格式以及增強應用程式效能,因此可為條碼掃描需求提供強大的解決方案。 實施 IronBarcode 是否需要豐富的編碼知識? 不,IronBarcode 的設計是為了方便開發人員使用,使其只需最低限度的編碼知識即可在 ASP.NET 應用程式中輕鬆實現條碼掃描功能。 IronBarcode 可以用於行動網路應用程式嗎? 是的,IronBarcode 可以集成到移动网络应用程序中,实现移动条码扫描,增强 ASP.NET 项目的多功能性。 Jordi Bardia 立即與工程團隊聊天 軟體工程師 Jordi 在 Python、C# 和 C++ 上最得心應手,當他不在 Iron Software 展現技術時,便在做遊戲編程。在分担产品测测试,产品开发和研究的责任时,Jordi 为持续的产品改进增值。他说这种多样化的经验使他受到挑战并保持参与, 而这也是他与 Iron Software 中工作一大乐趣。Jordi 在佛罗里达州迈阿密长大,曾在佛罗里达大学学习计算机科学和统计学。 相關文章 發表日期 12月 18, 2025 IronBarcode 與開源條碼閱讀器 .NET 的對比 了解如何使用IronBarcode在C#中讀取條碼 閱讀更多 發表日期 12月 18, 2025 C# 資料矩陣產生器:IronBarcode 完整指南 數據矩陣生成器C#教程。學習如何使用IronBarcode創建ECC200數據矩陣條碼。簡單的2D條碼生成代碼示例。 閱讀更多 發表日期 10月 19, 2025 如何使用VB.NET在Crystal Reports中打印條碼 在VB.NET中使用IronBarcode SDK在Crystal Reports中生成和打印條碼的分步教程,確保可靠的條碼集成。 閱讀更多 IronBarcode對比.NET中的開源條碼閱讀器如何在C#中創建USB條碼掃描器
發表日期 12月 18, 2025 C# 資料矩陣產生器:IronBarcode 完整指南 數據矩陣生成器C#教程。學習如何使用IronBarcode創建ECC200數據矩陣條碼。簡單的2D條碼生成代碼示例。 閱讀更多
發表日期 10月 19, 2025 如何使用VB.NET在Crystal Reports中打印條碼 在VB.NET中使用IronBarcode SDK在Crystal Reports中生成和打印條碼的分步教程,確保可靠的條碼集成。 閱讀更多