本文主要介绍如何在.net环境下,基于Asp.Net Core,利用ZXing来生成二维码的一般操作。对二维码工作原理了解,详情见:https://blog.csdn.net/weixin_36191602/article/details/82466148文章介绍。

1、前期准备

  .net core preview8,vs2019(用于支持core3.0),二维码生成插件:开源库ZXIng。相关插件可以在github上找到。安装vs2019后新建.net core web解决方案,也可以右键该解决方案,通过管理解决方案Nuget包功能来找到。如下图:浏览中搜索Zxing第一个既是。选中安装即可。

  可通过项目中依赖性查看相应包的引用。如图:

 2.二维码生成

2.1前端页面

在login.cshtml页面中添加前端元素,主要是一个图片控件。

 <div style="text-align:center">
<div style="margin-top:20px">
<span>扫码获取</span><br/>
<img id="barcode" width="400" height="400" alt="扫码获取" src="Dynpass/GetBarCode"/>
</div>
</div>
src="Dynpass/GetBarCode"表示image数据从DynpassController的GetBarCode方法获取。

2.1后端代码

初始化界面以及二维码资源生成方法:

  public class DynPassController : Controller
{
private readonly BarCodeVue _barCodeContent;//
public DynPassController(IOptions<BarCodeVue> content)
{
this._barCodeContent = content.Value;
} /// <summary>
/// 初始化显示页面
/// </summary>
/// <returns></returns>
[HttpGet]
public IActionResult Login()
{
return View();
} /// <summary>
/// Svn显示==请求获取二维码资源
/// </summary>
/// <returns></returns>
[HttpGet]
public ActionResult GetBarCode()
{
var bar= _barCodeContent != null ? _barCodeContent.BarCode : "扫码获取";
Bitmap bitmap = MyZxingBarcode.GenerateBitmapCode(bar);//扫码获取
System.IO.MemoryStream ms = new System.IO.MemoryStream();
bitmap.Save(ms, ImageFormat.Bmp);
return File(ms.GetBuffer(), "image/png");//
}
}
DynPassController生成二维码的内容即_barCodeContent值由core框架依赖注入(构造该对象时通过构造函数传入)。所以需在ConfigureServices中进行注册。
Barcode类结构
  public class BarCodeVue
{
public string BarCode { get; set; }
}
二维码内容注册

具体步骤:

1.在appsettings.json中添加节点。

 {
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"BarCodeVue": {
"BarCode":"MyBarCode"
}, "AllowedHosts": "*"
}

2.BarCodeVue注册

在Program类中ConfigureServices方法中通过Configure注册。

  // This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => true;
});
services.Configure<BarCodeVue>(Configuration.GetSection("BarCodeVue"));//注册BarCodeVue键值
//services.AddMvc().AddViewOptions(options => options.HtmlHelperOptions.ClientValidationEnabled = true);
services.AddControllersWithViews()
.AddNewtonsoftJson();
services.AddRazorPages();
}

