hjam40 / Camera.MAUI

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

BrarcodeDetection besides QR_codes doesn't work #145

Open Uridel opened 6 months ago

Uridel commented 6 months ago

I updated to the latest version of Camera.Maui and Camera.Maui.ZXing. However I have noticed that barcode detection for anything besides QR_Code just doesn't seem to work.

I tried to verify whether I was incorrectly implementing the setup. So I downloaded the test project from Github and ran it. I simply added the BarcodeFormat.All_1Dto the FullScreenPage.xaml.cs expecting it to work. But it doesn't the BarcodeDetectedevent simply is not fired.

Is anyone else experiencing this?

image

Uridel commented 6 months ago

Update: I appears to only be a problem on Android, iOS seems to be working fine.

CedreLo commented 6 months ago

Same issue on Android

BarcodeDetected is never fired with latest version

jayhayman-hdd commented 6 months ago

Can confirm that versions 1.5.1 and 1.5.0 for iOS and Android has stopped scanning any barcodes (QR, Data Matrix, Code 128 etc.)

Reverting to 1.44 and ZXing.BarcodeFormat in the PossibleFormats settings works as expected.

Uridel commented 6 months ago

@hjam40 are you aware that barcode scanning currently doesn't work?

hjam40 commented 6 months ago

Hi @Uridel,

Barcode scanning is working fine in the 1.5.1 version. Here you can see my tests.

Windows QR: image Android EAN13: Capture_ean13

Maybe you could take a look to the new readme for how to use the new plugging. The new property BarCodeDecoder must be initiated: cameraView.BarCodeDecoder = new ZXingBarcodeDecoder();

CryptKat commented 6 months ago

I've spent half a day, trying to get barcode scanning to work. Camera.MAUI 1.5.1, Android 10 I've found, that if WidthRequest and HeightRequest attributes on CameraView are missing, then barcode scanning is breaked.

<cv:CameraView x:Name="cameraView" WidthRequest="300" HeightRequest="300" />

Working code:

<?xml version="1.0" encoding="utf-8" ?>
<mct:Popup xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:mct="clr-namespace:CommunityToolkit.Maui.Views;assembly=CommunityToolkit.Maui"
             xmlns:cv="clr-namespace:Camera.MAUI;assembly=Camera.MAUI"
             xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"
             x:Class="FiscalCloud.Mobile.BarcodeScannerPopupPage" Opened="Popup_Opened" Closed="Popup_Closed">

    <Grid>
        <toolkit:MediaElement x:Name="beepMediaElement" IsVisible="false"
            Source="embed://beep.mp3" />

        <cv:CameraView x:Name="cameraView" WidthRequest="300" HeightRequest="300" />
    </Grid>

</mct:Popup>
 public BarcodeScannerPopupPage()
 {
     InitializeComponent();

     cameraView.CamerasLoaded += cameraView_CamerasLoaded;
     cameraView.BarcodeDetected += cameraView_BarcodeDetected;
     cameraView.BarCodeDecoder = new ZXingBarcodeDecoder();
     cameraView.BarCodeOptions = new BarcodeDecodeOptions
     {
         AutoRotate = true,
         PossibleFormats = { BarcodeFormat.QR_CODE, BarcodeFormat.All_1D },
         ReadMultipleCodes = false,
         TryHarder = false,
         TryInverted = true
     };
     cameraView.BarCodeDetectionEnabled = true;
 }

 private void cameraView_CamerasLoaded(object? sender, EventArgs e)
{
    if (cameraView.Cameras.Count > 0)
    {
        cameraView.Camera = cameraView.Cameras.First();
        MainThread.BeginInvokeOnMainThread(async () =>
        {
            await cameraView.StopCameraAsync();
            await cameraView.StartCameraAsync();
        });
    }
}

private void cameraView_BarcodeDetected(object? sender, BarcodeEventArgs args)
{
    MainThread.BeginInvokeOnMainThread(() =>
    {
        foreach (var result in args.Result)
        {
            if (string.IsNullOrWhiteSpace(result.Text))
                continue;

            PlayBeep();
            barcodeDetectedCallback.Invoke(result.Text);
        }
    });
}
strajk- commented 5 months ago

I've spent half a day, trying to get barcode scanning to work. Camera.MAUI 1.5.1, Android 10 I've found, that if WidthRequest and HeightRequest attributes on CameraView are missing, then barcode scanning is breaked.

<cv:CameraView x:Name="cameraView" WidthRequest="300" HeightRequest="300" />

Working code:

<?xml version="1.0" encoding="utf-8" ?>
<mct:Popup xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:mct="clr-namespace:CommunityToolkit.Maui.Views;assembly=CommunityToolkit.Maui"
             xmlns:cv="clr-namespace:Camera.MAUI;assembly=Camera.MAUI"
             xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"
             x:Class="FiscalCloud.Mobile.BarcodeScannerPopupPage" Opened="Popup_Opened" Closed="Popup_Closed">

    <Grid>
        <toolkit:MediaElement x:Name="beepMediaElement" IsVisible="false"
            Source="embed://beep.mp3" />

        <cv:CameraView x:Name="cameraView" WidthRequest="300" HeightRequest="300" />
    </Grid>

