码迷,mamicode.com
首页 > 其他好文 > 详细

分享一个二维码图片识别控制台程序Demo

时间:2018-04-28 17:56:08      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:exception   name   result   image   获得   MF   链接   blog   for   

怎么用NuGet和配置log4net就不介绍了,直接上代码(Visual Studio 2015 下的项目,用的.NET Framework 4.5.2)。

其中QRDecodeConsoleApp.exe.config文件里配置图片路劲(默认为D:\我的文档\Pictures\二维码)、图片类型(默认为*.png)。

也支持在命令行里执行,exe后接图片路劲参数。 

  1 using System;
  2 using System.IO;
  3 using System.Drawing;
  4 using System.Configuration;
  5 using ThoughtWorks.QRCode.Codec;
  6 using ThoughtWorks.QRCode.Codec.Data;
  7 using log4net;
  8 
  9 namespace QRDecodeConsoleApp
 10 {
 11     class Program
 12     {
 13         /// <summary>
 14         /// 私有日志对象
 15         /// </summary>
 16         private static readonly ILog logger = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
 17 
 18         /// <summary>
 19         /// 识别指定目录下的全部二维码图片(默认是PNG)
 20         /// </summary>
 21         /// <param name="args"></param>
 22         static void Main(string[] args)
 23         {
 24             try
 25             {
 26                 string[] files;
 27                 if (args.Length > 0)
 28                 {
 29                     //args[0]为CMD里exe后的第一个参数 ImgType默认配置的*.png
 30                     files = Directory.GetFiles(args[0], ConfigurationManager.AppSettings["ImgType"]);
 31                 }
 32                 else
 33                 {
 34                     //读取指定路劲(QRDecodeConsoleApp.exe.config里配置的路劲)
 35                     files = Directory.GetFiles(ConfigurationManager.AppSettings["QRImgPath"],
 36                                                 ConfigurationManager.AppSettings["ImgType"]);
 37                 }
 38 
 39                 //存放结果的文件
 40                 string filePath = "txtResult" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".config";
 41                 CreateTxtFile(filePath, "");//初始为空
 42 
 43                 //一个个读取并追加到记录文件
 44                 for (int i = 0; i < files.Length; i++)
 45                 {
 46                     File.AppendAllText(filePath, CodeDecoder(files[i]) + "\t" + files[i] + "\n");//追加到文件里记录
 47                     logger.Info("" + i + "个识别成功");
 48                     Console.WriteLine("" + i + "个识别成功");
 49                 }
 50                 Console.WriteLine("识别完成,按任意键退出");
 51                 Console.ReadLine();
 52             }
 53             catch (Exception ex)
 54             {
 55                 Console.WriteLine("识别出错:" + ex.Message);
 56                 logger.Error("识别出错");
 57                 logger.Error("异常描述:\t" + ex.Message);
 58                 logger.Error("异常方法:\t" + ex.TargetSite);
 59                 logger.Error("异常堆栈:\t" + ex.StackTrace);
 60                 Console.ReadLine();
 61             }
 62 
 63         }
 64 
 65         /// <summary>
 66         /// 读取图片文件,识别二维码
 67         /// </summary>
 68         /// <param name="filePath">图片文件路劲</param>
 69         /// <returns>识别结果字符串</returns>
 70         public static string CodeDecoder(string filePath)
 71         {
 72             string decoderStr;
 73             try
 74             {
 75                 if (!System.IO.File.Exists(filePath))//判断有没有需要读取的主文件夹,如果不存在,终止  
 76                     return null;
 77 
 78                 Bitmap bitMap = new Bitmap(Image.FromFile(filePath));//实例化位图对象,把文件实例化为带有颜色信息的位图对象  
 79                 QRCodeDecoder decoder = new QRCodeDecoder();//实例化QRCodeDecoder  
 80 
 81                 //通过.decoder方法把颜色信息转换成字符串信息  
 82                 decoderStr = decoder.decode(new QRCodeBitmapImage(bitMap), System.Text.Encoding.UTF8);
 83             }
 84             catch (Exception ex)
 85             {
 86                 throw ex;
 87             }
 88 
 89             return decoderStr;//返回字符串信息  
 90         }
 91 
 92         /// <summary>
 93         /// 创建文本类型文件
 94         /// </summary>
 95         /// <param name="filename">文件名(.config、.txt文件等)</param>
 96         /// <param name="txtStr">文件内容字符串</param>
 97         static void CreateTxtFile(string filename, string txtStr)
 98         {
 99             try
100             {
101                 FileStream fs = new FileStream(filename, FileMode.Create);//生成文件
102                 byte[] data = System.Text.Encoding.UTF8.GetBytes(txtStr);//获得字节数组
103                 fs.Write(data, 0, data.Length);//开始写入
104                 fs.Flush();//清空缓冲区
105                 fs.Close();//关闭流
106             }
107             catch (Exception ex)
108             {
109                 throw ex;
110             }
111         }
112 
113     }
114 }

 代码链接:(QRDecodeDemo.zip)

 

分享一个二维码图片识别控制台程序Demo

标签:exception   name   result   image   获得   MF   链接   blog   for   

原文地址:https://www.cnblogs.com/xuezhizhang/p/8968515.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!