使用IRONBARCODE 如何使用IronBarcode創建MAUI條碼掃描器 Jordi Bardia 發表日期:9月 29, 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 現代移動應用程式越來越依賴條碼掃描進行庫存管理、銷售點系統和產品追踪。 構建 MAUI 條碼掃描器允許您將條碼檢測直接整合到您的 .NET MAUI 應用程式中,將攝影機畫面與影像檔案處理結合起來,以檢測 QR 碼、Data Matrix 和其他條碼格式。 雖然許多庫側重於相機預覽,但 IronBarcode 擅長於準確地閱讀條碼並在困難條件下掃描它們。 在本指南中,我將向您展示如何在 .NET MAUI 專案中使用 IronBarcode 實現條碼掃描功能。 到最後,您將能夠在一個圖片文件或裝置的相機中掃描多個條碼,從任何給定的條碼中讀取資料,並在自己的 MAUI 專案中自信地使用 IronBarcode。 構建 MAUI 條碼掃描器的先決條件是什麼? 在開始之前,確保您已安裝 Visual Studio Code 或 Visual Studio 2022 以及 .NET MAUI 工作負載,並擁有基本的 C# 知識。 您還需要 .NET 8 SDK 以獲得最佳性能和跨平台應用程式支持。 如何設置 MAUI 條碼掃描專案? 在 Visual Studio 2022 中建立一個新的 .NET MAUI 應用專案。將其命名為“BarcodeScannerApp”並選擇 .NET 8 作為目標框架。 通過 NuGet 套件管理控制台安裝 IronBarcode: Install-Package BarCode 這會安裝一個支持多種條碼格式的 .NET 庫,包括 QR 碼、Code 128 和數據矩陣。 IronBarcode 可離線運行並處理本地未支持的反相條碼,確保持續掃描和更好的掃描一致性。 要啟用 IronBarcode,獲取一個免費試用許可證並將其添加到您的代碼中: License.LicenseKey = "YOUR-LICENSE-KEY"; License.LicenseKey = "YOUR-LICENSE-KEY"; IRON VB CONVERTER ERROR developers@ironsoftware.com $vbLabelText $csharpLabel 如何配置 Android 和 iOS 的攝像頭權限? 平台特定的攝像頭權限對於條碼掃描功能至關重要。 每個平台都需要在其清單檔案中進行特定配置。 對於 Android,編輯 Platforms/Android/AndroidManifest.xml: <uses-permission android:name="android.permission.CAMERA" /> <uses-feature android:name="android.hardware.camera" /> <uses-permission android:name="android.permission.CAMERA" /> <uses-feature android:name="android.hardware.camera" /> IRON VB CONVERTER ERROR developers@ironsoftware.com $vbLabelText $csharpLabel 這些權限啟用相机访问并声明相机硬件的使用,确保您的应用程序能够在 Android 设备上捕获条码图像。 对于 iOS,更改 Platforms/iOS/Info.plist: <key>NSCameraUsageDescription</key> <string>This app requires camera access to scan barcodes</string> <key>NSCameraUsageDescription</key> <string>This app requires camera access to scan barcodes</string> IRON VB CONVERTER ERROR developers@ironsoftware.com $vbLabelText $csharpLabel 此配置提供 iOS 在请求用户的相机权限时显示的所需隐私说明。 如何创建条码扫描器界面? 在 MainPage.xaml 中设计一个简单、用户友好的界面: <?xml version="1.0" encoding="utf-8" ?> <ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="BarcodeScannerApp.MainPage"> <VerticalStackLayout Padding="20" Spacing="20"> <Label Text="Barcode Scanner" FontSize="24" HorizontalOptions="Center" /> <Image x:Name="CapturedImage" HeightRequest="300" Aspect="AspectFit" /> <Label x:Name="ResultLabel" Text="Tap button to scan" HorizontalOptions="Center" /> <Button Text="Scan Barcode" Clicked="OnScanClicked" BackgroundColor="#007ACC" TextColor="White" /> </VerticalStackLayout> </ContentPage> <?xml version="1.0" encoding="utf-8" ?> <ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="BarcodeScannerApp.MainPage"> <VerticalStackLayout Padding="20" Spacing="20"> <Label Text="Barcode Scanner" FontSize="24" HorizontalOptions="Center" /> <Image x:Name="CapturedImage" HeightRequest="300" Aspect="AspectFit" /> <Label x:Name="ResultLabel" Text="Tap button to scan" HorizontalOptions="Center" /> <Button Text="Scan Barcode" Clicked="OnScanClicked" BackgroundColor="#007ACC" TextColor="White" /> </VerticalStackLayout> </ContentPage> IRON VB CONVERTER ERROR developers@ironsoftware.com $vbLabelText $csharpLabel 该布局创建了一个包含图像预览区域、结果展示标签和扫描按钮的简单界面。 VerticalStackLayout 提供一致的间距和填充,使它外观专业。 如何实现条码读取功能? 利用 IronBarcode 的图像处理能力在 MainPage.xaml.cs 中实现扫描逻辑: using IronBarCode; using IronSoftware.Drawing; namespace BarcodeScannerApp; public partial class MainPage : ContentPage { public MainPage() { InitializeComponent(); License.LicenseKey = "YOUR-LICENSE-KEY"; } private async void OnScanClicked(object sender, EventArgs e) { try { // Capture photo using device camera var photo = await MediaPicker.Default.CapturePhotoAsync(); if (photo == null) return; // Convert photo to byte array using var stream = await photo.OpenReadAsync(); using var memoryStream = new MemoryStream(); await stream.CopyToAsync(memoryStream); var imageBytes = memoryStream.ToArray(); // Display captured image CapturedImage.Source = ImageSource.FromStream(() => new MemoryStream(imageBytes)); // Process with IronBarcode var bitmap = AnyBitmap.FromBytes(imageBytes); var results = await BarcodeReader.ReadAsync(bitmap); // Display results if (results.Any()) { var barcodeValue = results.First().Value; ResultLabel.Text = $"Scanned: {barcodeValue}"; } else { ResultLabel.Text = "No barcode detected"; } } catch (Exception ex) { await DisplayAlert("Error", $"Scanning failed: {ex.Message}", "OK"); } } } using IronBarCode; using IronSoftware.Drawing; namespace BarcodeScannerApp; public partial class MainPage : ContentPage { public MainPage() { InitializeComponent(); License.LicenseKey = "YOUR-LICENSE-KEY"; } private async void OnScanClicked(object sender, EventArgs e) { try { // Capture photo using device camera var photo = await MediaPicker.Default.CapturePhotoAsync(); if (photo == null) return; // Convert photo to byte array using var stream = await photo.OpenReadAsync(); using var memoryStream = new MemoryStream(); await stream.CopyToAsync(memoryStream); var imageBytes = memoryStream.ToArray(); // Display captured image CapturedImage.Source = ImageSource.FromStream(() => new MemoryStream(imageBytes)); // Process with IronBarcode var bitmap = AnyBitmap.FromBytes(imageBytes); var results = await BarcodeReader.ReadAsync(bitmap); // Display results if (results.Any()) { var barcodeValue = results.First().Value; ResultLabel.Text = $"Scanned: {barcodeValue}"; } else { ResultLabel.Text = "No barcode detected"; } } catch (Exception ex) { await DisplayAlert("Error", $"Scanning failed: {ex.Message}", "OK"); } } } IRON VB CONVERTER ERROR developers@ironsoftware.com $vbLabelText $csharpLabel 该实现通过 MediaPicker 捕获图像,将其转换为字节数组以进行处理,并使用 IronBarcode 的 BarcodeReader.ReadAsync 方法进行检测。 AnyBitmap.FromBytes 方法可自動處理各種圖像格式。 錯誤處理確保以用戶友好的消息優雅地進行失敗恢復。 通過此代碼,我們可以掃描此條碼: 而且您應該能夠在屏幕上看到條碼的數據顯示: IronBarcode 提供哪些高级功能? IronBarcode 提供多項高級功能以增強掃描可靠性。 該庫的機器學習算法自動調整信心阈值,提高在困難條件下的準確性。 圖像校正濾波器在不需要其他配置的情况下,處理旋轉、歪斜或光線不佳的條碼。 針對特定掃描需求,定制讀取選項: var options = new BarcodeReaderOptions { Speed = ReadingSpeed.Balanced, ExpectMultipleBarcodes = true, ExpectBarcodeTypes = BarcodeEncoding.QRCode | BarcodeEncoding.Code128 }; var results = await BarcodeReader.ReadAsync(bitmap, opcanbvations); var options = new BarcodeReaderOptions { Speed = ReadingSpeed.Balanced, ExpectMultipleBarcodes = true, ExpectBarcodeTypes = BarcodeEncoding.QRCode | BarcodeEncoding.Code128 }; var results = await BarcodeReader.ReadAsync(bitmap, opcanbvations); IRON VB CONVERTER ERROR developers@ironsoftware.com $vbLabelText $csharpLabel 該配置優化對特定條碼類型的掃描,並啟用在單個圖像中檢測多個條碼,減少處理時間同時保持準確性。 !如何使用 IronBarcode 創建 MAUI 條碼掃描器:圖 4 - 同一圖像中掃描的多個代碼 常见故障排除提示 即使擁有強大的 MAUI 條碼掃描器設置,問題偶爾也會因為設備設置、圖像質量或平台特定的限制而出現。 以下提示解決了實施 .NET MAUI 應用程式中的條碼掃描功能時遇到的最常見問題: 相機無法開啟: 確保在清单节点中正确设置了以下权限,並重新部署應用程式。此外,如果您正在多攝相機裝置或自定義拍侒設置下測試,請檢查支持的多攝影機設置。 扫描结果差:调整图像分辨率或光照,或对条码应用变换。 此外,启用持续扫描并增加 BarcodeReaderOptions 的速度选项可以提高扫描一致性。 记忆问题:使用using语句正确处理图像流。 這確保您的條碼掃描功能保持響應並防止在Android設備或Microsoft Windows机器上意外崩潰或變慢。 iOS问题: 确认 Info.plist 包含适当的 QR 代码可绑定属性。 建议在 iPhone 和 iPad 设备上进行测试,以确保在 iOS 设备上具有一致的条码扫描功能。 結論 IronBarcode 通过其强大的图像处理引擎和机器学习能力变革 MAUI 条码扫描。 与实时相机预览库不同,IronBarcode 的方法确保在困难条件下可靠扫描。 該庫的離線功能和豐富的格式支持使其成為企業應用程式的理想選擇。 現在,您可以自信地使用今天所教的内容创建自己的 .NET MAUI 条码扫描器。 还想了解更多? 一定要阅读 IronBarcodes 的详细文档。 使用 免费试用进行生产部署开始开发。 IronBarcode 凭借其準確性、易实现性和全面的功能组合,成为 MAUI 条码扫描应用程序的最佳选择。 常見問題解答 在 MAUI 條碼掃描器中使用 IronBarcode 的優勢是什麼? IronBarcode 擅長準確讀取條碼,甚至在挑戰條件下進行掃描,使其成為在 MAUI 應用程式中集成的理想選擇。 IronBarcode 可以在 MAUI 應用程式中處理多種條碼格式嗎? 是的,IronBarcode 可以檢測多種條碼格式,包括 QR 碼和數據矩陣,使其適用於不同的應用程式需求。 IronBarcode 如何在 MAUI 條碼掃描器中與相機輸入集成? IronBarcode 允許與相機輸入無縫集成,直接在 MAUI 應用程式中進行即時條碼檢測和處理。 IronBarcode 支持在 MAUI 應用程式中離線條碼掃描嗎? 是的,IronBarcode 支持離線條碼掃描,使其在沒有互聯網連接的情況下也能運行,這對於移動應用程式非常有利。 IronBarcode 用於 MAUI 中條碼掃描的關鍵特點是什麼? 關鍵特點包括準確條碼識別、支持多種條碼格式,即使在挑戰條件下也能保證強大的性能。 IronBarcode 如何處理挑戰性的掃描條件? IronBarcode 被設計用於在挑戰条件下(如光線不足或角度偏斜)準確讀取條碼,確保可靠性。 在 MAUI 中使用 IronBarcode 設置條碼掃描權限容易嗎? 是的,IronBarcode 提供了設置條碼掃描所需權限的清晰指導,使在 MAUI 中實施變得直截了當。 IronBarcode 可以用於 MAUI 中的庫存管理應用程式嗎? 絕對可以,IronBarcode 十分適合庫存管理,提供了高效的條碼掃描功能,可以集成到 MAUI 應用程式中。 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在C#中構建條碼掃描器API使用.NET條碼閱讀器SDK
發表日期 10月 19, 2025 如何使用VB.NET在Crystal Reports中打印條碼 在VB.NET中使用IronBarcode SDK在Crystal Reports中生成和打印條碼的分步教程,確保可靠的條碼集成。 閱讀更多