Powershell Decoder Sample

Jul 27, 2015 at 8:07 AM
Edited Jul 27, 2015 at 8:08 AM
I have included sample code below for a powershell based decoder using the ZXing.dll if anyone finds this useful.

Best Regards,

// Synopsis
// Load Bitmap from File and call ZXing Decoder for barcode decoding.


$reader = New-Object ZXing.BarcodeReader
$bitmap = [System.Drawing.Bitmap]::FromFile("c:\temp\sample.png")

// $result = $reader.Decode($bitmap)
Aug 13, 2015 at 8:43 PM
Thanks for sharing it.
Oct 20, 2015 at 4:38 PM
You can now use Add-Type, instead of Reflection.Assembly.
Add-Type -Path (Join-Path $PSScriptRoot 'zxing.dll')
$reader = New-Object ZXing.BarcodeReader
$bitmap = [System.Drawing.Bitmap]::FromFile('C:\path\to\image.png')
$result = $reader.Decode($bitmap)
For me, System.Drawing did not need to be explicitly loaded. If it did, one would use:
Add-Type -AssemblyName System.Drawing
Oct 22, 2015 at 8:35 PM
This works ok in v3 and later, however; I did experience inconsistent problems with function calls from the DLL when loaded in this way for v2.

Overall, I have had significant difficulty getting Powershell to use the ZXing library properly in V2.

The above code will work for a default option basic QRCode read, however; if you need to change options I have yet to discover an easy way of passing those parameters natively/semi-natively without defining the IDictionary Object fully first in a C# code block and then dynamically compiling it at runtime.

It seems Powershell v2 simply cannot create strongly-typed Custom Dictionary Object members, and it handles Enumeration Types only slightly less poorly.

All variations of New-Object where it is not defined below as 2[], Powershell cannot find the Type with the library loaded.
Every variation I could think of below would find the type, but could not convert System.Object[] to System.String <<< ComObject

The code below assumes both the ZXing.dll and System.Collections.Generic.* have been loaded with Add-Type or through Reflection.
$IDict = New-Object "System.Collections.Generic.Dictionary
2[[ZXing.DecodeHintType, zxing.dll],[System.Collections.Generic.List[System.Object],mscorlib.dll]]"