hjam40 / Camera.MAUI

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

No fullscreen barcode detection on Android #57

Open InkFeather opened 1 year ago

InkFeather commented 1 year ago

Hi there,

I use the CameraView to detect barcodes on Android. Everything works pretty fine as long as I set fixed dimensions on the view:

<cv:CameraView
        x:Name="CameraComponent"
        WidthRequest="450"
        HeightRequest="300"
        BarCodeDetectionEnabled="True"
        BarCodeDetectionFrameRate="10"
        BarCodeDetectionMaxThreads="5"
        CamerasLoaded="CameraView_CamerasLoaded"
        BarcodeDetected="CameraComponent_BarcodeDetected"/>

Things go wrong when I try to expand the view to full screen:

<cv:CameraView
        x:Name="CameraComponent"
        HorizontalOptions="FillAndExpand"
        VerticalOptions="FillAndExpand"
        BarCodeDetectionEnabled="True"
        BarCodeDetectionFrameRate="10"
        BarCodeDetectionMaxThreads="5"
        CamerasLoaded="CameraView_CamerasLoaded"
        BarcodeDetected="CameraComponent_BarcodeDetected"/>

Barcode detection doesn't trigger anymore. I also tried to encapsulate the view into a grid with the same result.

Did I missed something ?

TimothyFran commented 1 year ago

Having the exact same problem. Have you managed to fix this?

adrianotrentim commented 1 year ago

Same problem!!!!

hiepis commented 1 year ago

same problem - version 1.4.4

ilhamakbar12 commented 1 year ago

same here, waiting for the answer.

Rajnikant-Civica commented 1 year ago

same problem - version 1.4.4

HaydenFerries commented 1 year ago

Same issue here

UkeHa commented 1 year ago

Binding WidthRequest/Heightrequest and setting via DeviceDisplay.MainDisplayInfo.Height in the constructor also doesn't solve it. Only specific height/width works.

hiepis commented 1 year ago

He used TakeSnap(wrong here) and use it to decode. Fixed: MauiCameraView-Android

UkeHa commented 1 year ago

@hiepis could you provide more info/examples?

hiepis commented 1 year ago

Camera.MAUI-master.zip @UkeHa Edit on this project - menu fullscreen. Sorry for slow response

UkeHa commented 1 year ago

@hiepis, could you open a pull request, so that @hjam40 can merge it?

WilliamWatterson86 commented 11 months ago

Not sure if anyone found a solution but I was able to get mine working with the code below:

In xaml define a Stacklayout filling the page:

<StackLayout IgnoreSafeArea="True" AutomationProperties.IsInAccessibleTree="False" x:Name="stkScannerView" Grid.RowSpan="2" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" Padding="0" Spacing="0"></StackLayout>

In cs file:

public ScanQrCodePage(ScanQrCodePageViewModel vm)
{
    InitializeComponent();
    this.BindingContext = vm;

    this.cameraView = new CameraView();

    this.cameraView.CamerasLoaded += this.cameraView_CamerasLoaded;
    cameraView.BarCodeDetectionFrameRate = 5;
    cameraView.BarCodeDetectionMaxThreads = 5;

    cameraView.BarCodeOptions = new Camera.MAUI.ZXingHelper.BarcodeDecodeOptions
    {
        PossibleFormats = { ZXing.BarcodeFormat.QR_CODE },
        ReadMultipleCodes = false,
        TryHarder = true,
        AutoRotate = false,
    };
}

private void ExecuteStartScanning()
{
    try
    {
        this.cameraView.CamerasLoaded -= this.cameraView_CamerasLoaded;
        this.cameraView.CamerasLoaded += this.cameraView_CamerasLoaded;
        this.cameraView.BarcodeDetected -= this.cameraView_BarcodeDetected;
        this.cameraView.BarcodeDetected += this.cameraView_BarcodeDetected;

        MainThread.BeginInvokeOnMainThread(async () =>
        {
            try
            {
                await Task.Delay(150);

                if (!this.stkScannerView.Children.Any())
                {
                    this.stkScannerView.Children.Add(this.cameraView);
                }

                await cameraView.StartCameraAsync();
            }
            catch (Exception ex)
            {
                Logger.LogException(ex, this);
            }
        });
    }
    catch (Exception ex)
    {
        Logger.LogException(ex, this);
    }
}

private void ExecuteStopScanning()
{
    try
    {
        this.cameraView.CamerasLoaded -= this.cameraView_CamerasLoaded;
        this.cameraView.BarcodeDetected -= this.cameraView_BarcodeDetected;
        this.cameraView.BarCodeDetectionEnabled = false;

        MainThread.BeginInvokeOnMainThread(async () =>
        {
            try
            {
                await cameraView.StopCameraAsync();

                if (this.cameraView.TorchEnabled)
                {
                    this.cameraView.TorchEnabled = false;
                    this.frmToggleTorch.Opacity = 0.5;
                }

                if (this.stkScannerView.Children.Any())
                {
                    this.stkScannerView.Children.Remove(this.cameraView);
                }

            }
            catch (Exception ex)
            {
                Logger.LogException(ex, this);
            }
        });
    }
    catch (Exception ex)
    {
        Logger.LogException(ex, this);
    }
}

private void cameraView_BarcodeDetected(object sender, Camera.MAUI.ZXingHelper.BarcodeEventArgs args)
{
    if (args != null && args.Result.Any())
    {
        MainThread.BeginInvokeOnMainThread(() =>
        {
            try
            {
                if (this.BindingContext is ScanQrCodePageViewModel vm)
                {
                    vm.ScanCommand?.Execute(args);
                }
            }
            catch (Exception ex)
            {
                Logger.LogException(ex, this);
            }
        });
    }
}

private void cameraView_CamerasLoaded(object sender, EventArgs e)
{
    if (cameraView.Cameras.Count > 0)
    {
        cameraView.Camera = cameraView.Cameras.First();
    }
}
WoodyJ007 commented 11 months ago

This still appears to be an issue. Is there a simple fix?

UkeHa commented 11 months ago

@WoodyJ007, yes, it's called using this https://github.com/JimmyPun610/BarcodeScanner.Mobile project instead. Everything works as intended and is blazing fast!