3.生成二维码方法MyZxingBarcode类

  public class MyZxingBarcode
{
/// <summary>
/// 生成二维码,保存成图片
/// </summary>
public static Bitmap GenerateBitmapCode(string content)
{
var writer = new BarcodeWriterPixelData();
writer.Format = BarcodeFormat.QR_CODE;
QrCodeEncodingOptions options = new QrCodeEncodingOptions();
options.DisableECI = true;
//设置内容编码
options.CharacterSet = "UTF-8";
//设置二维码的宽度和高度
options.Width = ;
options.Height = ;
//设置二维码的边距,单位不是固定像素
options.Margin = ;
writer.Options = options;
//
var pixdata = writer.Write(content);
var map = PixToBitmap(pixdata.Pixels, pixdata.Width, pixdata.Height);
//string filename = @"D:\generate1.png";
//map.Save(filename, ImageFormat.Bmp);
return map;
} /// <summary>
/// 将一个字节数组转换为位图
/// </summary>
/// <param name="pixValue">显示字节数组</param>
/// <param name="width">图像宽度</param>
/// <param name="height">图像高度</param>
/// <returns>位图</returns>
private static Bitmap PixToBitmap(byte[] pixValue, int width, int height)
{
//// 申请目标位图的变量,并将其内存区域锁定
var m_currBitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb);
var m_rect = new Rectangle(, , width, height);
var m_bitmapData = m_currBitmap.LockBits(m_rect, ImageLockMode.WriteOnly, PixelFormat.Format32bppRgb); IntPtr iptr = m_bitmapData.Scan0; // 获取bmpData的内存起始位置 //// 用Marshal的Copy方法,将刚才得到的内存字节数组复制到BitmapData中
System.Runtime.InteropServices.Marshal.Copy(pixValue, , iptr, pixValue.Length);
m_currBitmap.UnlockBits(m_bitmapData);
//// 算法到此结束,返回结果 return m_currBitmap; ////初始化条形码格式,宽高,以及PureBarcode=true则不会留白框
//var writer = new BarcodeWriterPixelData
//{
// Format = BarcodeFormat.QR_CODE,
// Options = new ZXing.Common.EncodingOptions { Height = 31, Width = 167, PureBarcode = true, Margin = 1 }
//};
//var pixelData = writer.Write("123236699555555555559989966");
//using (var bitmap = new Bitmap(pixelData.Width, pixelData.Height, PixelFormat.Format32bppRgb))
//using (var ms = new MemoryStream())
//{
// var bitmapData = bitmap.LockBits(new Rectangle(0, 0, pixelData.Width, pixelData.Height),
// System.Drawing.Imaging.ImageLockMode.WriteOnly, PixelFormat.Format32bppRgb);
// try
// {
// // we assume that the row stride of the bitmap is aligned to 4 byte multiplied by the width of the image
// System.Runtime.InteropServices.Marshal.Copy(pixelData.Pixels, 0, bitmapData.Scan0, pixelData.Pixels.Length);
// }
// finally
// {
// bitmap.UnlockBits(bitmapData);
// }
// // save to stream as PNG
// bitmap.Save(ms, ImageFormat.Png);
// Image image = Bitmap.FromStream(ms, true);
// image.Save(@"D:\content.png");
// byte[] bytes = ms.GetBuffer();
//}
}
}

运行生成结果:

遗留问题:

当barcode包含中文时,生成二维码扫码得出结果是乱码。网上找了一些解决方案均不行。有时间在研究吧。在此记录作个记录。

基于Asp.Net Core,利用ZXing来生成二维码的一般流程的更多相关文章

  1. 【VB.NET】利用 ZXing.Net 生成二维码(支持自定义LOGO)

    有任何疑问请去我的新博客提出 https://blog.clso.fun/posts/2019-03-03/vb-net-zxing-net-qr-maker.html ZXing .NET 的项目主 ...

  2. 利用QrCode.Net生成二维码 asp.net mvc c#

    利用QrCode.Net生成二维码 asp.net mvc c# 里面介绍了.net的方式及js的方式,还不错. 里面用到的qrcode.net的类库下载地址:https://qrcodenet.co ...

  3. C# ZXing.Net生成二维码、识别二维码、生成带Logo的二维码(二)

    1.使用ZXint.Net生成带logo的二维码 /// <summary> /// 生成带Logo的二维码 /// </summary> /// <param name ...

  4. C# 动态创建SQL数据库(二) 在.net core web项目中生成二维码 后台Post/Get 请求接口 方式 WebForm 页面ajax 请求后台页面 方法 实现输入框小数多 自动进位展示,编辑时实际值不变 快速掌握Gif动态图实现代码 C#处理和对接HTTP接口请求

    C# 动态创建SQL数据库(二) 使用Entity Framework  创建数据库与表 前面文章有说到使用SQL语句动态创建数据库与数据表,这次直接使用Entriy Framwork 的ORM对象关 ...

  5. 利用google api生成二维码名片

    利用google api生成二维码名片 二维条码/二维码可以分为堆叠式/行排式二维条码和矩阵式二维条码.堆叠式/行排式二维条码形态上是由多行短截的一维条码堆叠而成:矩阵式二维条码以矩阵的形式组成,在矩 ...

  6. 利用Spring Boot+zxing,生成二维码还能这么简单

    在网站开发中,经常会遇到要生成二维码的情况,比如要使用微信支付.网页登录等,本文分享一个Spring Boot生成二维码的例子,这里用到了google的zxing工具类. 本文目录 一.二维码简介二. ...

  7. Java中使用google.zxing快捷生成二维码(附工具类源码)

    移动互联网时代,基于手机端的各种活动扫码和收付款码层出不穷:那我们如何在Java中生成自己想要的二维码呢?下面就来讲讲在Java开发中使用 google.zxing 生成二维码. 一般情况下,Java ...

  8. 使用python调用zxing库生成二维码图片

    (1)     安装Jpype 用python调用jar包须要安装jpype扩展,在Ubuntu上能够直接使用apt-get安装jpype扩展 $ sudo apt-get install pytho ...

  9. 利用google api生成二维码名片例子

    二维条码/二维码可以分为堆叠式/行排式二维条码和矩阵式二维条码.堆叠式/行排式二维条码形态上是由多行短截的一维条码堆叠而成:矩阵式二维条码以矩阵的形式组成,在矩阵相应元素位置上用“点”表示二进制“1” ...

