{"id":24872,"date":"2016-03-07T12:54:48","date_gmt":"2016-03-07T20:54:48","guid":{"rendered":"https:\/\/blog.xamarin.com\/?p=24872"},"modified":"2016-03-07T12:54:48","modified_gmt":"2016-03-07T20:54:48","slug":"barcode-scanning-made-easy-with-zxing-net-for-xamarin-forms","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/xamarin\/barcode-scanning-made-easy-with-zxing-net-for-xamarin-forms\/","title":{"rendered":"Barcode Scanning Made Easy with ZXing.Net Mobile"},"content":{"rendered":"<p>\t\t\t\tBarcode scanning is a common functionality in many mobile applications. From checking attendees into an event and opening web pages to creating a <a href=\"https:\/\/blog.xamarin.com\/create-your-own-scavenger-hunt-with-evolve-quest\/\">conference scavenger hunt<\/a>, there are plenty of use cases. <a href=\"https:\/\/github.com\/Redth\/ZXing.Net.Mobile\">ZXing.NET Mobile<\/a> (Zebra crossing) is an open source library that makes scanning barcodes as effortless and painless as possible in your Xamarin and Xamarin.Forms applications. <\/p>\n<h2>Getting Started<\/h2>\n<p>ZXing.Net Mobile is available for your traditional Xamarin applications as both a <a href=\"https:\/\/components.xamarin.com\/view\/zxing.net.mobile\">component<\/a> and a <a href=\"https:\/\/www.nuget.org\/packages\/ZXing.Net.Mobile\/\">NuGet package<\/a> that can be installed in your iOS, Android, Windows, and PCL projects. For this post I&#8217;m going go focus on the new ZXing.Net package enhanced for Xamarin.Forms. This is a special <a href=\"https:\/\/components.xamarin.com\/gettingstarted\/zxing.net.mobile.forms\">component<\/a> and <a href=\"https:\/\/components.xamarin.com\/view\/zxing.net.mobile.forms\">NuGet package<\/a> that can easily be installed in your Xamarin.Forms projects that adds all the great functionality from the original library, but integrates deeply into Xamarin.Forms by allowing you to customize Xamarin.Forms pages with an embedded scanner. Ensure that you add the package or NuGet to all of your projects to get started.<\/p>\n<h2>Create a Scanner Page<\/h2>\n<p>You are able to get barcode scanning in your application with as little as two lines of code. First, create a new ZXingScannerPage and then push the page onto the navigation stack:<\/p>\n<pre class=\"lang:csharp decode:true\">\nvar scanPage = new ZXingScannerPage ();\n\/\/ Navigate to our scanner page\nawait Navigation.PushAsync (scanPage);\n<\/pre>\n<p>This will bring up the barcode scanner and put it into scanner mode immediately. <\/p>\n<h2>Checking results<\/h2>\n<p>Of course, you&#8217;ll want to check the results of what was actually being scanned. You can do this when the OnScanResult event is fired to stop scanning, pop the scanner page, and check the result:<\/p>\n<pre class=\"lang:csharp decode:true\">\nscanPage.OnScanResult += (result) =&gt; \n{\n    \/\/ Stop scanning\n    scanPage.IsScanning = false;\n\n    \/\/ Pop the page and show the result\n    Device.BeginInvokeOnMainThread (async () =&gt; \n    {\n        await Navigation.PopAsync ();        \n        await DisplayAlert(\"Scanned Barcode\", result.Text, \"OK\");\n    });\n};\n<\/pre>\n<h2>Handling Permissions<\/h2>\n<p>On Android ZXing.Net requires the Camera and Flashlight permission to function, so you&#8217;ll need to check these permissions in the project settings or add them to your Android Manifest:<\/p>\n<pre class=\"lang:xml decode:true\">\n\n\n<\/pre>\n<p>When Android Marshmallow was released it added <a href=\"https:\/\/blog.xamarin.com\/requesting-runtime-permissions-in-android-marshmallow\/\">new runtime permissions<\/a> when apps need to use features such as the camera or geolocation, similar to iOS. ZXing.Net will handle the runtime permissions in your app automatically by simply adding the following code to your MainActivity.cs:<\/p>\n<pre class=\"lang:csharp decode:true\">\npublic override void OnRequestPermissionsResult(int requestCode, string[] permissions, Permission[] grantResults)\n{\n    global::ZXing.Net.Mobile.Forms.Android.PermissionsHandler.OnRequestPermissionsResult (requestCode, permissions, grantResults);           \n}\n<\/pre>\n<p>On Windows apps you&#8217;ll need to ensure that you select the Webcam permission. On iOS, ZXing handles everything.<\/p>\n<h2>Customization<\/h2>\n<p>There are several ways to customize the scanner pages. The easiest thing to do is to simply set additional properties on the default scanner page and pass it scanning options. This will allow you to customize the text on the page, but also adjust things such as scanning speed, types, and rotation:<\/p>\n<pre class=\"lang:csharp decode:true\">\n\/\/setup options\nvar options = new MobileBarcodeScanningOptions \n    { \n        AutoRotate = false,  \n        UseFrontCameraIfAvailable = true,\n        TryHarder = true,\n        PossibleFormats = new List\n        { \n           ZXing.BarcodeFormat.Ean8, ZXing.BarcodeFormat.Ean13 \n        }\n    };\n\n\/\/add options and customize page\nscanPage = new ZXingScannerPage(options)\n    {\n        DefaultOverlayTopText = \"Align the barcode within the frame\",\n        DefaultOverlayBottomText = string.Empty,\n        DefaultOverlayShowFlashButton = true\n    };\n<\/pre>\n<h2>Learn More<\/h2>\n<p>This is just a sample of everything that ZXing.Net Mobile has to offer. You can continue to customize with custom overlays and even generate and display your very own Barcode with just a few lines of code. Be sure to read through the <a href=\"https:\/\/components.xamarin.com\/gettingstarted\/zxing.net.mobile.forms\">ZXing.Net Mobile Getting Started<\/a> guide to learn more. \t\t<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Barcode scanning is a common functionality in many mobile applications. From checking attendees into an event and opening web pages to creating a conference scavenger hunt, there are plenty of use cases. ZXing.NET Mobile (Zebra crossing) is an open source library that makes scanning barcodes as effortless and painless as possible in your Xamarin and [&hellip;]<\/p>\n","protected":false},"author":544,"featured_media":24881,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[2],"tags":[4,16],"class_list":["post-24872","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-developers","tag-xamarin-platform","tag-xamarin-forms"],"acf":[],"blog_post_summary":"<p>Barcode scanning is a common functionality in many mobile applications. From checking attendees into an event and opening web pages to creating a conference scavenger hunt, there are plenty of use cases. ZXing.NET Mobile (Zebra crossing) is an open source library that makes scanning barcodes as effortless and painless as possible in your Xamarin and [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/24872","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/users\/544"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/comments?post=24872"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/24872\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/media?parent=24872"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/categories?post=24872"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/tags?post=24872"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}