关于生成\读取 一维码二维码的方法

一、首先下载 ZXing.Net

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

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

主要是用 BarcodeWriter、BarcodeReader。

二、生成二维码

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


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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% 纠错能力。

三、生成条形码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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();

四、识别二维码、条形码

?
1
2
3
4
5
6
7
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 中画,希望本文的内容对大家的学习或者工作能带来一定的帮助

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值