随机推荐

  1. 使用Blazor Server 线路处理程序 (circuit handler)跟踪打开的SignalR连接

    Blazor服务器允许定义线路处理程序(circuit handler)代码,该处理程序(handler)允许在更改用户线路状态时运行此代码. 线路处理程序(circuit handler)是通过从C ...

  2. 学习Java技术哪家强

    https://github.com/CyC2018/CS-Notes https://github.com/Snailclimb/JavaGuide SpringBoot 之 配置文件优先级 htt ...

  3. JavaScript实现树结构(二)

    JavaScript实现树结构(二) 一.二叉搜索树的封装 二叉树搜索树的基本属性: 如图所示:二叉搜索树有四个最基本的属性:指向节点的根(root),节点中的键(key).左指针(right).右指 ...

  4. 02 layui 下载和搭建环境

    Layui官方网站 官方网站:https://www.layui.com/ 下载地址:https://res.layui.com/static/download/layui/layui-v2.5.5. ...

  5. 安卓 打飞机 app 开发 第一篇

    先上效果图 其实,当时刚买 htc G8 的时候(那时北京的房价还是6千一平),安卓2.1 ,2.3 的时候就已经有安卓方面的开发的兴趣,但后来就没有弄过... today 突然想起来,手机上连个游戏 ...

  6. linux 读取 USB HID鼠标坐标和点击 在 LCD上显示

    首先要,编译内核时启用了 USB HID 设备.启用了 鼠标 . 在开发板上插入usb 时会有如下提示. 可以看到,多了一个 mouse0 和 eventX 打出来的是我的 联想鼠标. 1, 在 终端 ...

  7. vlc 播放器的点播和广播服务

    vlc 是一个开源的,同时跨平台的播放器.在研究 rtsp 协议时发现,它同时还是一个强大的流媒体服务器 VLM VLM(VideoLAN Manager) 在 vlc 中是一个小型的媒体管理器,它能 ...

  8. 用 jQuery 实现表单验证(摘抄)——选自《锋利的jQuery》(第2版)第5章的例题 5.1.5 表单验证

    5.1.5 表单验证 表单(form)作为 HTML 最重要的一个组成部分,几乎在每个网页上都有体现,例如用户提交信息.用户反馈信息和用户查询信息等,因此它是网站管理者与浏览者之间沟通的桥梁.在表单中 ...

  9. Java多线程并发07——锁在Java中的实现

    上一篇文章中,我们已经介绍过了各种锁,让各位对锁有了一定的了解.接下来将为各位介绍锁在Java中的实现.关注我的公众号「Java面典」了解更多 Java 相关知识点. 在 Java 中主要通过使用sy ...

  10. iOS/macOS推荐个高效苹果开发工具, JSON 转模型代码工具,不再为复杂JSON数据写模型而烦恼,支持Swift/Objective-C,极速转换

    CCJSON 是一款运行在macOS上 JSON 转模型代码工具,不再为复杂JSON数据写模型而烦恼,可识别嵌套模型,字典/数组,支持Swift/Objective-C,操作方便,极速转换.下载 效果 ...