C#如何实现二维码图片识别
发表于:2024-10-12 作者:千家信息网编辑
千家信息网最后更新 2024年10月12日,这篇文章给大家分享的是有关C#如何实现二维码图片识别的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。具体内容如下怎么用NuGet和怎么配置log4net就不介绍了,直接上代码
千家信息网最后更新 2024年10月12日C#如何实现二维码图片识别
这篇文章给大家分享的是有关C#如何实现二维码图片识别的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。
具体内容如下
怎么用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即可(运行环境上文已附链接)。
后续更新一个批量生成二维码图片的工具,网上除了在线生成的,下载下来的工具都不怎么好用。
using System;using System.IO;using System.Drawing;using System.Configuration;using ThoughtWorks.QRCode.Codec;using ThoughtWorks.QRCode.Codec.Data;using log4net;namespace QRDecodeConsoleApp{ class Program { ////// 私有日志对象 /// private static readonly ILog logger = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); ////// 识别指定目录下的全部二维码图片(默认是PNG) /// /// static void Main(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"]); } //存放结果的文件 string filePath = "txtResult" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".config"; //一个个读取并追加到记录文件 for (int i = 0; i < files.Length; i++) { File.AppendAllText(filePath, CodeDecoder(files[i]) + "\t" + files[i] + "\n");//追加到文件里记录 logger.Info("第" + i + "个识别成功"); Console.WriteLine("第" + i + "个识别成功"); } Console.WriteLine("识别完成,按任意键退出"); Console.ReadLine(); } catch (Exception ex) { Console.WriteLine("识别出错:" + ex.Message); logger.Error("识别出错"); logger.Error("异常描述:\t" + ex.Message); logger.Error("异常方法:\t" + ex.TargetSite); logger.Error("异常堆栈:\t" + ex.StackTrace); Console.ReadLine(); } } ////// 读取图片文件,识别二维码 /// /// 图片文件路劲 ///识别结果字符串 public static string CodeDecoder(string filePath) { string decoderStr; try { if (!System.IO.File.Exists(filePath))//判断有没有需要读取的主文件夹,如果不存在,终止 return null; Bitmap bitMap = new Bitmap(Image.FromFile(filePath));//实例化位图对象,把文件实例化为带有颜色信息的位图对象 QRCodeDecoder decoder = new QRCodeDecoder();//实例化QRCodeDecoder //通过.decoder方法把颜色信息转换成字符串信息 decoderStr = decoder.decode(new QRCodeBitmapImage(bitMap), System.Text.Encoding.UTF8); } catch (Exception ex) { throw ex; } return decoderStr;//返回字符串信息 } }}
感谢各位的阅读!关于"C#如何实现二维码图片识别"这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!
图片
文件
二维
二维码
配置
信息
内容
字符
字符串
实例
对象
C#
成功
位图
参数
工具
方法
更多
篇文章
结果
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
北京定制公交盈利数据库表
苏州微莱博软件开发有限公司
聚声音软件开发
魔兽世界如何使用数据库
软件开发运行中的关键问题
现代网络技术的优势
潢川软件开发有限公司
陈志泊数据库第四版简答题
怀旧服服务器里的外星人
长江云同上一堂网络安全课
发生网络安全事件应当怎么办
b站服务器需要多大磁盘
win10设置代理服务器
数据库01756怎么解决
软件开发项目提高分配
连锁超市的数据库与数据仓库
广州千蛛云网络技术有限公司
早盘光学网络安全
最新可视化数据库软件有哪些
你软件开发工具
软件开发管理体系适用标准
软件开发费用满足资本化
网络安全等级考试有哪些
福建网络服务器机柜云空间
数据库期末考试的名词解释
什么社交软件开发国外客户
服务器配置与管理技术
阿里云服务器续费说明
数据库 dsn
幼儿园网络安全主题活动