Home >Backend Development >C#.Net Tutorial >.NET C# uses ZXing to generate and identify QR codes/barcodes

.NET C# uses ZXing to generate and identify QR codes/barcodes

高洛峰
高洛峰Original
2017-01-07 11:15:572494browse

一、首先下载 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中文网!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn