Forum Xamarin.Android

How to read barcodes by zxing Libraries

monemdcmonemdc Member ✭✭✭

hi

These codes are used to read barcodes, and the application stops when you press the button

error message

System.NullReferenceException: 'Object reference not set to an instance of an object.'

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.companyname.app7" android:installLocation="auto"> <uses-sdk android:minSdkVersion="19" android:targetSdkVersion="28" /> <supports-screens android:resizeable="true" android:smallScreens="true" android:normalScreens="true" android:largeScreens="true" android:xlargeScreens="true" /> <application android:allowBackup="true" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"></application> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.READ_CONTACTS" /> <uses-permission android:name="android.permission.READ_PHONE_NUMBERS" /> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <uses-permission android:name="android.permission.READ_PROFILE" /> <uses-permission android:name="android.permission.CAMERA" /> <uses-permission android:name="android.permission.FLASHLIGHT" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> </manifest>

`
using Android.App;
using Android.OS;
using Android.Support.V7.App;
using Android.Runtime;
using Android.Widget;
using System;
using Android.Telephony;
using System.Runtime.Remoting.Contexts;
using Android.Util;
using Android.Accounts;
using ZXing.Mobile;

namespace App7
{
[Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]
public class MainActivity : AppCompatActivity
{
//private TelephonyManager tm;

    Button buttonDoScan;

    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        Xamarin.Essentials.Platform.Init(this, savedInstanceState);
        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.activity_main);

        var scanner = new MobileBarcodeScanner();

       buttonDoScan = this.FindViewById<Button>(Resource.Id.button1);
        TextView textDisply = FindViewById<TextView>(Resource.Id.textView1);

        buttonDoScan.Click += async delegate
        {
            scanner.UseCustomOverlay = false;
            scanner.TopText = "Scanning for barcode";
            var result = await scanner.Scan();
            DisplayResult(result);
        };
    }

    void DisplayResult(ZXing.Result result)
    {
        string message = "";

        if (result != null && !string.IsNullOrEmpty(result.Text))
            message = "Barcode: " + result.Text;
        else
            message = "Could not scan.";

        this.RunOnUiThread(() => Toast.MakeText(this, message, ToastLength.Short).Show());
    }



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);
    }
}

}

`

Tagged:

Best Answer

Answers

  • LandLuLandLu Member, Xamarin Team Xamurai

    We need to initialize it before using the scanner. Try to put the code below in your OnCreate event:

    MobileBarcodeScanner.Initialize(Application);
    

    I tried to create a sample with ZXing. You could refer to the attachment for the specific code.

  • monemdcmonemdc Member ✭✭✭

    THANKS LandLu .

    Is there a way to add top close button to the barcode reader interface?

Sign In or Register to comment.