.NET MAUI Skaner Kodów Kreskowych
Wprowadzenie
.NET MAUI (.NET Multi-platform App UI) to wieloplatformowy framework pozwalający na bezproblemowe tworzenie aplikacji wieloplatformowych w jednym kodzie źródłowym. Na przykład, można łatwo stworzyć aplikacje dla Microsoft Windows, iOS i Android w jednym projekcie. Co odróżnia go od innych platform, frameworków i bibliotek, to sposób, w jaki umożliwia społeczności deweloperskiej korzystanie z natywnych kontrolek w ich projektach i dostarcza im dodatkowe komponenty. W rezultacie deweloperzy mogą używać tych gotowych komponentów i usług do szybszego budowania aplikacji bez konieczności pisania każdej części kodu od podstaw.
W tym artykule wyjaśnimy, jak zintegrować IronBarcode w aplikacji .NET MAUI na Windows, aby zeskanować kod kreskowy lub kod QR.
Jak czytać i skanować kody kreskowe w .NET MAUI
- Zainstaluj bibliotekę C# do odczytu i skanowania kodów kreskowych
- Zaprojektować front-end aplikacji zgodnie z zadaniem w .NET MAUI
- Pobierz ścieżkę obrazu dla danego obrazu kodu kreskowego
- Użyj metody
Readdo zeskanowania dostarczonego kodu kreskowego - Skopiuj wartość wyniku metodą
SetTextAsync
IronBarcode: biblioteka IronBarcode w języku C
Aby czytać kody kreskowe w naszej aplikacji, będziemy używać biblioteki IronBarcode .NET. Zapewnia ona solidne i proste API do odczytu kodów kreskowych, umożliwiając rozwój bez problemów lub potrzeby wiedzy o domenie kodów kreskowych. Można ją łatwo zainstalować za pomocą menedżera pakietów NuGet.
IronBarcode obsługuje wiele formatów kodów kreskowych do odczytu, w tym Code 39, Code 128, PDF417 oraz wiele innych. Można czytać z różnorodnych formatów danych, takich jak pliki graficzne, strumienie pamięci i PDF-y.
Kroki do przeczytania kodów kreskowych w aplikacji .NET MAUI
Wykonaj te kroki, aby przeczytać kod kreskowy w aplikacji .NET MAUI.
Wymagania wstępne
- Visual Studio 2022
- Projekt .NET MAUI w Visual Studio
Zainstaluj bibliotekę IronBarcode
Możemy zainstalować bibliotekę IronBarcode używając NuGet Package Manager Console. Aby otworzyć tę konsolę w Visual Studio, przejdź do Tools > NuGet Package Manager > Package Manager Console. Następnie wpisz następującą komendę w konsoli:
Install-Package BarCode
Ta komenda konsolowa pobierze najnowszą wersję biblioteki IronBarcode w projekcie MAUI. Alternatywnie, można również wyszukać najnowszą wersję pakietu NuGet nastronie NuGet.
Front-End
Pierwszym krokiem będzie stworzenie projektu front-end. W tym celu stworzymy układ złożony z dwóch przycisków, jednej strefy tekstowej i jednego pola obrazu. Jeden przycisk zostanie użyty do wyboru kodu kreskowego, a inny do skopiowania tekstu kodu kreskowego. Pole obrazu wyświetli wybrany obraz.
Zamień zawartość w pliku MainPage.xaml na poniższą:
<?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>
Wszystkie elementy są w pionowym stosie z pozycją środkową. Możesz zmienić to według własnych preferencji.
Skanowanie kodu kreskowego przy użyciu IronBarcode
Ta sekcja opisze kod do skanowania kodów kreskowych przy użyciu biblioteki IronBarcode. Najpierw użyjemy FilePicker do wyboru pliku i określenia typu pliku dla obrazu. Po tym, użyjemy właściwości FullPath do pobrania ścieżki do pliku obrazu, a następnie ustawimy źródło pola obrazu na wartość FullPath. Na końcu, użyjemy wartości path w funkcji Read z BarcodeReader do pobrania tekstu.
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;
}
Private Async Sub SelectBarcode(ByVal sender As Object, ByVal e As EventArgs)
' Use FilePicker to allow the user to select an image file.
Dim images = Await FilePicker.Default.PickAsync(New PickOptions With {
.PickerTitle = "Pick image",
.FileTypes = FilePickerFileType.Images
})
' Get the full path of the selected image file.
Dim 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.
Dim result = BarcodeReader.Read(imageSource).First().Text
' Display the read result in the Editor.
outputText.Text = result
End Sub
Poniższy kod będzie używany do kopiowania tekstu edytora tekstu i wyświetlania użytkownikowi powiadomienia, że tekst został skopiowany.
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");
}
Private Async Sub CopyEditorText(ByVal sender As Object, ByVal e As EventArgs)
' 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")
End Sub
You can find the project source code in this article on GitHub.
Wynik
Po uruchomieniu projektu zobaczysz następujące wyjście. Obraz nie jest wyświetlany, ponieważ nie został jeszcze wybrany.
Wyjście, gdy obraz nie jest wybrany
Gdy kod kreskowy zostanie wybrany, zostanie wyświetlony jak na poniższym zrzucie ekranu, a tekst wyjściowy kodu QR zostanie wyświetlony w edytorze.
Wyjście po wybraniu obrazu
Kliknięcie przycisku Kopiuj wywoła wcześniej wspomniane okno powiadomienia.
Powiadomienie o skopiowaniu
Wnioski
W tym artykule wyjaśniliśmy, jak można czytać kody kreskowe w aplikacji .NET MAUI przy użyciu IronBarcode. Jako czytnik kodów QR, IronBarcode działa bardzo dobrze — zapewnia dokładne wyniki zgodnie z oczekiwaniami. Co więcej, może odczytywać kody kreskowe, które są trudne do odczytania. Możesz także tworzyć i dostosowywać kody kreskowe za pomocą różnych czcionek. Znajdź więcej postów samouczkowych dotyczących IronBarcode z tego linku.
IronBarcode musi być licencjonowany na potrzeby rozwoju i użytkowania komercyjnego. Więcej informacji na temat licencjonowania znajdziesz tutaj.
Często Zadawane Pytania
How can I scan QR codes in a .NET MAUI application?
You can scan QR codes in a .NET MAUI application by using the IronBarcode library. Install the library via NuGet Package Manager in Visual Studio, and use BarcodeReader.Read method to extract text from a selected image file.
What is the process to install IronBarcode in a .NET MAUI project?
To install IronBarcode in a .NET MAUI project, open the NuGet Package Manager Console in Visual Studio and execute the command Install-Package Barcode to download and install the library.
What barcode formats can be read using the IronBarcode library?
IronBarcode supports a variety of barcode formats, including QR codes, Code 39, Code 128, PDF417, and more, allowing for versatile barcode reading capabilities in your applications.
How do I design the interface for a barcode scanner app in .NET MAUI?
In .NET MAUI, the interface for a barcode scanner app can be designed using XAML. Typically, it includes a layout with buttons, a text area, and an image box, which can be defined in the MainPage.xaml file.
How can I copy the scanned barcode text to the clipboard in a .NET MAUI app?
Use the Clipboard.SetTextAsync method in your .NET MAUI app to copy the scanned barcode text to the clipboard. This method can be triggered by a button click, displaying an alert to confirm the action.
Is it possible to customize barcode appearances in .NET MAUI with IronBarcode?
Yes, IronBarcode allows customization of barcode appearances by providing options to alter fonts, colors, and styles, enabling the creation of visually tailored barcodes.
Do I need a license to use IronBarcode in commercial applications?
Yes, a license is required to use IronBarcode for both development and commercial purposes. Licensing details and options are available on the IronBarcode website.
Where can I access the source code for the .NET MAUI barcode scanner tutorial?
The source code for the .NET MAUI barcode scanner tutorial is available on GitHub. A link to the repository is provided in the article for easy access.
How does IronBarcode enhance barcode scanning in .NET MAUI apps?
IronBarcode enhances barcode scanning in .NET MAUI apps by providing a robust API that supports multiple barcode formats and seamless integration with .NET MAUI projects, ensuring efficient and accurate barcode reading.

