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


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

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

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

    需要直接用的朋友,确认完QRDecodeDemoinDebug下的配置文件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]) + "	" + files[i] + "
    ");//追加到文件里记录
    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("异常描述:	" + ex.Message);
    57                 logger.Error("异常方法:	" + ex.TargetSite);
    58                 logger.Error("异常堆栈:	" + 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)

  • 相关阅读:
    模板 无源汇上下界可行流 loj115
    ICPC2018JiaozuoE Resistors in Parallel 高精度 数论
    hdu 2255 奔小康赚大钱 最佳匹配 KM算法
    ICPC2018Beijing 现场赛D Frog and Portal 构造
    codeforce 1175E Minimal Segment Cover ST表 倍增思想
    ICPC2018Jiaozuo 现场赛H Can You Solve the Harder Problem? 后缀数组 树上差分 ST表 口胡题解
    luogu P1966 火柴排队 树状数组 逆序对 离散化
    luogu P1970 花匠 贪心
    luogu P1967 货车运输 最大生成树 倍增LCA
    luogu P1315 观光公交 贪心
  • 原文地址:https://www.cnblogs.com/xuezhizhang/p/8968515.html
Copyright © 2020-2023  润新知