微信小程序生成二維碼


微信小程序客戶端生成二維碼的方法, 請參考這里: 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)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM