一個C#二維碼圖片識別的Demo


怎么用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)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM