0

I am trying to read barcode through Xamarin Forms Application but unfortunately couldn't read the barcode. What I want is when scanner is opened, I want the results real time on same screen. I am using ZXing 2.2.9 nuget as 4.2.1 is not supported. Below is my code.

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
             xmlns:zxing="clr-namespace:ZXing.Net.Mobile.Forms;assembly=ZXing.Net.Mobile.Forms"
             x:Class="WarehouseApp.Views.ScanAndReceive.ScanReceive">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="4*"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <zxing:ZXingScannerView x:Name="zxing" 
                                VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand"/>
        <zxing:ZXingDefaultOverlay TopText="Hold your phone up to the barcode"
                                   BottomText="Scanning will happen automatically"/>
        <Label x:Name="lblResult"
               Grid.Row="1"
               TextColor="Red"
               HorizontalOptions="Center"
               VerticalOptions="Center"
               HorizontalTextAlignment="Center"
               VerticalTextAlignment="Center"/>
    </Grid>
</ContentPage>
    public ScanReceive()
        {
            InitializeComponent();
            ZXing.Net.Mobile.Forms.ZXingScannerPage scanPage = new ZXing.Net.Mobile.Forms.ZXingScannerPage();
            scanPage.IsScanning = true;
            scanPage.OnScanResult += (result) => Device.BeginInvokeOnMainThread(() => {
                lblResult.Text = "abc";
            });
            //BtnScan_OnClicked();
        }
        protected override void OnAppearing()
        {
            base.OnAppearing();
            lblResult.Text = "test";
        }

MainActivity.cs

public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

        Xamarin.Essentials.Platform.Init(this, savedInstanceState);
        global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
        ZXing.Net.Mobile.Forms.Android.Platform.Init();
        LoadApplication(new App());
        Window.SetStatusBarColor(Android.Graphics.Color.Rgb(236, 178, 66));
    }

    public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
    {
        Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);

        base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
    }
}

this is what I have tried so far...

ToolmakerSteve
  • 18,547
  • 14
  • 94
  • 196
  • on scan receive constructor you can see I have passed abc but it will be lblResult = result.Text – Mirza Fawad Baig May 31 '22 at 09:30
  • 1
    why do you have a `ScannerView` in your XAML and then create a `ZXingScannerPage` in the code behind that does nothing? Have you looked at the sample app provided? – Jason May 31 '22 at 12:12
  • I have followed this also [link](https://www.c-sharpcorner.com/article/embedding-qr-code-scanner-in-xamarin-forms/) but no use – Mirza Fawad Baig May 31 '22 at 12:16
  • Really? Because the code for their scanner page in step 3 is NOT what you are doing in your page – Jason May 31 '22 at 12:22
  • I have done the same thing as they have done on step 3. Followed exactly what they did – Mirza Fawad Baig May 31 '22 at 12:33
  • 1
    no, your are not. See my first comment. In the linked code they have a ScanView in their XAML, and in the code behind they attach an event handler. That is NOT what you are doing – Jason May 31 '22 at 12:39
  • At first what they done on step 3 I actually did that but than changed it later. Anyhow the code they mentioned actually worked. The problem was in nuget version. It does not work in 2.2.9, you have to install 2.4.1 which is Zxing latest version – Mirza Fawad Baig Jun 02 '22 at 16:45
  • So when you change the package version to 2.4.2, the problem was soled? – Liyun Zhang - MSFT Jun 06 '22 at 09:03
  • 2.4.1, Yes it solved my problem – Mirza Fawad Baig Jun 07 '22 at 10:04

0 Answers0