IronBarcode 開始使用 .NET MAUI 條碼掃描器與讀取器 .NET MAUI條碼掃描器 Curtis Chau 更新:2025年8月20日 下載 IronBarcode NuGet 下載 DLL 下載 開始免費試用 LLM副本 LLM副本 將頁面複製為 Markdown 格式,用於 LLMs 在 ChatGPT 中打開 請向 ChatGPT 諮詢此頁面 在雙子座打開 請向 Gemini 詢問此頁面 在 Grok 中打開 向 Grok 詢問此頁面 打開困惑 向 Perplexity 詢問有關此頁面的信息 分享 在 Facebook 上分享 分享到 X(Twitter) 在 LinkedIn 上分享 複製連結 電子郵件文章 This article was translated from English: Does it need improvement? Translated View the article in English 介紹 .NET MAUI (.NET多平台應用程式 UI)是一個跨平台框架,可在單一程式碼庫中無縫建立跨平台應用程式。 例如,您可以在一個專案中輕鬆建立 Microsoft Windows、iOS 和 Android 應用程式。 它與其他平台、框架和程式庫的不同之處在於,它允許開發者社群在其專案中使用原生控件,並為他們提供額外的元件。 因此,開發人員可以使用這些預製元件和服務更快地建立應用程序,而無需從頭開始編寫程式碼的每個方面。 在本文中,我們將解釋如何在.NET MAUI Windows 應用程式中整合IronBarcode以掃描條碼或二維碼。 如何在.NET MAUI中讀取和掃描條碼 安裝用於讀取和掃描條碼的 C# 函式庫。 根據任務要求,使用.NET MAUI設計應用程式前端。 取得給定條碼影像的影像路徑 使用`Read`方法掃描提供的條碼 使用`SetTextAsync`方法複製結果值 IronBarcode:C# 條碼庫 為了在我們的應用程式中讀取條碼,我們將使用IronBarcode .NET程式庫。 它提供了一個強大而簡單的條碼讀取 API,使得開發無需任何麻煩或條碼領域知識。 它可以透過NuGet套件管理器輕鬆安裝。 IronBarcode支援多種條碼格式的讀取,包括 Code 39、Code 128、PDF417 等。 您可以讀取多種資料格式,例如影像檔案、記憶體流和 PDF 檔案。 在.NET MAUI應用程式中讀取條碼的步驟 請依照下列步驟在.NET MAUI應用程式中讀取條碼。 先決條件 Visual Studio 2022 Visual Studio 中的 .NET MAUI項目 安裝IronBarcode庫 我們可以使用NuGet安裝IronBarcode庫 Package Manager Console。 若要在 Visual Studio 中開啟此控制台,請導航至 Tools > NuGet Package Manager > Package Manager Console。 然後,在控制台中輸入以下命令: Install-Package BarCode 此控制台命令將在 MAUI 專案中下載最新版本的IronBarcode庫。 或者,您也可以在NuGet網站上搜尋最新版本的NuGet套件。 前端 第一步是創建前端設計。 為此,我們將建立一個包含兩個按鈕、一個文字區域和一個圖像框的佈局。 一個按鈕用於選擇條碼,另一個按鈕用於複製條碼的文字。 "圖像"框將顯示所選圖像。 將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="MAUI_Barcode.MainPage"> <ScrollView> <VerticalStackLayout Spacing="25" Padding="30,0" VerticalOptions="Center"> <Button x:Name="ImageSelect" Text="Select Barcode" SemanticProperties.Hint="Select Image" Clicked="SelectBarcode" HorizontalOptions="Center" /> <Image x:Name="barcodeImage" SemanticProperties.Description="Selected Barcode" HeightRequest="200" HorizontalOptions="Center" /> <Editor x:Name="outputText" Placeholder="Output text" HeightRequest="100" WidthRequest="500" /> <Button x:Name="copyText" Text="Copy" SemanticProperties.Hint="Copy Text" WidthRequest="150" Clicked="CopyEditorText" HorizontalOptions="Center" /> </VerticalStackLayout> </ScrollView> </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="MAUI_Barcode.MainPage"> <ScrollView> <VerticalStackLayout Spacing="25" Padding="30,0" VerticalOptions="Center"> <Button x:Name="ImageSelect" Text="Select Barcode" SemanticProperties.Hint="Select Image" Clicked="SelectBarcode" HorizontalOptions="Center" /> <Image x:Name="barcodeImage" SemanticProperties.Description="Selected Barcode" HeightRequest="200" HorizontalOptions="Center" /> <Editor x:Name="outputText" Placeholder="Output text" HeightRequest="100" WidthRequest="500" /> <Button x:Name="copyText" Text="Copy" SemanticProperties.Hint="Copy Text" WidthRequest="150" Clicked="CopyEditorText" HorizontalOptions="Center" /> </VerticalStackLayout> </ScrollView> </ContentPage> XML 所有元素垂直堆疊,位於中心位置。 您可以根據自己的喜好進行更改。 使用IronBarcode進行條碼掃描 本節將介紹使用IronBarcode庫掃描條碼的程式碼。 首先,我們將使用 FilePicker 來選擇檔案並指定映像的檔案類型。 之後,我們將使用 FullPath 屬性來檢索影像檔案的路徑,然後將影像框的來源設為 FullPath 值。 最後,我們將使用 path 值作為 Read 函數中的 BarcodeReader 來檢索文字。 private async void SelectBarcode(object sender, EventArgs e) { // Use FilePicker to allow the user to select an image file. var images = await FilePicker.Default.PickAsync(new PickOptions { PickerTitle = "Pick image", FileTypes = FilePickerFileType.Images }); // Get the full path of the selected image file. var imageSource = images.FullPath.ToString(); // Set the source of the Image view to the selected image's path. barcodeImage.Source = imageSource; // Use IronBarcode to read the barcode from the image file and get the first result. var result = BarcodeReader.Read(imageSource).First().Text; // Display the read result in the Editor. outputText.Text = result; } private async void SelectBarcode(object sender, EventArgs e) { // Use FilePicker to allow the user to select an image file. var images = await FilePicker.Default.PickAsync(new PickOptions { PickerTitle = "Pick image", FileTypes = FilePickerFileType.Images }); // Get the full path of the selected image file. var imageSource = images.FullPath.ToString(); // Set the source of the Image view to the selected image's path. barcodeImage.Source = imageSource; // Use IronBarcode to read the barcode from the image file and get the first result. var result = BarcodeReader.Read(imageSource).First().Text; // Display the read result in the Editor. outputText.Text = result; } $vbLabelText $csharpLabel 下面的程式碼將用於複製文本編輯器中的文本,並向使用者顯示提示訊息,告知文本已被複製。 private async void CopyEditorText(object sender, EventArgs e) { // Copy the text from the Editor to the clipboard. await Clipboard.SetTextAsync(outputText.Text); // Show a success message to the user. await DisplayAlert("Success", "Text is copied!", "OK"); } private async void CopyEditorText(object sender, EventArgs e) { // Copy the text from the Editor to the clipboard. await Clipboard.SetTextAsync(outputText.Text); // Show a success message to the user. await DisplayAlert("Success", "Text is copied!", "OK"); } $vbLabelText $csharpLabel You can find the project source code in this article on GitHub. 輸出 項目運行後,您將看到以下輸出。 圖片尚未顯示,因為它尚未被選取。 未選擇影像時的輸出 選取條碼後,它將顯示如下截圖所示,二維碼的輸出文字將顯示在編輯器中。 選擇影像後的輸出 點擊"複製"按鈕將觸發先前提到的警告視窗出現。 複製提醒 結論 在本文中,我們解釋瞭如何使用IronBarcode在.NET MAUI應用程式中讀取條碼。 作為一款二維碼閱讀器, IronBarcode 的表現非常出色——它能提供符合預期的精準輸出。 此外,它還能讀取難以辨識的條碼。 您也可以使用不同的字體來建立和自訂條碼。 點擊此連結以獲取更多關於IronBarcode的教學文章。 IronBarcode必須獲得許可才能用於開發和商業用途。 您可以在這裡找到更多關於許可證方面的資訊。 常見問題解答 如何在.NET MAUI應用程序中掃描QR碼? 您可以通過使用 IronBarcode 庫在.NET MAUI應用程序中掃描QR碼。通過 Visual Studio 中的 NuGet 套件管理器安裝該庫,並使用 BarcodeReader.Read 方法從選定的圖像文件中提取文本。 在 .NET MAUI 項目中安裝 IronBarcode 的過程是什麼? 要在 .NET MAUI 項目中安裝 IronBarcode,請在 Visual Studio 中打開 NuGet 套件管理器控制台,運行命令 Install-Package Barcode 以下載並安裝該庫。 可以使用 IronBarcode 庫讀取哪些條碼格式? IronBarcode 支持多種條碼格式,包括 QR 碼、Code 39、Code 128、PDF417 等,使您的應用程序具備多功能的條碼閱讀能力。 如何在 .NET MAUI 中設計條碼掃描應用程序的界面? 在 .NET MAUI 中,可以使用 XAML 設計條碼掃描應用程序的界面。通常,它包括一個布局,其中包含按鈕、文本區域和圖像框,這些可以在 MainPage.xaml 文件中定義。 如何在.NET MAUI應用程式中將掃描到的條碼文字複製到剪貼簿? 在 .NET MAUI 應用程序中使用 Clipboard.SetTextAsync 方法將掃描到的條碼文本複製到剪貼板。此方法可以通過按鈕單擊觸發,並顯示一個警報以確認該操作。 在 .NET MAUI 中使用 IronBarcode 可以自定義條碼外觀嗎? 可以,IronBarcode 提供自定義條碼外觀的選項,可以更改字體、顏色和樣式,支持創建視覺上量身定制的條碼。 需要許可才能在商業應用中使用 IronBarcode 嗎? 是的,無論是開發還是商業用途,都需要許可才能使用 IronBarcode。許可詳情和選項可在 IronBarcode 網站上獲得。 我可以在哪裡查找.NET MAUI條碼掃描教程的原始碼? 該.NET MAUI條碼掃描教程的源代碼可在 GitHub 上找到。文章中提供了一個鏈接以方便訪問倉儲庫。 IronBarcode 怎樣增強 .NET MAUI 應用中的條碼掃描功能? IronBarcode 透過提供一個支持多種條碼格式的強大 API,並與 .NET MAUI 項目無縫集成,增強了.NET MAUI 應用中的條碼掃描,確保高效和準確的條碼閱讀。 Curtis Chau 立即與工程團隊聊天 技術作家 Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。除了開發之外,Curtis 對物聯網 (IoT) 有著濃厚的興趣,探索將硬體和軟體結合的創新方式。在閒暇時間,他喜愛遊戲並構建 Discord 機器人,結合科技與創意的樂趣。 準備好開始了嗎? Nuget 下載 2,121,847 | 版本: 2026.3 剛剛發布 開始免費試用 免費 NuGet 下載 總下載量:2,121,847 查看許可證 還在捲動嗎? 想要快速證明? PM > Install-Package BarCode 執行範例 看您的字串變成 BarCode。 免費 NuGet 下載 總下載量:2,121,847 查看許可證