ZXing.Net.Mobile扫码视图无法识别条码问题求助(已定位硬件原因)
ZXing.Net.Mobile.Forms OnScanResult Not Triggering on HTC M9
Problem Overview
- Successfully added
ZXing.Net.MobileandZXing.Net.Mobile.Formsto the project - Scanner view, camera display, and flash button all work as expected
- Critical Issue: The
OnScanResultevent never fires, even when testing with DataMatrix, PDF417, and QR codes - Test device: HTC M9
- Verified the feature works correctly on newer devices, pointing to a hardware-related compatibility issue
Possible Solutions & Troubleshooting Steps
1. Verify Camera Permissions & Hardware Compatibility
Old devices like the HTC M9 often have stricter hardware limitations. First:
- Ensure your app has camera permissions configured (add entries in
AndroidManifest.xmlfor Android orInfo.plistfor iOS; use a permissions plugin for dynamic requests in Xamarin.Forms) - Try switching between front and rear cameras—some older rear cameras have poor resolution/focus that hinders barcode recognition
2. Tweak Scanning Options for Better Compatibility
Adjust the MobileBarcodeScanningOptions to optimize for older hardware:
ScannerView.Options = new ZXing.Mobile.MobileBarcodeScanningOptions { PossibleFormats = new List<BarcodeFormat> { BarcodeFormat.DATA_MATRIX, BarcodeFormat.PDF_417, BarcodeFormat.QR_CODE }, TryHarder = true, AutoRotate = true, // Auto-adjust for device orientation UseFrontCameraIfAvailable = false, // Force rear camera (often more reliable) DelayBetweenAnalyzingFrames = 100, // Adjust frame analysis interval InvertedScan = false // Try toggling this if barcodes have low contrast };
3. Fix Scan Result Callback & Initialization
Ensure the scan state is properly managed, and add a slight delay for camera initialization:
protected override async void OnAppearing() { base.OnAppearing(); await Task.Delay(500); // Wait for camera to fully initialize ScannerView.IsScanning = true; ScannerView.IsAnalyzing = true; } // Update the OnScanResult handler to properly stop/restart analysis ScannerView.OnScanResult += (result) => { if (result != null && !string.IsNullOrEmpty(result.Text)) { ScannerView.IsAnalyzing = false; // Prevent duplicate scans Device.BeginInvokeOnMainThread(async () => { await DisplayAlert("Scanned Barcode", result.Text, "OK"); // Re-enable scanning if needed after alert ScannerView.IsAnalyzing = true; }); } };
4. Android-Specific Configuration
For the HTC M9 (Android), add these extra steps:
- Initialize ZXing in
MainActivity.cs'sOnCreatemethod:ZXing.Mobile.MobileBarcodeScanner.Initialize(Application); - Confirm these permissions exist in
AndroidManifest.xml:<uses-permission android:name="android.permission.CAMERA" /> <uses-feature android:name="android.hardware.camera" /> <uses-feature android:name="android.hardware.camera.autofocus" />
5. Test Barcode Quality & Distance
Old cameras struggle with low-quality barcodes. Make sure:
- Barcodes are clear, high-contrast, and free of blurriness
- You're scanning from a distance of 10-20cm (optimal for most older device cameras)
Original Code Reference
ScannerPage.xaml
<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="MyApp.ScannerPage"> <ContentPage.Content> <Grid VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand"> <zxing:ZXingScannerView x:Name="ScannerView"/> <zxing:ZXingDefaultOverlay x:Name="ScannerOverlay" TopText="Hold your phone up to the barcode" BottomText="Scanning will happen automatically"/> </Grid> </ContentPage.Content> </ContentPage>
ScannerPage.xaml.cs
[XamlCompilation(XamlCompilationOptions.Compile)] public partial class ScannerPage : ContentPage { public ScannerPage() { InitializeComponent(); ScannerView.Options = new ZXing.Mobile.MobileBarcodeScanningOptions { PossibleFormats = new List<BarcodeFormat> { BarcodeFormat.DATA_MATRIX, BarcodeFormat.PDF_417, BarcodeFormat.QR_CODE }, TryHarder = true }; ScannerView.OnScanResult += (result) => { var x = 3; // Breakpoint here, never hit Device.BeginInvokeOnMainThread(async () => { // Stop analysis until we navigate away so we don't keep reading barcodes //ScannerView.IsAnalyzing = false; // Show an alert await DisplayAlert("Scanned Barcode", result.Text, "OK"); }); }; ScannerOverlay.ShowFlashButton = ScannerView.HasTorch; ScannerOverlay.FlashButtonClicked += (se, ev) => ScannerView.ToggleTorch(); } protected override void OnAppearing() { base.OnAppearing(); ScannerView.IsAnalyzing = true; ScannerView.IsScanning = true; } protected override void OnDisappearing() { base.OnDisappearing(); ScannerView.IsScanning = false; } }
内容的提问来源于stack exchange,提问作者user7885142
相关产品推荐
相关产品推荐

