MAUI 에서 Google Vision 사용하는 방법입니다.
https://github.com/JimmyPun610/BarcodeScanner.Mobile
MauiProgram.cs
using BarcodeScanner.Mobile;
using Microsoft.Extensions.Logging;
namespace Maui.VisionTest;
public static class MauiProgram
{
public static MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp<App>()
.ConfigureFonts(fonts =>
{
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
})
.ConfigureMauiHandlers(handlers =>
{
// Add the handlers
handlers.AddBarcodeScannerHandler();
});
#if DEBUG
builder.Logging.AddDebug();
#endif
return builder.Build();
}
}
MainPage.xaml
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage
x:Class="Maui.VisionTest.MainPage"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:gv="clr-namespace:BarcodeScanner.Mobile;assembly=BarcodeScanner.Mobile.Maui">
<Grid BackgroundColor="Black" RowDefinitions="7*,3*">
<gv:CameraView
x:Name="Camera"
Margin="10"
HorizontalOptions="FillAndExpand"
OnDetected="Camera_OnDetected"
ScanInterval="100"
TorchOn="False"
VerticalOptions="FillAndExpand"
VibrationOnDetected="False" />
<Button
x:Name="facingButton"
Margin="0,0,0,15"
BackgroundColor="White"
Clicked="FacingButton_Clicked"
HeightRequest="40"
HorizontalOptions="End"
Text="Front/Rear"
TextColor="Black"
VerticalOptions="End"
WidthRequest="110" />
<ScrollView Grid.Row="1">
<Grid>
<Button
x:Name="clearButton"
Margin="5,0"
BackgroundColor="White"
Clicked="ClearButton_Clicked"
HeightRequest="40"
HorizontalOptions="End"
Text="Clear"
TextColor="Black"
VerticalOptions="Start"
WidthRequest="60" />
<Label
x:Name="resultLabel"
Text=""
TextColor="White" />
</Grid>
</ScrollView>
</Grid>
</ContentPage>
MainPage.xaml.cs
using BarcodeScanner.Mobile;
namespace Maui.VisionTest;
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
// 권한 획득
BarcodeScanner.Mobile.Methods.AskForRequiredPermission();
}
private void Camera_OnDetected(object sender, BarcodeScanner.Mobile.OnDetectedEventArg e)
{
List<BarcodeResult> barcodeResults = e.BarcodeResults;
string result = string.Empty;
for (int i = 0; i < barcodeResults.Count; i++)
{
result += $"BarcodeType : {barcodeResults[i].BarcodeType}, Result : {barcodeResults[i].DisplayValue}{Environment.NewLine}";
}
Dispatcher.Dispatch(() =>
{
// 중복 결과 방지
if (!resultLabel.Text.Contains(result))
{
// 결과 표시
resultLabel.Text = result + Environment.NewLine + resultLabel.Text;
}
// 스캐딩 다시 시작
Camera.IsScanning = true;
});
}
private void FacingButton_Clicked(object sender, EventArgs e)
{
if (this.Camera.CameraFacing == CameraFacing.Front)
{
this.Camera.CameraFacing = CameraFacing.Back;
}
else
{
this.Camera.CameraFacing = CameraFacing.Front;
}
}
private void ClearButton_Clicked(object sender, EventArgs e)
{
this.resultLabel.Text = "";
}
}
e.BarcodeResults 에 바코드 인식결과들이 들어있습니다.
동시에 여러게의 바코드를 인식합니다.
결과
소스
https://github.com/kei-soft/Maui.VisionTest
참고
https://github.com/JimmyPun610/BarcodeScanner.Mobile/wiki/3.-Installation-for-Maui
Syncfusion MAUI 소스를 오픈소스로 공개 (0) | 2024.10.23 |
---|---|
MAUI 에서 AdMob 처리하기 - .NET 8 기준 (0) | 2024.06.23 |
.NET MAUI ViewModel 단에서 DisplayPromptAsync 사용하기 (0) | 2024.05.16 |
.NET MAUI Picker 사용 방법 (0) | 2024.05.16 |
.NET MAUI Circle Image 표시하기 (0) | 2024.05.16 |