当前位置: 移动技术网 > IT编程>开发语言>c# > .NET C#利用ZXing生成、识别二维码/条形码

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

2019年07月18日  | 移动技术网IT编程  | 我要评论

一、首先下载 zxing.net

地址是:

然后将对应版本 .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();

纠错级别:

  1.     l - 约 7% 纠错能力。
  2.     m - 约 15% 纠错能力。
  3.     q - 约 25% 纠错能力。
  4.     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 中画,希望本文的内容对大家的学习或者工作能带来一定的帮助

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网