Redth / ZXing.Net.Mobile

Barcode Scanner for Xamarin.iOS, Xamarin.Android, UWP and Tizen
MIT License
1.07k stars 703 forks source link

Still cannot get Scan() to work in Android using Xamarin.Forms v2.3.1.114 PCL #406

Closed calbert52 closed 4 years ago

calbert52 commented 8 years ago

I know this is a closed issue, but hoping to get some ideas on where else I could look to solve this problem.

I compiled your sample forms project, and it works fine there.

I am sure that I followed the same coding pattern as in the sample forms project, but I still get "Object reference not set to an instance of an object", when I execute scanner.Scan(); This did work in earlier versions of my code.

Stack trace: at ZXing.Mobile.MobileBarcodeScanner.GetContext (Android.Content.Context context) [0x0001f] in :0 at ZXing.Mobile.MobileBarcodeScanner.Scan (Android.Content.Context context, ZXing.Mobile.MobileBarcodeScanningOptions options) [0x00015] in :0 at ZXing.Mobile.MobileBarcodeScanner.Scan (ZXing.Mobile.MobileBarcodeScanningOptions options) [0x00001] in :0 at ZXing.Mobile.MobileBarcodeScannerBase.Scan () [0x00006] in :0 at ClocTrakMobile.ViewCells.GroupBadgeIdCell+<b__13_1>d.MoveNext () [0x0005c] in C:\Source\Mobile\ClocTrakMobile\ClocTrakMobile\ClocTrakMobile\ViewCells\GroupBadgeIdCell.cs:146

My Code below

Android MainActivity code in OnCreate global::ZXing.Net.Mobile.Forms.Android.Platform.Init();

Included: public override void OnRequestPermissionsResult(....) { global::ZXing.Net.Mobile.Forms.Android.PermissionsHandler.OnRequestPermissionsResult( requestCode, permissions, grantResults); }

I have a custom ViewCell hosted in a TableView that creates a Entry control and a Button to click to scan a barcode. When I click on the button that executes the below code, I still get the following exception "Object reference not set to an instance of an object", when I execute scanner.Scan();

var scanner = new MobileBarcodeScanner(); scanner.TopText = "Top text"; scanner.BottomText ="Bottom text"; var result = await scanner.Scan();

faceoffers28 commented 8 years ago

I'm not sure if this helps, but I only ever got it to work by commenting out this line in my MainActivity.cs file.

//      public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Permission[] grantResults)
//      {
//          global::ZXing.Net.Mobile.Forms.Android.PermissionsHandler.OnRequestPermissionsResult (requestCode, permissions, grantResults);           
//      }
calbert52 commented 8 years ago

Thanks, but commenting the above code out does not get it to work.

faceoffers28 commented 8 years ago

I didn't realize it, but I have this commented out as well. I know it seems weird, but it works for me.

//Barcode scanner
//global::ZXing.Net.Mobile.Forms.Android.Platform.Init ();

Your using a PCL, so I assume that you have added the ScannerPage to your PCL Project. This is the page I'm using. https://github.com/Redth/ZXing.Net.Mobile/blob/master/Samples/Forms/Core/CustomScanPage.cs

calbert52 commented 8 years ago

I am not using a custom scan page. When using the following, I think it is suppose to use the built in scan page.

var scanner = new MobileBarcodeScanner(); scanner.TopText = "Hold the camera up to the barcode\nAbout 6 inches away"; scanner.BottomText = "Wait for the barcode to automatically scan!"; var result = await scanner.Scan();

faceoffers28 commented 8 years ago

It will use the custom overlay unless you specify otherwise, but you have to give it enough code to call the overlay that contains the horizontal red line. If you aren't seeing this page, then you are doing something wrong. I didn't get the entire solution to work on the first try, but I did get the overlay with the horizontal red line to display. I then had to work to get the camera to activate. I see where you had this working at one time, so perhaps this a new issue. :)

calbert52 commented 8 years ago

Have tried using customOverlay, customScanPage, cannot get it to display the scanner page.

This worked before inside the ScanButton.Clicked event, with just 4 lines of code as follows: var scanner = new MobileBarcodeScanner(); scanner.TopText = "Hold the camera up to the barcode\nAbout 6 inches away"; scanner.BottomText = "Wait for the barcode to automatically scan!"; var result = await scanner.Scan();

While the scanner page should be displayed, the debugger output window has the following:

