Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 58056

Cross platform Barcode Reader

$
0
0

Hello guys,
I am developing a prototype of an app that will have the ability to read barcodes in app.
I have decided to use the ZXing component.

In the PCL I have created an Interface which should handle the scanning operation

public interface IScannerService
{
    ScanResult Scan();
}

I have then created a page which will delivery access to the scanning capabilities through a ToolbarItem object.

<ToolbarItem Text="Leggi carta" Activated="Scanner_Activated"  Order="Primary" Priority="{StaticResource Priority2}">
    <ToolbarItem.Icon>
        <OnPlatform x:TypeArguments="FileImageSource"
                    iOS="scanner.png"
                    Android="ic_action_camera.png"
                    WinPhone="Toolkit.Content/ApplicationBar.Scanner.png" />
    </ToolbarItem.Icon>
</ToolbarItem>

and created the event handler as in the following example

void Scanner_Activated( object sender, EventArgs e ) {
    IScannerService scanner = DependencyService.Get<IScannerService>();
    var result = scanner.Scan();
    this.DisplayAlert( "Barcode: ", result.Code, "OK" );
}

Then, to wire up the effective device specific implementation I have created, for example in the Windows Phone project a concrete implementation of the IScannerService interface.

public class WinPhoneScannerService : IScannerService
{
    public async Task<ScanResult> Scan( ) {
        var dispatcher = Deployment.Current.Dispatcher;
        var scanner = new MobileBarcodeScanner( dispatcher ) {
            UseCustomOverlay = false,
            BottomText = "Scanning will happen automatically",
            TopText = "Hold your camera about \n6 inches away from the barcode",
        };
        var result = await scanner.Scan();
        return new ScanResult { ScannerCode = result.Text };
    }
}

I am trying on the device but the code always hangs on the var result = await scanner.Scan(); and does not return. Moreover I di dnot understand if the camera shall be activated by me or it is automatically open when I invoke the Scan() method.

Any help/thought/suggestion on how to solve this problem?


Viewing all articles
Browse latest Browse all 58056

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>