Reputation: 43
I'm trying to integrate ZXing.Net.Maui into a simple .NET MAUI project(converted from Xamarin.Android) that I created from scratch. The project targets net8.0-android and only contains four files. However, the camera is not showing up when I run the app. If I replace the CameraBarcodeReaderView with a simple label, it shows up as expected.
Here's the relevant code:
using Microsoft.Maui.Embedding;
using Microsoft.Maui.Platform;
using ZXing.Net.Maui.Controls;
namespace ZXingMauiNativeEmbedding.Droid
{
[Activity(Label = "@string/app_name", MainLauncher = true, Theme = "@style/Theme.MaterialComponents.Light.DarkActionBar")]
public class MainActivity : Activity
{
MauiContext mauiContext;
protected override void OnCreate(Bundle? savedInstanceState)
{
base.OnCreate(savedInstanceState);
var builder = MauiApp.CreateBuilder();
builder.UseMauiEmbedding<Microsoft.Maui.Controls.Application>().UseBarcodeReader();
var mauiApp = builder.Build();
mauiContext = new MauiContext(mauiApp.Services, this);
var myMauiPage = new MainPage();
var containerView = myMauiPage.ToContainerView(mauiContext);
SetContentView(containerView);
}
}
<?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"
xmlns:zxing="clr-namespace:ZXing.Net.Maui.Controls;assembly=ZXing.Net.MAUI.Controls"
x:Class="ZXingMauiNativeEmbedding.Droid.MainPage">
<ScrollView>
<VerticalStackLayout Padding="30,0" Spacing="25" VerticalOptions="Center">
<zxing:CameraBarcodeReaderView x:Name="barcodeReader" BarcodesDetected="BarcodesDetected" />
</VerticalStackLayout>
</ScrollView>
</ContentPage>
using ZXing.Net.Maui;
namespace ZXingMauiNativeEmbedding.Droid
{
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
barcodeReader.Options = new BarcodeReaderOptions
{
Formats = BarcodeFormats.All,
AutoRotate = true,
Multiple = false
};
barcodeReader.CameraLocation = CameraLocation.Front;
}
public void BarcodesDetected(object sender, BarcodeDetectionEventArgs e)
{
var first = e.Results?.FirstOrDefault();
if (first is null)
return;
Dispatcher.DispatchAsync(async () =>
{
await DisplayAlert("Barcode Detected", first.Value, "OK");
});
}
}
}
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true">
</application>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CAMERA" />
</manifest>
I have already added the camera permission to the AndroidManifest.xml, but it doesn't seem to make a difference. What am I missing? How can I get the camera to show up when using ZXing.Net.Maui in a Native embedded .NET MAUI project? Any help would be greatly appreciated.
Please find the github link here: https://github.com/nandhakishore92/ZXingMauiNativeEmbedding
Upvotes: 0
Views: 345