Read QR Code using ZXing library in Windows Console application (C#)

1. Download the library from http://zxingnet.codeplex.com/
2. Create a new Console Application
3. Add reference zxing.dll to your project
4. Paste the following sample code (.Net 4.0) in your Program.cs file

using System;
using ZXing;
using ZXing.Common;
using System.Drawing;

namespace ZXingConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            string fileName = @"d:\tmp\qr.jpg"; //test file
            if (args.Length >0)
                fileName = args[0];

            Console.WriteLine(GetImage(fileName));
            Console.ReadKey();
        }

        private static string GetImage(string fName)
        {
            string ret = "";
            using (var bitmap = (Bitmap)Bitmap.FromFile(fName))
            {
                ret = Decode(bitmap);
            }
            return ret;
        }

        private static string Decode(Bitmap image)
        {
            BarcodeReader barcodeReader = new BarcodeReader
            {
                AutoRotate = true,
                TryInverted = true,
                Options = new DecodingOptions 
                    { TryHarder = true }
            };

            var result = barcodeReader.Decode(image);
            if (result != null)
                return "QR-Code:" + result.Text.ToUpper();

            return "ERROR";
        }
    }
}
This entry was posted in .Net. Bookmark the permalink.