微信小程序客户端生成二维码的方法, 请参考这里: https://github.com/demi520/wxapp-qrcode 代码片段如下:
const QR = require("../../utils/qrcode.js"); createQrCode: function (url, canvasId, cavW, cavH) { //调用插件中的draw方法,绘制二维码图片 QR.api.draw(url, canvasId, cavW, cavH); setTimeout(() => { this.canvasToTempImage(); }, 1000); }, //获取临时缓存照片路径,存入data中 canvasToTempImage: function () { var that = this; wx.canvasToTempFilePath({ canvasId: 'mycanvas', success: function (res) { var tempFilePath = res.tempFilePath; console.log(tempFilePath); that.setData({ imagePath: tempFilePath, // canvasHidden:true }); }, fail: function (res) { console.log(res); } }); }, //点击图片进行预览,长按保存分享图片 previewImg: function (e) { var img = this.data.imagePath; console.log(img); wx.previewImage({ current: img, // 当前显示图片的http链接 urls: [img] // 需要预览的图片http链接列表 }) },
另一个方案: net core 服务器端生成二维码图片, 小程序显示图片
private void GenerateQRCode(string CustomOrderNumber, string code) { //You only need five lines of code, to generate and view your first QR code. QRCodeGenerator qrGenerator = new QRCodeGenerator(); QRCodeData qrCodeData = qrGenerator.CreateQrCode(code, QRCodeGenerator.ECCLevel.Q); QRCode qrCode = new QRCode(qrCodeData); Bitmap qrCodeImage = qrCode.GetGraphic(20); var filepath = _host.WebRootPath + "\\images\\qrcode\\" + CustomOrderNumber + "_" + code + ".jpg"; FileStream fs = new FileStream(filepath, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write); qrCodeImage.Save(fs, System.Drawing.Imaging.ImageFormat.Jpeg); fs.Close(); qrCodeImage.Dispose(); }
Nuget引用 QRCoder,现在最新版本是1.3.5 ,需要引用 System.Drawing.Common.dll. 这个发布时,记得把发布目录下的runtime目录整个 上传到服务器.(windows服务器应该是用到 runtimes\win\lib\netcoreapp2.0)