当前位置: 移动技术网 > IT编程>开发语言>.net > 一个C#二维码图片识别的Demo

一个C#二维码图片识别的Demo

2018年04月28日  | 移动技术网IT编程  | 我要评论

祖达克老鼠,代订飞机票,kb973688

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

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

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

需要直接用的朋友,确认完QRDecodeDemo\bin\Debug下的配置文件QRDecodeConsoleApp.exe.config后,运行QRDecodeConsoleApp.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 
42                 //一个个读取并追加到记录文件
43                 for (int i = 0; i < files.Length; i++)
44                 {
45                     File.AppendAllText(filePath, CodeDecoder(files[i]) + "\t" + files[i] + "\n");//追加到文件里记录
46                     logger.Info("第" + i + "个识别成功");
47                     Console.WriteLine("第" + i + "个识别成功");
48                 }
49                 Console.WriteLine("识别完成,按任意键退出");
50                 Console.ReadLine();
51             }
52             catch (Exception ex)
53             {
54                 Console.WriteLine("识别出错:" + ex.Message);
55                 logger.Error("识别出错");
56                 logger.Error("异常描述:\t" + ex.Message);
57                 logger.Error("异常方法:\t" + ex.TargetSite);
58                 logger.Error("异常堆栈:\t" + ex.StackTrace);
59                 Console.ReadLine();
60             }
61 
62         }
63 
64         /// <summary>
65         /// 读取图片文件,识别二维码
66         /// </summary>
67         /// <param name="filePath">图片文件路劲</param>
68         /// <returns>识别结果字符串</returns>
69         public static string CodeDecoder(string filePath)
70         {
71             string decoderStr;
72             try
73             {
74                 if (!System.IO.File.Exists(filePath))//判断有没有需要读取的主文件夹,如果不存在,终止  
75                     return null;
76 
77                 Bitmap bitMap = new Bitmap(Image.FromFile(filePath));//实例化位图对象,把文件实例化为带有颜色信息的位图对象  
78                 QRCodeDecoder decoder = new QRCodeDecoder();//实例化QRCodeDecoder  
79 
80                 //通过.decoder方法把颜色信息转换成字符串信息  
81                 decoderStr = decoder.decode(new QRCodeBitmapImage(bitMap), System.Text.Encoding.UTF8);
82             }
83             catch (Exception ex)
84             {
85                 throw ex;
86             }
87 
88             return decoderStr;//返回字符串信息  
89         }
90 
91 
92     }
93 }

 

代码链接:(QRDecodeDemo.zip)

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网