Razack
Razack

Reputation: 950

Zxing QR Code decode function returns null in c#

I am getting null while using the code below. I am using ZXing dll downloaded from NuGet

    using ZXing.Common;
    using ZXing.QrCode;
    using ZXing.QrCode.Internal;

    private void Decode()
    {
        Bitmap bitmap = new Bitmap(@"D:\Project\QRCodes\myqrcode.png");
        try
        {
            MemoryStream memoryStream = new MemoryStream();
            bitmap.Save(memoryStream, ImageFormat.Bmp);

            byte[] byteArray = memoryStream.GetBuffer();

            ZXing.LuminanceSource source = new RGBLuminanceSource(byteArray, bitmap.Width, bitmap.Height);
            var binarizer = new HybridBinarizer(source);
            var binBitmap = new BinaryBitmap(binarizer);
            QRCodeReader qrCodeReader = new QRCodeReader();

            Result str = qrCodeReader.decode(binBitmap);

        }
        catch{ }

    }

Please give me a solution Thanks in advance

Upvotes: 3

Views: 10722

Answers (1)

Razack
Razack

Reputation: 950

problom solved I got it working by using below code

         Bitmap bitmap = new Bitmap(@"D:\Project\QRCodes\myqrcode.png");
         try
        {               
            BarcodeReader reader = new BarcodeReader { AutoRotate = true, TryHarder = true };
            Result result = reader.Decode(bitmap);
            string decodedData = result.Text;                
        }
        catch
        {
            throw new Exception("Cannot decode the QR code");
        }

Upvotes: 7

Related Questions