hjam40 / Camera.MAUI

A CameraView Control for preview, take photos and control the camera options
MIT License
448 stars 72 forks source link

version 1.5.1 does not work on ios #168

Open maslewandowski opened 1 month ago

maslewandowski commented 1 month ago

The event for qr code recognition is not fired when holding a qr code from a business card right in front of the camera.

XAML (Mainpage):

<ScrollView>
    <VerticalStackLayout Padding="30,0" Spacing="25">
        <Label x:Name="barcodeResult" FontSize="20" Text="{Binding BarcodeText}"/>
        <cv:CameraView x:Name="cameraView" WidthRequest="300" HeightRequest="200" BarCodeDetectionEnabled="True" BarCodeResults="{Binding BarcodeResults}">
            <cv:CameraView.Behaviors>
                <toolkit:EventToCommandBehavior EventName="BarcodeDetected" Command="{Binding BarcodeDetectedCommand}" CommandParameter="{Binding Source={x:Reference cameraView}}"/>
                <toolkit:EventToCommandBehavior EventName="CamerasLoaded" Command="{Binding CamerasLoadedCommand}" CommandParameter="{Binding Source={x:Reference cameraView}}"/>
            </cv:CameraView.Behaviors>
        </cv:CameraView>

        <Button Text="Start Camera" Command="{Binding StartCameraCommand}" CommandParameter="{Binding Source={x:Reference cameraView}}"/>
        <Button Text="Stop Camera" Command="{Binding StopCameraCommand}" CommandParameter="{Binding Source={x:Reference cameraView}}"/>
        <CollectionView ItemsSource="{Binding BarcodeResults}">
            <CollectionView.ItemTemplate>
                <DataTemplate>
                    <Label Text="{Binding Text}" FontSize="20"/>
                </DataTemplate>
            </CollectionView.ItemTemplate>
        </CollectionView>
    </VerticalStackLayout>
</ScrollView>

ViewModel (MainPageViewModel):

    public partial class MainPageViewModel : ObservableObject
    {
        [ObservableProperty]
        private string barcodeText;
        [ObservableProperty]
        private BarcodeResult[] barcodeResults;
        [RelayCommand]
        private async Task StartCamera(CameraView cameraView)
        {
            cameraView.IsEnabled = true;
            cameraView.IsVisible = true;
            await cameraView.StartCameraAsync();
        }
        [RelayCommand]
        private async Task StopCamera(CameraView cameraView)
        {
            cameraView.IsEnabled = false;
            cameraView.IsVisible = false;
            await cameraView.StopCameraAsync();
        }
        [RelayCommand]
        private async Task BarcodeDetected(CameraView cameraView)
        {
            await cameraView.StopCameraAsync();
            BarcodeText = cameraView.BarCodeResults.FirstOrDefault()?.Text;
        }
        [RelayCommand]
        private async Task CamerasLoaded(CameraView cameraView)
        {
            cameraView.BarCodeOptions = new BarcodeDecodeOptions()
            {
                ReadMultipleCodes = false,
                AutoRotate = true,
                TryInverted = true,
                TryHarder = true,
            };

            if (cameraView.Cameras.Count > 0)
            {
                cameraView.Camera = cameraView.Cameras.First();
                MainThread.BeginInvokeOnMainThread(async () =>
                {
                    await cameraView.StopCameraAsync();
                    await cameraView.StartCameraAsync();
                });
            }
        }
    }

i have invested a bit time trying to resolve the problem but i dont get it working.

interesting is, that with the version 1.2.1 the code is working fine and the event gets fired.

am i doing something wrong or is there a bug in this version?

ios version 17.5.1 vs professional version 17.11.0 preview 3.0

thanks!

connorjarred commented 1 month ago

I also discovered this yesterday that the Camera.Maui versions 1.5.0 and 1.5.1 barcode reader are also not detecting the barcode event trigger for EAN13 codes on Android. All the Camera.Maui versions from 1.2.1 to 1.4.4 are working fine. The versions of the package Camera.Maui up to 1.4.4 are dependent on the ZXing.Net package (0.16.9) and the Camera.Maui package versions above this from 1.5.0 do not reference the ZXing.Net package anymore, so I think that the external ZXing.Net package is now built in into the Camera.Maui package or its using another software mechanism. This is the description of the ZXing.Net package from nuget: ZXing.Net is a port of ZXing, an open-source, multi-format 1D/2D barcode image processing library originally implemented in Java. It has been ported by hand with a lot of optimizations and improvements. It is compatible with .Net 2.0, .Net 3.5, .Net 4.x, .Net 5.x, .Net 6.x, .Net 7.x, Windows RT Class Library and Components, UWP, .Net Standard 1.x and 2.0x, .Net Core App 3.x, Silverlight 4, Silverlight 5, Windows Phone 7.x and Windows Phone 8.x and Xamarin.Android

Hope this info might help whoever has time to look into the source code. Greetz, Connor Jarred

maslewandowski commented 1 month ago

All the Camera.Maui versions from 1.2.1 to 1.4.4 are working fine.

I will check this out, thanks.

connorjarred commented 1 month ago

I solved it for Android. I use Camera.MAUI nuget package 1.5.1 and added the Camera.MAUI.ZXing nuget package (1.0.0 only verssion) to my solution (VS 2022). Camera.MAUI.ZXing nuget package is based on the ZXing.Net dependancy (0.16.9) which is required for Barcode encoding/decoding. Then in my simple MainPage.xaml.cs added the usings and default code. I'm using the BarcodeFormat.EAN_13 but the Qcode option also worked 4 me. using Camera.MAUI; using Camera.MAUI.ZXing; using Camera.MAUI.ZXingHelper; .....

I've added the VS solution as a Zip file to this comment. TestBarcode.zip

Hope this helps you with your iOS problem as well as I don't have a Mac yet to create iOS builds.

Good luck!