08-30 15:59:48.779 D/ZXing.Net.Mobile( 7074): Checking android.permission.CAMERA... Getting Camera Exclusive access 08-30 15:59:48.781 D/Mono ( 7074): Assembly Ref addref ZXing.Net.Mobile.Core[0xaa6dd620] -> System.Diagnostics.Debug[0xaa6de5e0]: 6 Got Camera Exclusive access 08-30 15:59:48.781 I/mono-stdout( 7074): Getting Camera Exclusive access 08-30 15:59:48.781 I/mono-stdout( 7074): Got Camera Exclusive access 08-30 15:59:48.782 D/ZXing.Net.Mobile( 7074): Checking Number of cameras...

Attached is my source for my ViewCell that uses ZXing.Net.Mobile.

GroupBadgeIdCell.zip

polofoufou commented 8 years ago

I got the exact same problem... The worst part is I don't know when it started but two days ago the same code without any change worked.

I still work on iOS but it throw a null ref exception on any Android device (tested with 3 devices). The last part of the stacktrace is ZXing.Mobile.MobileBarcodeScanner.GetContext(Android.Context. context)

If someone has a workaround that would be great

redwolf2 commented 8 years ago

Did yall initialized as sugested in the sample source here: https://github.com/Redth/ZXing.Net.Mobile

Otherwise the Context object may not be available.

// Initialize the scanner first so it can track the current context
MobileBarcodeScanner.Initialize (Application);
polofoufou commented 8 years ago

When i try to do MobileBarcodeScanner.Initialize (Application.Current); In my Droid project I stil have a convert error xamarin.application to android.app.application.

I put the scan methode in a dependcyservice beacause the Initialise methode is only for Android.

calbert52 commented 8 years ago

I finally did get it to work, not sure why, but I uninstalled the two nuget packages:

  1. ZXing.Net.Mobile.Forms
  2. ZXing.Net.Mobile

I then installed nuget packages in this order:

  1. ZXing.Net.Mobile
  2. ZXing.Net.Mobile.Forms

In my Android project, MainActivity.cs, in OnCreate(), added: ZXing.Mobile.MobileBarcodeScanner.Initialize(Application); global::ZXing.Net.Mobile.Forms.Android.Platform.Init();

in my portable class library, referenced: using ZXing.Net.Mobile.Forms; private ZXingScannerPage scanPage;

