Reputation: 809
I have successfully implemented Zxing Barcode scanner in my Xamarin app. I am using it to scan QR Codes. The scan works successfully only sometimes. Mostly the scanner is unable to scan. No exception is thrown, the scan page continues to display the QR Code and the OnScanResult is not triggered. However, if I turn the mobile upside down, it is able to successfully scan all the times. What could be wrong?
I have tried setting the AutoRotate flag to both true/false but the behavior is same.
Here's the scanner code:
var options = new MobileBarcodeScanningOptions
{
AutoRotate = true,
UseFrontCameraIfAvailable = false,
TryHarder = true,
};
options.PossibleFormats.Clear();
options.PossibleFormats.Add(ZXing.BarcodeFormat.QR_CODE);
var overlay = new ZXingDefaultOverlay
{
TopText = "Please scan QR code",
BottomText = "Align the QR code within the frame"
};
var QRScanner = new ZXingScannerPage(options, overlay) { IsScanning = true, IsAnalyzing = true };
QRScanner.OnScanResult += async (qrcode) =>
{
// Stop scanning
QRScanner.IsAnalyzing = false;
QRScanner.IsScanning = false;
// Pop the page and show the result
Device.BeginInvokeOnMainThread(() =>
{
Navigation.PopModalAsync(true);
});
ApiHelper.ApiResult apiresult = await ApiHelper.MarkAttendanceAsync(qrcode.Text);
if (apiresult.success == true)
{
Device.BeginInvokeOnMainThread(() =>
{
DependencyService.Get<IToastService>().ShortAlert(apiresult.message);
});
}
else
{
Device.BeginInvokeOnMainThread(async () =>
{
await DisplayAlert("Error", apiresult.message, "OK");
});
}
};
await Navigation.PushModalAsync(QRScanner);
Here is a sample QR that I am using. You can see that there are no disturbing elements above or below the QR:
Upvotes: 2
Views: 1255
Reputation: 737
I have also faced a similar issue in the past
Please try a few experiments.
Try turning the qr code upside side down. This will isolate issues related to auto rotate in your phone. Also try scanning some other standard QR codes which you can find on Google Images or such. Does the app work there?
If not, try increasing the size of the encoded string. I have observed that ZXing sometimes struggles with smaller codes. This is what fixed it for me.
Upvotes: 1