C#二维码图片识别代码
本文实例为大家分享了C#二维码图片识别的具体代码,供大家参考,具体内容如下
怎么用NuGet和怎么配置log4net就不介绍了,直接上代码(VisualStudio2015下的项目,用的.NETFramework4.5.2)。
其中QRDecodeConsoleApp.exe.config文件里配置图片路劲(默认为D:\我的文档\Pictures\二维码)、图片类型(默认为*.png)。
也支持在命令行里执行,exe后接图片路劲参数。
需要直接用的朋友,确认完QRDecodeDemo\bin\Debug下的配置文件QRDecodeConsoleApp.exe.config后,运行QRDecodeConsoleApp.exe即可(运行环境上文已附链接)。
后续更新一个批量生成二维码图片的工具,网上除了在线生成的,下载下来的工具都不怎么好用。
usingSystem;
usingSystem.IO;
usingSystem.Drawing;
usingSystem.Configuration;
usingThoughtWorks.QRCode.Codec;
usingThoughtWorks.QRCode.Codec.Data;
usinglog4net;
namespaceQRDecodeConsoleApp
{
classProgram
{
///
///私有日志对象
///
privatestaticreadonlyILoglogger=LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
///
///识别指定目录下的全部二维码图片(默认是PNG)
///
///
staticvoidMain(string[]args)
{
try
{
string[]files;
if(args.Length>0)
{
//args[0]为CMD里exe后的第一个参数ImgType默认配置的*.png
files=Directory.GetFiles(args[0],ConfigurationManager.AppSettings["ImgType"]);
}
else
{
//读取指定路劲(QRDecodeConsoleApp.exe.config里配置的路劲)
files=Directory.GetFiles(ConfigurationManager.AppSettings["QRImgPath"],
ConfigurationManager.AppSettings["ImgType"]);
}
//存放结果的文件
stringfilePath="txtResult"+DateTime.Now.ToString("yyyyMMddHHmmssfff")+".config";
//一个个读取并追加到记录文件
for(inti=0;i
///读取图片文件,识别二维码
///
///图片文件路劲
///识别结果字符串
publicstaticstringCodeDecoder(stringfilePath)
{
stringdecoderStr;
try
{
if(!System.IO.File.Exists(filePath))//判断有没有需要读取的主文件夹,如果不存在,终止
returnnull;
BitmapbitMap=newBitmap(Image.FromFile(filePath));//实例化位图对象,把文件实例化为带有颜色信息的位图对象
QRCodeDecoderdecoder=newQRCodeDecoder();//实例化QRCodeDecoder
//通过.decoder方法把颜色信息转换成字符串信息
decoderStr=decoder.decode(newQRCodeBitmapImage(bitMap),System.Text.Encoding.UTF8);
}
catch(Exceptionex)
{
throwex;
}
returndecoderStr;//返回字符串信息
}
}
}
代码链接:(QRDecodeDemo.zip)
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。