Giter Site home page Giter Site logo

barcodescanner's Introduction

Barcode Scanning Service

I have simply found that I'm relying on Barcode Scanning so often that it gets old having needing to implement barcode scanning over and over. This library aims to reduce your effort while developing a new app while providing you a framework that provides for ease in testing.

ScannerNuGetShield

License & Support

Found a bug, or have an idea to make this library better please feel free to create an issue.

This library is distributed with an MIT License. You can make any modifications you require or patent any software using this library. If this library was helpful for your project please send me a tweet and remember I accept donations to help fund my various open source projects.

paypal

Getting Started

This library has no direct dependencies on any MVVM Framework or DI container and as such can be used with ease regardless of the Framework or DI Container you use. The sample code is based on Prism with DryIoc. Note that the PopupBarcodeScannerService does have a dependency on the IPopupNavigation service. you will need to be sure to register the PopupNavigation.Instance with your DI container.

public void RegisterTypes()
{
    // Uses a Popup Page to contain the Scanner
    Container.Register<IBarcodeScannerService, PopupBarcodeScannerService>();
    Container.UseInstance<IPopupNavigation>(PopupNavigation.Instance);

    // Uses a Content Page to contain the Scanner
    Container.Register<IBarcodeScannerService, ContentPageBarcodeScannerService>();
}

public class ViewAViewModel
{
    private IBarcodeScannerService _barcodeScanner { get; }

    public ViewAViewModel(IBarcodeScannerService barcodeScanner)
    {
        _barcodeScanner = barcodeScanner;
        ScanBarcodeCommand = new DelegateCommand(OnScanBarcodeCommandExecuted);
    }

    public DelegateCommand ScanBarcodeCommand { get; }

    private async void OnScanBarcodeCommandExecuted()
    {
        // Returns the string value of the Barcode
        string barcodeValue = await _barcodeScanner.ReadBarcodeAsync();

        // Returns the ZXing.NET Barcode Scan Result
        Result result = await _barcodeScanncer.ReadBarcodeResultAsync();
    }
}

Customization

Both the ContentPageBarcodeScannerService and the PopupBarcodeScannerService are designed to allow you to customize the look and feel without having to worry about hooking into the Scan Result event.

public class MyBarcodeScannerService : PopupBarcodeScannerService
{
    public MyBarcodeScannerService()
        : base()
    {
        // You can access and set any properties of the ZXing Scanner View
        scannerView.HorizontalOptions = LayoutOptions.Center;
    }

    // The Top and Bottom Text are only used if you are using the Default Scanner Overlay
    protected override string TopText() => "Hello Scanner";

    protected override string BottomText() => "Point this at a barcode";

    // You can add any View that you'd like for the Overlay
    protected override View GetScannerOverlay() => new View();

    // You can choose to customize the Scanning Options such as below where we limit the scanning
    // to look for Code 128
    protected override MobileBarcodeScanningOptions GetScanningOptions()
    {
        var options = base.GetScanningOptions();
        options.PossibleFormats = new List<BarcodeFormat>()
                {
                    BarcodeFormat.CODE_128
                }
        return options;
    }
}

Platform Setup

This library has no direct requirement to be configured on any platform. However it does depend on both Rg.Plugins.Popup and ZXing.Net.Mobile which do require some initialization. NOTE Rg.Plugins.Popup requires no initialization on Android or iOS.

Android

AssemblyInfo.cs

[assembly: UsesPermission(Android.Manifest.Permission.Flashlight)]

AndroidManifest.xml

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.FLASHLIGHT" />

MainActivity.cs

public class MainActivity : FormsAppCompatActivity
{
    protected override void OnCreate(Bundle savedInstanceState)
    {
        TabLayoutResource = Resource.Layout.Tabbar;
        ToolbarResource = Resource.Layout.Toolbar;

        base.OnCreate(savedInstanceState);

        global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
        global::ZXing.Net.Mobile.Forms.Android.Platform.Init();

        LoadApplication(new App());
    }

    public override void OnRequestPermissionsResult(int requestCode, string[] permissions,
                                                    Permission[] grantResults)
    {
        global::ZXing.Net.Mobile
                         .Android
                         .PermissionsHandler
                         .OnRequestPermissionsResult(requestCode, permissions, grantResults);
    }
}

iOS

AppDelegate.cs

public partial class AppDelegate : FormsApplicationDelegate
{
    public override bool FinishedLaunching(UIApplication uiApplication, NSDictionary launchOptions)
    {
        global::Xamarin.Forms.Forms.Init();
        global::ZXing.Net.Mobile.Forms.iOS.Platform.Init();

        LoadApplication(new App());

        return base.FinishedLaunching(uiApplication, launchOptions);
    }
}

Info.plist

<key>NSCameraUsageDescription</key>
<string>Add your Camera Use Description here</string>

barcodescanner's People

Contributors

dansiegel avatar reallinfo avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.