0

I am using zxing library to generate and decode the QR codes. I my application I am generating QR code dynamically and sending the file containing QR by fax API. If I get this fax message from the api and decode it, Qr code is read successfully, but when I send a scanned copy of this file by fax and then receive and read it I am unable to do that. But if I try to read this file using my mobile Qr application it properly reads the Qr code. I am unable to find a solution how to read this file.

Methods used for encoding:

public static System.Drawing.Image GenerateJSONQrCode(QRJsonFax model)
    {
        var jsonString = JsonConvert.SerializeObject(model);
        //encrypt json string
        jsonString = Hugo.BLL.Utilities.EncryptionHelper.EncryptQR(jsonString, FaxSetting.IsUseHashing);
        var bw = new ZXing.BarcodeWriter();
        var encOptions = new ZXing.Common.EncodingOptions() { Width = 200, Height = 200, Margin = 0 };
        bw.Options = encOptions;
        bw.Format = ZXing.BarcodeFormat.QR_CODE;
        var image = new Bitmap(bw.Write(Compress(jsonString)));
        return image;
    }

private static string Compress(string text)
    {
        byte[] buffer = Encoding.UTF8.GetBytes(text);
        var ms = new MemoryStream();
        using (var zip = new GZipStream(ms, CompressionMode.Compress, true))
        {
            zip.Write(buffer, 0, buffer.Length);
        }

        ms.Position = 0;
        byte[] compressed = new byte[ms.Length];
        ms.Read(compressed, 0, compressed.Length);

        byte[] gzBuffer = new byte[compressed.Length + 4];
        System.Buffer.BlockCopy(compressed, 0, gzBuffer, 4, compressed.Length);
        System.Buffer.BlockCopy(BitConverter.GetBytes(buffer.Length), 0, gzBuffer, 0, 4);
        return Convert.ToBase64String(gzBuffer);
    }

Methods used for encoding and decoding

 public static FaxReceiver.QrFinder DecodeQrCode(string imagePathToDecode)
    {
        long userId = 0;
            Bitmap bitmapImage = (Bitmap)Image.FromFile(imagePathToDecode);
            ZXing.BarcodeReader barcodeReader = new BarcodeReader() { AutoRotate = true, TryHarder = true }; ;
            Result decode = barcodeReader.Decode(bitmapImage);
            var scanResult = string.Empty;
            if (decode != null)
            {
              scanResult= Decompress(decode.Text);
            }

        if (!string.IsNullOrWhiteSpace(scanResult))
            {
                //decrypt Qr received
                var decryptedString = DecryptionHelper.Decrypt(scanResult, FaxSetting.IsUseHashing);

                //deserialize JSON received
                var resultJson = JsonConvert.DeserializeObject<QRJsonFax>(decryptedString);
                if (resultJson != null)
                {
                    long.TryParse(resultJson.UserID.ToString(), out userId);
                    return new QrFinder()
                    {
                        FilePath = imagePathToDecode,
                        UserId = userId,
                        PageNo = 0,
                        DataSourceID = resultJson.DataSourceID,
                        InboundFaxTypeID = resultJson.InboundFaxTypeID
                    };
                }
            }
        return null;
    }

   private static string Decompress(string compressedText)
    {
        byte[] gzBuffer = Convert.FromBase64String(compressedText);
        using (var ms = new MemoryStream())
        {
            int msgLength = BitConverter.ToInt32(gzBuffer, 0);
            ms.Write(gzBuffer, 4, gzBuffer.Length - 4);

            byte[] buffer = new byte[msgLength];

            ms.Position = 0;
            using (var zip = new GZipStream(ms, CompressionMode.Decompress))
            {
                zip.Read(buffer, 0, buffer.Length);
            }

            return Encoding.UTF8.GetString(buffer);
        }
    }

File containing Qr code enter image description here

Jim Aho
  • 9,932
  • 15
  • 56
  • 87
rupinder18
  • 795
  • 1
  • 18
  • 43

1 Answers1

2

The problem is that the QR Decoder is getting confused by the gaps between the pixels in your faxed image. If we zoom into a corner of it, this is what we see.

enter image description here

The scanner is looking for solid black squares to identify the QR code.

If we shrink the image by 50%, it becomes readable.

enter image description here

See for yourself at http://zxing.org/w/decode?u=http%3A%2F%2Fi.stack.imgur.com%2FSCYsd.png

I would suggest that after receiving the faxed image, you should either shrink it, or apply a filter to ensure that the QR codes are solid black. You could also look at sending it at a smaller resolution to see if that helps.

Terence Eden
  • 14,034
  • 3
  • 48
  • 89
  • thanks for the reply but may I know how did you calculate that by shrinking to 50% Qr will be readable because dimensions of my original image are 1728 * 2148 at its 50% is 86* 1074 but at this dimensions also Qr is not readable but the image provided by you have dimensions 512 * 636 and for this Qr is readable,so basically I want to know what criteria you used to find out the dimensions 512*636 – rupinder18 Nov 23 '15 at 06:15
  • I experimented. I suggest you try a few and see what works best for your images. – Terence Eden Nov 23 '15 at 09:36
  • thanks,actually I have different images and for some just resizing the width to half works fine,for some dimensions provide by you works fine but I am looking for a generic solution can you suggest something how should make a generic solution that will read all Qr images which are atleast readable by mobile scanners. – rupinder18 Nov 23 '15 at 09:51
  • Try resizing to several different sizes and picking the one that works. – Terence Eden Nov 23 '15 at 10:00