title: Reading QR code in Xamarin Forms With Zxing
body: <p>I am trying to import a <strong>QR code</strong> from a <em>.png file</em> and decode it using <a href="https://www.nuget.org/packages/ZXing.Net.Mobile/" rel="nofollow noreferrer">Zxing.Net.Mobile</a> and <a href="https://www.nuget.org/packages/ZXing.Net.Mobile.Forms" rel="nofollow noreferrer">ZXing.Net.Mobile.Forms</a>.</p>
  
  <p>If I scan the QR code using the <code>ZXing.Mobile.MobileBarcodeScanner</code> class, the decoding works as required, however, when importing it from a file, the Qr code reader (<code>ZXing.QrCode.QRCodeReader()</code>) decode function always returns <code>null</code>.</p>
  
  <p>As I'm using <strong>Xamarin Forms</strong>; each platform handles the bitmap/image creation and the portable part handle the rest (<code>Zxing BinaryBitmap</code> creation and decoding).</p>
  
  <pre><code>//Store rawBytes and image demensions
  PotableBitmap bMap = DependencyService.Get&lt;IBitmap&gt;().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
  </code></pre>
  
  <p>The <code>DependencyService</code> will call the platform specific function, at the moment I am working with Andorid so, the function is as follows:</p>
  
  <pre><code>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&lt;byte&gt; result = new List&lt;byte&gt;();
      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);
  }
  </code></pre>
  
  <p>I have looked through some of the posts on SO that are having the same problem and have tried the following:</p>
  
  <ul>
  <li>Using <code>BitmapLuminanceSource</code>: still returns <code>null</code> and requires the use of another library</li>
  <li>Using different <em>bitmap formats</em> for the <code>RGBLuminanceSource</code>: RGB32, BGR32, ARGB32, ABGR32 (each time changing the FileToBitmap function)</li>
  <li>Tried the different <code>Binarizer</code>, <code>GlobalHistogramBinarizer()</code></li>
  <li>Checked that the file is being read correctly by reading and wrinting it back to a file.</li>
  <li>I have tried using the <code>MultiFormatReader()</code> with the Pure barcode and try harder hints</li>
  <li>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.</li>
  </ul>
  
  <p>Here is where the return <code>null</code> is made:</p>
  
  <pre><code>private FinderPattern[] selectBestPatterns()
      {
          int startSize = possibleCenters.Count;
          if (startSize &lt; 3)
          {
              // Couldn't find enough finder patterns
              return null; // It returns here
          }
          ...
  </code></pre>
  
  <p>The online <a href="https://zxing.org/w/decode.jspx" rel="nofollow noreferrer">Zxing decoder</a> can decode the QR code I'm testing correctly. Here is my test QR code:</p>
  
  <p><a href="https://i.stack.imgur.com/pu9bE.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/pu9bE.png" alt="My QR Code Image"></a></p>