Search code examples
zxingxamarin.forms

Using a custom renderer with zxing and xamarin forms


I have my Xamarin Forms project set up with zxing installed from nuget in each project. For the most part, the apps do what they need to do (performs a QR scan when a button is clicked).

My issue is that I don't want a separate view for the scan, but to have it as part of my main UI via a custom renderer.

I'm hitting an issue. When I hit the button that fires dependency service, zxing starts a fresh view. This is the correct behaviour as far as I can make out, so is there a way to tell zxing that it needs to use my custom view as the overlay view?


Solution

  • You should implement this as a custom View with custom renderers for each platform. http://developer.xamarin.com/guides/cross-platform/xamarin-forms/custom-renderer/

    Copied from https://github.com/Redth/ZXing.Net.Mobile

    Using the ZXingScanner View / Fragment / Control

    On each platform, the ZXing scanner has been implemented as a reusable component (view, fragment, or control), and it is possible to use the reusable component directly without using the MobileBarcodeScanner class at all. On each platform, the instance of the view/fragment/control contains the necessary properties and methods required to control your scanner. By default, the default overlay is automatically used, unless you set the CustomOverlay property as well as the UseCustomOverlay property on the instance of the view/fragment/control. You can use methods such as ToggleTorch() or StopScanning() on the view/fragment/control, however you are responsible for calling StartScanning(...) with a callback and an instance of MobileBarcodeScanningOptions when you are ready for the view's scanning to begin. You are also responsible for stopping scanning if you want to cancel at any point.

    The view/fragment/control classes for each platform are:

    • iOS: ZXingScannerView (UIView) - See ZXingScannerViewController.cs View Controller for an example of how to use this view iOS: AVCaptureScannerView (UIView) - This is API equivalent to ZXingScannerView, but uses Apple's AVCaptureSession Metadata engine to scan the barcodes instead of ZXing.Net. See AVCaptureScannerViewController.cs View Controller for an example of how to use this view
    • Android: ZXingScannerFragment (Fragment) - See ZXingActivity.cs Activity for an example of how to use this fragment
    • Windows Phone: ZXingScannerControl (UserControl) - See ScanPage.xaml Page for an example of how to use this Control