Decoding Barcodes in F# with Barcode Xpress
F# is a strongly-typed functional-first language developed by Microsoft to bring the paradigms and advantages of functional style programming into the .NET platform. Influenced by languages like OCaml and Haskell, F# allows programmers to write more concise, readable software while maintaining the advantages of a strongly static typed compiler. While templates and support for F# have existed for some time in .NET Framework, it is considered a first class member of the .NET Core platform. Barcode Xpress 13 recently launched and began providing support for .NET Core. Now is the perfect time to demonstrate how simple it is to use Barcode Xpress in F#.
Adding BarcodeXpress.NETCore to Your Project
Since F# is a first class member of .NET Core, utilizing .NET Core class libraries couldn’t be simpler. You can use visual studio to search for and add the BarcodeXpress.NetCore package to your application or use the following command to do so with the dotnet command line interface:
dotnet add package Accusoft.BarcodeXpress.NetCore
You may also want to specify a version: for instance to add BX 13.0.4, the latest at the time of writing this article, it would simply be:
dotnet add package Accusoft.BarcodeXpress.NetCore --version 13.0.4
After that, add an open statement, F# equivalent of using, and you’re ready to start using Barcode Xpress types and functions in your module:
Instantiate a Barcode Xpress Instance and Licensing
You are free to instantiate your Barcode Xpress object inside your main function as we do in our sample code for C# but if you want to define functions that use the SDK, which you will definitely want to do, you should use a let binding to create it at the top level inside your module.
module BarcodeReader = let barcodeXpress = new BarcodeXpress()
Notice that we do not have to specify the type of our Barcode Xpress object due to F#’s type inference. Many languages are able to infer the type in this context, as in C#’s var type, but as we will see shortly, F#’s type inference extends to a number of contexts that are less common.
If you already have an evaluation or toolkit license for Barcode Xpress, then you should need no further code to start working with the SDK. If you are on a system using a deployment or OEM license, you will need some additional calls placed right after the instantiation and you should take a look at your product documentation to see which calls are needed.
This is also a good place to let the SDK know which types of barcodes you would like to scan. For now, we will set this to scan for Code 128 barcodes, but you can set it for whatever type of barcode you have available. We can skip this step and the engine will search for all 1D barcodes, but even if we are using those types of barcodes we can speed up recognition by specifying exactly the type we want to read.
barcodeXpress.reader.BarcodeTypes <- [| BarcodeType.Code128Barcode |]
This looks slightly different than how we might accomplish the same task in C#, mostly due to F#’s different Array literal syntax and its use of the reverse arrow for setting properties.
Barcode Xpress for .NET Core, like Barcode Xpress for .NET Framework, operates on the standard library System.Drawing.Bitmap class. For now, we will iterate over the command line arguments passed to our program and print out the values of each barcode:
[<EntryPoint>] let main args = for filename in args do use bitmap = new Bitmap(filename) let results = barcodeXpress.reader.Analyze bitmap for result in results do printfn "Barcode Value: %A" result.BarcodeValue 0
That is really all there is to it! You’ll probably notice that, as before, we have not needed to manually specify any types in this code. Sometimes you will want to do so for readability reasons or to ensure that you get back the types you expect and you can do that here if desired:
let (results: Result) = barcodeXpress.reader.Analyze bitmap
Including the necessary system assemblies the complete code looks like this:
open System open System.Drawing open Accusoft.BarcodeXpressSdk module BarcodeReader = let barcodeXpress = new BarcodeXpress() barcodeXpress.reader.BarcodeTypes <- [| BarcodeType.Code128Barcode |] [<EntryPoint>] let main args = for filename in args do use bitmap = new Bitmap(filename) let results = barcodeXpress.reader.Analyze bitmap for result in results do printfn "Barcode Value: %A" result.BarcodeValue 0
Everything we’ve done so far would be pretty simple to do in C#, albeit likely with a little more code. If you’d like to see some examples of how you can introduce more functional programming idioms into your barcode processing code, take a look at the rest of the article here.