.NET C#利用ZXing生成、识别二维码/条形码

高洛峰
发布: 2017-01-07 11:15:57
原创
2889人浏览过

一、首先下载 zxing.net

地址是:http://zxingnet.codeplex.com/releases/view/117068

然后将对应版本 .dll 拖入项目中,再引用之。

主要是用 BarcodeWriter、BarcodeReader。

二、生成二维码

.NET 平台的代码始终要简单些。

QrCodeEncodingOptions options = new QrCodeEncodingOptions();
options.CharacterSet = "UTF-8";
options.DisableECI = true; // Extended Channel Interpretation (ECI) 主要用于特殊的字符集。并不是所有的扫描器都支持这种编码。
options.ErrorCorrection = ZXing.QrCode.Internal.ErrorCorrectionLevel.H; // 纠错级别
options.Width = 300;
options.Height = 300;
options.Margin = 1;
// options.Hints,更多属性,也可以在这里添加。
 
BarcodeWriter writer = new BarcodeWriter();
writer.Format = BarcodeFormat.QR_CODE;
writer.Options = options;
 
Response.Clear();
using (Bitmap bmp = writer.Write("http://www.cftea.com")) // Write 具备生成、写入两个功能
{
 MemoryStream ms = new MemoryStream();
 {
  bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
 
  Response.ContentType = "image/png";
  Response.BinaryWrite(ms.ToArray());
 }
}
Response.End();
登录后复制

   

纠错级别:

    L - 约 7% 纠错能力。

    M - 约 15% 纠错能力。

    Q - 约 25% 纠错能力。

    H - 约 30% 纠错能力。

三、生成条形码

QrCodeEncodingOptions options = new QrCodeEncodingOptions();
options.CharacterSet = "UTF-8";
options.Width = 300;
options.Height = 50;
options.Margin = 1;
options.PureBarcode = false; // 是否是纯码,如果为 false,则会在图片下方显示数字
 
BarcodeWriter writer = new BarcodeWriter();
writer.Format = BarcodeFormat.CODE_128;
writer.Options = options;
 
Response.Clear();
using (Bitmap bmp = writer.Write("12345678"))
{
 MemoryStream ms = new MemoryStream();
 {
  bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
 
  Response.ContentType = "image/png";
  Response.BinaryWrite(ms.ToArray());
 }
}
Response.End();
登录后复制

   

四、识别二维码、条形码

BarcodeReader reader = new BarcodeReader();
reader.Options.CharacterSet = "UTF-8";
using (Bitmap bmp = new Bitmap("D:\qr.png"))
{
 Result result = reader.Decode(bmp);
 Response.Write(result.Text);
}
登录后复制

   

总结

好了,以上就是这篇文章的全部内容了,如果要改变背景颜色、画头像,可以直接在 Bitmap 中画,希望本文的内容对大家的学习或者工作能带来一定的帮助

更多.NET C#利用ZXing生成、识别二维码/条形码相关文章请关注PHP中文网!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号