</mct:Popup>
 public BarcodeScannerPopupPage()
 {
     InitializeComponent();

     cameraView.CamerasLoaded += cameraView_CamerasLoaded;
     cameraView.BarcodeDetected += cameraView_BarcodeDetected;
     cameraView.BarCodeDecoder = new ZXingBarcodeDecoder();
     cameraView.BarCodeOptions = new BarcodeDecodeOptions
     {
         AutoRotate = true,
         PossibleFormats = { BarcodeFormat.QR_CODE, BarcodeFormat.All_1D },
         ReadMultipleCodes = false,
         TryHarder = false,
         TryInverted = true
     };
     cameraView.BarCodeDetectionEnabled = true;
 }

 private void cameraView_CamerasLoaded(object? sender, EventArgs e)
{
    if (cameraView.Cameras.Count > 0)
    {
        cameraView.Camera = cameraView.Cameras.First();
        MainThread.BeginInvokeOnMainThread(async () =>
        {
            await cameraView.StopCameraAsync();
            await cameraView.StartCameraAsync();
        });
    }
}

private void cameraView_BarcodeDetected(object? sender, BarcodeEventArgs args)
{
    MainThread.BeginInvokeOnMainThread(() =>
    {
        foreach (var result in args.Result)
        {
            if (string.IsNullOrWhiteSpace(result.Text))
                continue;

            PlayBeep();
            barcodeDetectedCallback.Invoke(result.Text);
        }
    });
}

Can confirm, had the same issue until I've set the dimensions to the CameraView as well, since I have it wrapped in a Border I had the WidthRequest and HeightRequest there and not in the CameraView since for the layout it was unnecessary.

Works

        <Border Stroke="Black" StrokeThickness="1">
            <cv:CameraView x:Name="cameraView" CamerasLoaded="cameraView_CamerasLoaded" Loaded="cameraView_Loaded" WidthRequest="300" HeightRequest="300"/>
        </Border>

Doesn't work

        <Border Stroke="Black" StrokeThickness="1" WidthRequest="300" HeightRequest="300">
            <cv:CameraView x:Name="cameraView" CamerasLoaded="cameraView_CamerasLoaded" Loaded="cameraView_Loaded"/>
        </Border>
Uridel commented 5 months ago

Bit late, but yes. Settings the width and height seems to fix it. @hjam40 , is there known reason to this sudden change, previous versions of the library didn't require Width/Height requests in order to scan 1D barcodes

infor-ajdragasa commented 5 months ago

Why version 1.2.1 is much faster at detecting barcodes and QR codes than the latest version? and no need to install Camera.MAUI.ZXing to detect barcode

skane526 commented 5 months ago

Why version 1.2.1 is much faster at detecting barcodes and QR codes than the latest version? and no need to install Camera.MAUI.ZXing to detect barcode

How much faster is it? Is it noticeably faster? What futures does it lack?

strajk- commented 5 months ago

For those that like me want the CameraView to fit the size of the container and ran into this issue, quick and dirty solution is to wrap it with a Grid or another container of your choice, name it, then Bind your dimensions into its Width and Height:

        <Grid x:Name="cameraViewContainer">
            <cv:CameraView x:Name="cameraView"  WidthRequest="{Binding Source={x:Reference cameraViewContainer}, Path=Width}" HeightRequest="{Binding Source={x:Reference cameraViewContainer}, Path=Height}"/>
        </Grid>

If the size requirements get removed in a future update, this workaround should still work.

infor-ajdragasa commented 5 months ago

Why version 1.2.1 is much faster at detecting barcodes and QR codes than the latest version? and no need to install Camera.MAUI.ZXing to detect barcode

How much faster is it? Is it noticeably faster? What futures does it lack?

It can scan barcodes like just a second unlike the latest version which taking forever and most of the time, not able to scan at all. I will be preparing a comparison and will make another thread for this one

e808080 commented 5 months ago

How did you solve this @info-ajdragasa ? We tried downgrade to 1.2.1 and looks a lot faster yeah. @skane526

athoma13 commented 3 months ago

I still cannot scan 1D barcodes on Android for some reason (v1.5.1). I have set the Width and Height request as suggested and can scan 2D codes (even though extremely finicky with distance from camera). The weird thing for me is the only codes detected are 2D when I have PossibleFormats = { BarcodeFormat.All_1D }...

strajk- commented 3 months ago

I still cannot scan 1D barcodes on Android for some reason (v1.5.1). I have set the Width and Height request as suggested and can scan 2D codes (even though extremely finicky with distance from camera). The weird thing for me is the only codes detected are 2D when I have PossibleFormats = { BarcodeFormat.All_1D }...

I would suggest you doing a rollback to 1.3.5, that's what I ended up doing. 1.5.1 has worse performance, it takes a while with stopping and starting the camera, image quality is bad compared to 1.3.5 despite selecting the highest res option and it takes forever to detect a code.

1.3.5 does all the above better, for now.

jaberlinz commented 2 months ago

camera started but barcode detection does not run at all!

nmg196 commented 1 month ago

I'm having this issue even though I have set WidthRequest and HeightRequest. It scans QR codes but no 1D barcodes that I can find.