QRCodeReader Portable decode result null

Jun 18, 2014 at 6:37 PM
I'm trying to unit test a service that will decode images. This is a portable class library service as follows:
        public string Decode(byte[] qrImage, int width, int height)
        {
            var luminance = new RGBLuminanceSource(qrImage, width, height);
            var binarizer = new HybridBinarizer(luminance);
            var binaryBitmap = new BinaryBitmap(binarizer);
            var result = reader.decode(binaryBitmap);

            return result.Text; // <-- result is null
        }
The test as follows:
        [TestMethod]
        [DeploymentItem("Assets/Images/qrcode.png")]
        public void Decode_Returns_Correct_Text()
        {
            var expected = "hello";
            // Arrange
            Image returnImage = Image.FromFile("qrcode.png");
            MemoryStream ims = new MemoryStream();
            returnImage.Save(ims, System.Drawing.Imaging.ImageFormat.Png);
            var img = ims.ToArray();

            // Act
            QrCodeService service = new QrCodeService();
            var result = service.Decode(img, returnImage.Width, returnImage.Height);

            // Assert
            Assert.AreEqual(expected, result, "result does not match");
        }
What is the proper way to decode a qr code image in a portable class library that will be used in a xamarin cross platform android and ios project?
Coordinator
Jun 19, 2014 at 6:21 PM
The following lines are wrong if you want to use the array "img" with the Decode method:
            MemoryStream ims = new MemoryStream();
            returnImage.Save(ims, System.Drawing.Imaging.ImageFormat.Png);
            var img = ims.ToArray();
In that case the array contains the binary data of the PNG file format.
The Decode method expects the raw RGB data, no file format headers or specific file format data.
You can take a look at the constructor of the class BitmapLuminanceSource which shows a way how to access the raw pixel data of an image.