Intermittent problem reading QR codes
I’m looking for some advice on reading QR codes I created via ZXing.net. Ideally, I’d like to use QR codes with higher data density. But as the density goes up, so does the failure rate.
My workflow is to create a QR code, then display it on screen to confirm it’s what I need. The displayed code is not scaled or otherwise distorted.
All QR codes appear to be created correctly, and I can scan them using my shaky handheld phone, which is pointed at the monitor.
However, occasionally ZXing refuses to scan some QR codes, like the example below.
Changing the Margin and PureBarcode will change the size of the image, but the system still has problems reading some codes, at a rate of about 1 per 30.
I'm using v0.16.10 net48 from NuGet
Here is my code that creates the QR code:
[int]$Size = 500
$BarcodeWriter = [ZXing.BarcodeWriterPixelData]::New()
$BarcodeWriter.Format = [ZXing.BarcodeFormat]::QR_CODE
$BarcodeWriter.Options = [ZXing.Common.EncodingOptions]::New()
$BarcodeWriter.Options.Width = $Size
$BarcodeWriter.Options.Height = $Size
# $BarcodeWriter.Options.Margin = 4
# $BarcodeWriter.Options.NoPadding = $True
# $BarcodeWriter.Options.PureBarcode = $True
$PixelData = $BarcodeWriter.Write( $Text )
$Bitmap = [System.Drawing.Bitmap]::New( $PixelData.Width, $PixelData.Height, [System.Drawing.Imaging.PixelFormat]::Format32bppRgb )
$BitmapData = $Bitmap.LockBits(
[System.Drawing.rectangle]::New( 0,0, $PixelData.Width, $PixelData.Height )
, [System.Drawing.Imaging.ImageLockMode]::WriteOnly
, $Bitmap.PixelFormat
) # end LockBits
[System.Runtime.InteropServices.Marshal]::Copy( $PixelData.Pixels, 0, $BitmapData.Scan0, $PixelData.Pixels.Length )
$Bitmap.UnlockBits( $BitmapData )
Return $Bitmap
This is my code that collects a screenshot of the exact location where a QR code is being displayed, and the call to ZXing
$bitmap = New-Object System.Drawing.Bitmap( $Area.Width, $Area.Height )
$graphics = [System.Drawing.Graphics]::FromImage( $bitmap )
$graphics.CopyFromScreen( $Area.Location, [System.Drawing.Point]::Empty, $Area.Size )
$bitmap.Save( "d:\QrReceive.Source.$( $Guid ).png", [System.Drawing.Imaging.ImageFormat]::Png )
$BarCodeReader = [ZXing.BarcodeReader]::New()
$BarCodeReader.Options.TryHarder = $True
$PossibleFormats = New-Object System.Collections.Generic.List[ZXing.BarcodeFormat]
$PossibleFormats.Add( [ZXing.BarcodeFormat]::QR_Code )
$BarCodeReader.Options.PossibleFormats = $PossibleFormats
# $BarCodeReader.Options.PureBarcode = $True
$Barcode = $BarCodeReader.Decode( $bitmap )
Here is that screenshot collected by the above code.