I am trying to import a QR code from a .png file and decode it using Zxing.Net.Mobile and ZXing.Net.Mobile.Forms.
If I scan the QR code using the ZXing.Mobile.MobileBarcodeScanner class, the decoding works as required, however, when importing it from a file, the Qr code reader (ZXing.QrCode.QRCodeReader()) decode function always returns null.
As I'm using Xamarin Forms; each platform handles the bitmap/image creation and the portable part handle the rest (Zxing BinaryBitmap creation and decoding).
//Store rawBytes and image demensions
PotableBitmap bMap = DependencyService.Get<IBitmap>().FileToBitmap(filePath);
RGBLuminanceSource source = new RGBLuminanceSource(bMap.RgbBytes, bMap.Width, bMap.Height, RGBLuminanceSource.BitmapFormat.RGB32);
HybridBinarizer binarized = new HybridBinarizer(source);
BinaryBitmap bitmap = new BinaryBitmap(binarized);
var reader = new ZXing.QrCode.QRCodeReader();
data = reader.decode(qrCodeBitmap); // This is always null
The DependencyService will call the platform specific function, at the moment I am working with Andorid so, the function is as follows:
public PortableBitmap FileToBitmap(string ms)
{
var bytes = File.ReadAllBytes(ms);
Android.Graphics.Bitmap bMap = BitmapFactory.DecodeByteArray(bytes, 0, bytes.Length);
int[] intArray = new int[bMap.Width * bMap.Height];
bMap.GetPixels(intArray, 0, bMap.Width, 0, 0, bMap.Width, bMap.Height)
List<byte> result = new List<byte>();
foreach (int intRgb in intArray)
{
Color pixelColor = new Color(intRgb);
result.Add(pixelColor.R);
result.Add(pixelColor.G);
result.Add(pixelColor.B);
}
return new PortableBitmap(result.ToArray(), bMap.Width, bMap.Height);
}
I have looked through some of the posts on SO that are having the same problem and have tried the following:
- Using
BitmapLuminanceSource: still returnsnulland requires the use of another library - Using different bitmap formats for the
RGBLuminanceSource: RGB32, BGR32, ARGB32, ABGR32 (each time changing the FileToBitmap function) - Tried the different
Binarizer,GlobalHistogramBinarizer() - Checked that the file is being read correctly by reading and wrinting it back to a file.
- I have tried using the
MultiFormatReader()with the Pure barcode and try harder hints - I have also debugged the libraries source code and from what I understand it just can't find the QR code in the imported image. No exception is thrown.
Here is where the return null is made:
private FinderPattern[] selectBestPatterns()
{
int startSize = possibleCenters.Count;
if (startSize < 3)
{
// Couldn't find enough finder patterns
return null; // It returns here
}
...
The online Zxing decoder can decode the QR code I'm testing correctly. Here is my test QR code:
