一、首先下载 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();
{
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% 纠错能力。
三、生成条形码
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 中画,希望本文的内容对大家的学习或者工作能带来一定的帮助