Hi,
I was trying out the ZXingScannerFragment sample here, but the sample doesn't work properly.
When I (almost) literally copy the sample and paste it in my own project, it never executes the scanResultHandler (action parameter of StartScanning method). It does however write something like "[ZXing.Net.Mobile] Barcode found: ....." to the application output..
I tried an older version of the sample, which does work (-ish). When I tried the old sample, it does call the scanResultHandler. This time there is another problem however.. Every time I back out of the app and go back in to it (so OnResume is called), the scanResultHandler is called +1 time. This means if I repeat the process of going in to and out of the app 5 times, the scanResultHandler is called 5 or 6 times.
New sample:
scanFragment.StartScanning (result => {.....}, opts);
Old sample:
scanFragment.StartScanning(HandleScanResult, opts);
I'd really appreciate some help with this.
Thanks!
Edit:
Btw, I'm using the latest version of the ZXing.Net.Mobile package (2.3.2)
I ran into the same problem yesterday - the scanner fragment never starts scanning. After reading the code I know why. Let's look at how the scanner fragment is initialized in the sample:
if (scanFragment == null)
{
scanFragment = new ZXingScannerFragment();
SupportFragmentManager.BeginTransaction()
.Replace(Resource.Id.fragment_container, scanFragment)
.Commit();
}
if (!needsPermissionRequest)
scan();
Where scan() calls scanFragment.StartScanning(..., ...). Let's look at StartScanning():
public void StartScanning (Action<Result> scanResultHandler, MobileBarcodeScanningOptions options = null)
{
ScanningOptions = options;
scanCallback = scanResultHandler;
if (scanner == null) {
//scanImmediately = true;
return;
}
scan ();
}
Here we see the actual scanning only starts if scanner != null. The scanner, in turn, is initialized in ZXingScannerFragment.OnCreateView(). This is the source of the problem. There is absolutely no guarantee that OnCreateView will have been called after the fragment transaction has been committed, this will generally happen sometime later. So, at the moment StartScanning is called, the scanner is still null and actual scanning doesn't start. The only way to have scanning start is by calling it after OnCreateView - which AFAIK right now can only be done by subclassing ZXingScannerFragment. Indeed something like this works:
public class CustomScannerFragment : ZXingScannerFragment
{
public override View OnCreateView(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle bundle)
{
var v = base.OnCreateView(layoutInflater, viewGroup, bundle);
var activity = (MyCustomActivity)Activity;
StartScanning(activity.OnScanResult, activity.ScanningOptions);
return v;
}
}
That indeed seems to fix this particular problem. Thanks.
Since I still had some performance issues with ZXing, I decided to switch to the Google Mobile Vision API. There isn't a lot of documentation for it, but it has a really simple implementation and it seems to work really well so far.
This issue should be reopened, since the problem hasn't really been solved.
ViggoL, I reopened the issue for you.
Please give some more details about the issues you're currently experiencing so that someone can try and help you better.
ElteHupkes has provided all information needed. It's not a big problem, but the sample should at least be fixed to reduce confusion for new developers.
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!
Most helpful comment
I ran into the same problem yesterday - the scanner fragment never starts scanning. After reading the code I know why. Let's look at how the scanner fragment is initialized in the sample:
Where
scan()callsscanFragment.StartScanning(..., ...). Let's look atStartScanning():Here we see the actual scanning only starts if
scanner != null. Thescanner, in turn, is initialized inZXingScannerFragment.OnCreateView(). This is the source of the problem. There is absolutely no guarantee thatOnCreateViewwill have been called after the fragment transaction has been committed, this will generally happen sometime later. So, at the momentStartScanningis called, thescanneris still null and actual scanning doesn't start. The only way to have scanning start is by calling it afterOnCreateView- which AFAIK right now can only be done by subclassing ZXingScannerFragment. Indeed something like this works: