Share a QR code image recognition console program Demo

How to use NuGet and configure log4net will not be introduced, directly on the code ( QRDecodeDemo.zip ).

(Project under Visual Studio 2015, using .NET Framework 4.5.2)

Tucao about the inability to insert code blocks in the blog park... The typesetting is ugly, as follows: 

using System;

using System.IO;

using System.Drawing;
using System.Configuration;
using ThoughtWorks.QRCode.Codec;
using ThoughtWorks.QRCode.Codec.Data;
using log4net;
namespace QRDecodeConsoleApp
{
    class Program
    {
        /// <summary>
        /// Private log object
        /// </summary>
        private static readonly ILog logger = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        /// <summary>
        /// Identify all QR code images (PNG) in the specified directory
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            try
            {
                string[] files;
                if (args.Length > 0)
                {
                    files = Directory.GetFiles(args[0], "*.png");//Parameters after exe in CMD
                }
                else
                {
                    //Read the specified road strength (the road strength configured in QRDecodeConsoleApp.exe.config)
                    files = Directory.GetFiles(ConfigurationManager.AppSettings["QRImgPath"], "*.png");
                }
                // file to store the result
                string filePath = "txtResult" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".config";
                CreateTxtFile(filePath, "");//initially empty
                // read and append to the record file one by one
                for (int i = 0; i < files.Length; i++)
                {
                    File.AppendAllText(filePath, CodeDecoder(files[i]) + "\t" + files[i] + "\n");//Append to the file record
                    logger.Info("The first" + i + "successful identification");
                    Console.WriteLine("" + i + "successful recognition");
                }
                Console.WriteLine("Recognition completed, press any key to exit");
                Console.ReadLine();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Recognition error: " + ex.Message);
                logger.Error("Identification error");
                logger.Error("Exception description:\t" + ex.Message);
                logger.Error("Exception method:\t" + ex.TargetSite);
                logger.Error("Exception stack: \t" + ex.StackTrace);
                Console.ReadLine();
            }
        }
        /// <summary>
        /// Read the image file and identify the QR code
        /// </summary>
        /// <param name="filePath">image file path</param>
        /// <returns>Identification result string</returns>
        public static string CodeDecoder(string filePath)
        {
            string decoderStr;
            try
            {
                if (!System.IO.File.Exists(filePath))//Determine whether there is a main folder that needs to be read, if it does not exist, terminate  
                    return null;
                Bitmap bitMap = new Bitmap(Image.FromFile(filePath));//Instantiate the bitmap object and instantiate the file as a bitmap object with color information  
                QRCodeDecoder decoder = new QRCodeDecoder();//实例化QRCodeDecoder  
                decoderStr = decoder.decode(new QRCodeBitmapImage(bitMap), System.Text.Encoding.UTF8);//Convert color information into string information through the .decoder method  
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return decoderStr;//Return string information  
        }
        /// <summary>
        /// Create a text type file
        /// </summary>
        /// <param name="filename">File name (.config, .txt file, etc.)</param>
        /// <param name="txtStr">File content string</param>
        static void CreateTxtFile(string filename, string txtStr)
        {
            try
            {
                FileStream fs = new FileStream(filename, FileMode.Create);//Generate file
                byte[] data = System.Text.Encoding.UTF8.GetBytes(txtStr);//Get byte array
                fs.Write(data, 0, data.Length);//Start writing
                fs.Flush();//Empty the buffer
                fs.Close();//Close the stream
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325079851&siteId=291194637