In my portable class library, ScanButton.Clicked event: ScanButton.Clicked += async (sender, args) => { String scanresult = ""; scanPage = new ZXingScannerPage(); scanPage.DefaultOverlayTopText = "Your top text"; scanPage.DefaultOverlayBottomText = "Your bottom text"; scanPage.Title = "Scan"; scanPage.Disappearing += (sender2, args2) => { scanPage.IsScanning = false; }; scanPage.OnScanResult += (result) => { scanPage.IsScanning = false; Navigation.PopAsync(); scanresult = result.Text; }; await App.AppStartPage.Navigation.PushAsync(scanPage); // scanresult will contain the scanned barcode text or nothing if you closed the navigation page. }

Using the below resulted in erratic behavior.

var scanner = new MobileBarcodeScanner(); scanner.TopText = "Your top text"; scanner.BottomText = "Your bottom text"; var result = await scanner.Scan();

Redth commented 8 years ago

Kind of sounds like something messed up in the install and the proper platform assemblies didn't get referenced in your android project, so the bait and switch failed.

polofoufou commented 8 years ago

Its still not working for me...

I got the scan page to show but I still get erros after the scan or the scan just dosent work.

calbert52 commented 8 years ago

The issue that I commented on above was that the scan page would not display. I have been testing in the emulator, so I have not actually really scanned a badge yet, so when I said that I got it to work, I meant that I got the scanner page to load and look like it was trying to scan. I am hopeful that the scan will actually work when I test it on a real device.

polofoufou commented 8 years ago

know i got a java error... Its been working for weeks and know it falls apart on android dont know why.... Still work on iOS.

polofoufou commented 8 years ago

And its working for me too in the android emulator but not on the 3 devices i tried.

Can we unclose? I need help big time....

Redth commented 8 years ago

Can someone please post a zip file of a reproducible solution with this issue? I can't reproduce it here.

polofoufou commented 8 years ago

Am I the only one left with problem on xamarin v2.3.1.114 PCL ? I cant find the way to publish all the app

polofoufou commented 8 years ago

capture d ecran 2016-09-02 a 10 55 17

Above you can see my error

capture d ecran 2016-09-02 a 10 56 03

Above is both old and new implementation of the code. the old code MobileBarcodeScanner throw the java error and the new code shutdown the app without any debuting message.

Hope this helps

calbert52 commented 8 years ago

Here is a zip file for a test application where barcode's will not scan in android. @Redth BarcodeReaderTest.zip

f1nzer commented 8 years ago

Hi @calbert52

I've checked your app and as I can see there are no problems with barcode scanning. Just be sure to call navigation methods and UI-related changes on the main thread.

scanPage.OnScanResult += result =>
{
    scanPage.IsScanning = false;
    if (result != null)
    {
        ZXingResult = result;
        Device.BeginInvokeOnMainThread(async () =>
        {
            await App.AppStartPage.Navigation.PopAsync();
            ValueEntry.Text = ZXingResult.Text;
        });
    }
};
calbert52 commented 8 years ago

If as you say, there are no problems with barcode scanning, then I would like to know why the scanPage.OnScanResult event never fires when I try to scan a barcode. It does not even try to focus in on the barcode. If it works for you using my sample app, then there has to be something different between your development system running the test app and mine.

f1nzer commented 8 years ago

@calbert52 you can try to focus by tapping on the screen or try #416

calbert52 commented 8 years ago

Hi

I tried the screen tap, did not help.

In #416 there is a section of code as below, where do I get the parameters object, I don’t see it in ZXingScannerPage()?

Carl

var supportedFocusModes = parameters.SupportedFocusModes;

else if (supportedFocusModes.Contains (Camera.Parameters.FocusModeContinuousVideo))

parameters.FocusMode = Camera.Parameters.FocusModeContinuousVideo;

else if (supportedFocusModes.Contains (Camera.Parameters.FocusModeFixed))

parameters.FocusMode = Camera.Parameters.FocusModeFixed;

From: Konstantin Chuvilev [mailto:notifications@github.com] Sent: Monday, September 12, 2016 11:56 AM To: Redth/ZXing.Net.Mobile ZXing.Net.Mobile@noreply.github.com Cc: Carl Albert calbert@timetrak.com; Mention mention@noreply.github.com Subject: Re: [Redth/ZXing.Net.Mobile] Still cannot get Scan() to work in Android using Xamarin.Forms v2.3.1.114 PCL (#406)

@calbert52https://github.com/calbert52 you can try focus by tapping on the screen or try #416https://github.com/Redth/ZXing.Net.Mobile/pull/416

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHubhttps://github.com/Redth/ZXing.Net.Mobile/issues/406#issuecomment-246394827, or mute the threadhttps://github.com/notifications/unsubscribe-auth/AOnY4RhBJYrBVwShUA1Smjw1ssmZ61grks5qpXYNgaJpZM4Jv1TP.

DennisWelu commented 7 years ago

I was having the same problem where the scan page would never go away. Thanks to this SO post for the idea, http://stackoverflow.com/a/17006447/1735721, I just moved the handler for OnScanResult onto the main thread (Device.BeginInvokeOnMainThread for Xamarin Forms) and voila...

jhonatanfernando commented 7 years ago

I have the same problem. @calbert52 Did you fix the problem?

calbert52 commented 7 years ago

No, I have not found a solution for this problem.

calbert52 commented 7 years ago

I did finally get it to work in the latest Xamarin.Forms

See attached file. BarcodeScannerSample.txt

jhonatanfernando commented 7 years ago

I will try it.. Tks man.

calbert52 commented 7 years ago

Sorry, but the above code I submitted does not work consistently. Will keep trying.

jhonatanfernando commented 7 years ago

I really tried it here and it did not work for my android version.

Regards,

Jhonatan Fernando

skype | jhonatanfernand

Linkedin https://www.linkedin.com/in/jhonatanfernando/pt | Twitter https://twitter.com/jhonf_oliveira

From: calbert52 [mailto:notifications@github.com] Sent: segunda-feira, 10 de abril de 2017 11:21 To: Redth/ZXing.Net.Mobile ZXing.Net.Mobile@noreply.github.com Cc: Jhonatan Fernando de Oliveira jhonatanfernando@yahoo.com.br; Comment comment@noreply.github.com Subject: Re: [Redth/ZXing.Net.Mobile] Still cannot get Scan() to work in Android using Xamarin.Forms v2.3.1.114 PCL (#406)

Sorry, but the above code I submitted does not work consistently. Will keep trying.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/Redth/ZXing.Net.Mobile/issues/406#issuecomment-292963797 , or mute the thread https://github.com/notifications/unsubscribe-auth/ABexl-b4bhPB6NA4-CzFk01H1sMNI1CAks5rujqwgaJpZM4Jv1TP . https://github.com/notifications/beacon/ABexl7vs7xQQG1XlaSA_VrNbHjOV5Eo8ks5rujqwgaJpZM4Jv1TP.gif

calbert52 commented 7 years ago

I have had better consistent luck with the code snippet in the attached file. I have tested it on Android 5.0 - API 21). Hope it works for you. BarcodeScannerSample.txt

jhonatanfernando commented 7 years ago

Man, now it worked. Thanks a lot.

nadjalla commented 7 years ago

Thanks, it worked for me too

BandulaPerera commented 7 years ago

Its working for me too...thanks.. Do you have code for the barcode sound. I am trying to play a sound when the scanner reads the barcode. Great help..

calbert52 commented 7 years ago

BandulaPerera, Xamarin Forms does not currently support an sound API, so you will have to use a Dependency Service. Here is a link to sample dependency service code that would play a sound file of your choice cross platform: https://www.codeproject.com/Articles/1088094/Playing-audio-mp-File-in-Xamarin-Forms

f1nzer commented 7 years ago

@BandulaPerera you can play sound by yourself (for reference you can try to use https://github.com/techierathore/AudioPlayEx or https://github.com/adrianstevens/Xamarin-Plugins)

calbert52 commented 7 years ago

There are many plugins to get around the dependency service approach. One that is listed in Xamarin Components at is https://github.com/martijn00/XamarinMediaManager

Redth commented 4 years ago

Thanks for reporting this issue! Unforunately it took me way too long to respond 😭. Sorry, I apologize! Recently the source code for this project was completely refactored to modernize it. Many PR's were included in this effort, and many bugs were hopefully fixed. Please try out the latest 3.x series of NuGet packages (currently in prerelease). To try and make the project more maintainable in my spare time going forward, I've decided to close all existing issues to start with a clean slate. If you're still experiencing this issue on the newest version, please open a new issue with as much detail as possible. Thank you for your patience and understanding! Happy scanning!

BandulaPerera commented 4 years ago

Thank you Jonathan

Really appreciate your work.

Regards Bandula

From: Jonathan Dick notifications@github.com Sent: Friday, 13 March 2020 12:48 PM To: Redth/ZXing.Net.Mobile ZXing.Net.Mobile@noreply.github.com Cc: Perera, Bandula BPerera@cabrini.technology; Mention mention@noreply.github.com Subject: Re: [Redth/ZXing.Net.Mobile] Still cannot get Scan() to work in Android using Xamarin.Forms v2.3.1.114 PCL (#406)

Thanks for reporting this issue! Unforunately it took me way too long to respond 😭. Sorry, I apologize! Recently the source code for this project was completely refactored to modernize it. Many PR's were included in this effort, and many bugs were hopefully fixed. Please try out the latest 3.x series of NuGet packages (currently in prerelease). To try and make the project more maintainable in my spare time going forward, I've decided to close all existing issues to start with a clean slate. If you're still experiencing this issue on the newest version, please open a new issue with as much detail as possible. Thank you for your patience and understanding! Happy scanning!

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHubhttps://urldefense.proofpoint.com/v2/url?u=https-3A__github.com_Redth_ZXing.Net.Mobile_issues_406-23issuecomment-2D598508815&d=DwMFaQ&c=7QGXs1UZsHRvmZ4tSUujmw&r=ZgH0XkAJ6WkdV2CHLv8_Vnp4tIrG9n7cvcrN9nJn1PI&m=J5IyUMZikAtMeH9dz6UPQLqiHSPSZAMe63sKdm--fHs&s=yR-9hDdOhfQHr13z1RFetahOBnj7ZrPRkOJnbJlpFYM&e=, or unsubscribehttps://urldefense.proofpoint.com/v2/url?u=https-3A__github.com_notifications_unsubscribe-2Dauth_AH52WFRNAXZIRIMESRDKXTDRHGGDTANCNFSM4CN7KTHQ&d=DwMFaQ&c=7QGXs1UZsHRvmZ4tSUujmw&r=ZgH0XkAJ6WkdV2CHLv8_Vnp4tIrG9n7cvcrN9nJn1PI&m=J5IyUMZikAtMeH9dz6UPQLqiHSPSZAMe63sKdm--fHs&s=76y0xFDGubcv3cSMvBbzQCb3wQVy68Q4JUwp_rkANpY&e=.

Please consider the environment before you print this e-mail.

This email and any attachments may be confidential, and are intended solely for the use of the individual(s) or entity to whom they are addressed. If you are not the intended recipient of this communication, please notify the sender immediately and delete the email and any attachments. Cabrini does not guarantee that this email is virus or error free.

This email has been scanned by the Cabrini Email Security service.