cmy efb59431fd 2024-04-17 hai 8 meses
..
common efb59431fd 2024-04-17 hai 8 meses
components efb59431fd 2024-04-17 hai 8 meses
js_sdk efb59431fd 2024-04-17 hai 8 meses
LICENSE.md efb59431fd 2024-04-17 hai 8 meses
README.md efb59431fd 2024-04-17 hai 8 meses
changelog.md efb59431fd 2024-04-17 hai 8 meses
package.json efb59431fd 2024-04-17 hai 8 meses

README.md

介绍

uQRCode是一款基于Javascript环境开发的二维码生成插件,适用所有Javascript运行环境的前端应用和Node.js应用。

uQRCode可扩展性高,它支持自定义渲染二维码,可通过uQRCode API得到二维码绘制关键信息后,使用canvassvgjs操作dom的方式绘制二维码图案。还可自定义二维码样式,如随机颜色、圆点、方块、块与块之间的间距等。

欢迎加入群聊【uQRCode交流群】:695070434

设计器

uQRCode发布了配套的可视化设计器,可根据自己喜好在设计器中设计二维码样式,一键生成配置代码复制到项目中,详情请在微信小程序搜索“柚子二维码”,或扫描下方小程序码体验。

uQRCode设计器

设计器模板示例

uQRCode设计器 uQRCode设计器 uQRCode设计器 uQRCode设计器 uQRCode设计器 uQRCode设计器 uQRCode设计器 uQRCode设计器 uQRCode设计器

快速上手

uni-app中,我们更推荐使用组件方式来生成二维码,组件方式大大提高了页面的可读性以及避开了一些平台容易出问题的地方,当组件无法满足需求的时候,再考虑切换成原生方式。

官方文档:https://uqrcode.cn/doc

github地址:https://github.com/Sansnn/uQRCode

npm地址:https://www.npmjs.com/package/uqrcodejs

uni-app插件市场地址:https://ext.dcloud.net.cn/plugin?id=1287

原生方式

原生方式仅需要获取uqrcode.js文件便可使用。详细配置请移步到:文档 > 原生

安装

  1. 通过npm安装,成功后即可使用importrequire进行引用。

    # npm安装
    npm install uqrcodejs
    # 或者
    npm install @uqrcode/js
    
    1. 通过项目开源地址获取uqrcode.js,下载uqrcode.js后,将其复制到您项目指定目录,在页面中引入uqrcode.js文件即可开始使用。

    引入

    • 通过import引入。 javascript // npm安装 import UQRCode from 'uqrcodejs'; // npm install uqrcodejs // 或者 import UQRCode from '@uqrcode/js'; // npm install @uqrcode/js
  • Node.js通过require引入。

    // npm安装
    const UQRCode = require('uqrcodejs'); // npm install uqrcodejs
    // 或者
    const UQRCode = require('@uqrcode/js'); // npm install @uqrcode/js
    
    • 原生浏览器环境,在js脚本加载时添加到windowhtml <script type="text/javascript" src="uqrcode.js"></script> <script> var UQRCode = window.UQRCode; </script>

简单用法

uQRCode基于Canvas API封装了一套方法,建议开发者使用canvas生成,一键调用,非常方便。以下是示例:

  • HTML示例

    • DOM部分

      <canvas id="qrcode" width="200" height="200"></canvas>
      
      • JS部分 javascript // 获取uQRCode实例 var qr = new UQRCode(); // 设置二维码内容 qr.data = "https://uqrcode.cn/doc"; // 设置二维码大小,必须与canvas设置的宽高一致 qr.size = 200; // 调用制作二维码方法 qr.make(); // 获取canvas元素 var canvas = document.getElementById("qrcode"); // 获取canvas上下文 var canvasContext = canvas.getContext("2d"); // 设置uQRCode实例的canvas上下文 qr.canvasContext = canvasContext; // 调用绘制方法将二维码图案绘制到canvas上 qr.drawCanvas();
  • uni-app示例

    • Template部分

      <canvas id="qrcode" canvas-id="qrcode" style="width: 200px;height: 200px;"></canvas>
      
      • JS部分 javascript onReady() { // 获取uQRCode实例 var qr = new UQRCode(); // 设置二维码内容 qr.data = "https://uqrcode.cn/doc"; // 设置二维码大小,必须与canvas设置的宽高一致 qr.size = 200; // 调用制作二维码方法 qr.make(); // 获取canvas上下文 var canvasContext = uni.createCanvasContext('qrcode', this); // 如果是组件,this必须传入 // 设置uQRCode实例的canvas上下文 qr.canvasContext = canvasContext; // 调用绘制方法将二维码图案绘制到canvas上 qr.drawCanvas(); }
  • 微信小程序,推荐使用Canvas 2D,关于Canvas 2D的使用请参考微信开放文档。

高级用法

考虑到部分平台可能不支持canvas,所以uQRCode并没有强制要求和canvas一起使用,您还可以选择其他方式来生成二维码,例如使用js操作dom进行绘制或是使用svg绘制等。以下是示例:

  • uni-app v-for+view
<template>
  <view>
    <view class="qrcode">
      <view v-for="(row, rowI) in modules" :key="rowI" style="display: flex;flex-direction: row;">
        <view v-for="(col, colI) in row" :key="colI">
          <view v-if="col.isBlack" style="width: 10px;height: 10px;background-color: black;">
            <!-- 黑色码点 -->
          </view>
          <view v-else style="width: 10px;height: 10px;background-color: white;">
            <!-- 白色码点 -->
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
  import UQRCode from '../../uni_modules/Sansnn-uQRCode/js_sdk/uqrcode/uqrcode.js';

  export default {
    data() {
      return {
        modules: []
      }
    },
    onLoad() {
      const qr = new UQRCode();
      qr.data = 'uQRCode';
      qr.make();
      this.modules = qr.modules;
    },
    methods: {

    }
  }
</script>
  • js操作dom
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>uQRCode二维码生成</title>
</head>
<body>
    <div id="qrcode" style="position: relative;"></div>
    <script type="text/javascript" src="uqrcode.js"></script>
    <script>
    // 引入uQRCode
    var UQRCode = window.UQRCode;
    // 获取uQRCode实例
    var qr = new UQRCode();
    // 设置二维码内容
    qr.data = "https://uqrcode.cn/doc";
    // 设置二维码大小,必须与canvas设置的宽高一致
    qr.size = 200;
    // 设置二维码前景图,可以是路径
    qr.foregroundImageSrc =
        'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGQAAABkCAYAAABw4pVUAAAAAXNSR0IB2cksfwAAAAlwSFlzAAALEwAACxMBAJqcGAAAC3xJREFUeJztnd1vFNcZxodSJ3y3EL7SYIQwu15wI5FSAkqVkISKgEkuSIEC6127RrloL9r8D4n5UFUZp/9C24A/okqUOzCmSdoohQtkXIkiRS1VC7YQF41Kbe/unL7PzHt2z45ndnZmd1l75hzrSSwzMzvn+c15z8ee3dcwdIlkWaRlqSnF62a+4dDiiMtZ36cKyc183NQ3WS2sZ2IqWX/phwTWEDhuEKT5S0hLSctJK1grWasiLllPWe9l7MUSowTJDU7oopKVICSEZXwz3yKtJj1HWkdaT9pA2hgTbeA6r2MPVrMnEpCEI8HU1FpUGC18cbQEPB1r+Ea+Q2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EZWsXyfX+QB78Kphr1W9xB5tZc/WKjDgqW/f4SxqBy+hoKkh/qGj38QvhriIySOeBADCOs3LfFN7I649XNfdXHd40MGebGWP4NVq9k6F4Ruq3IraUtDEEPfQGYE0wGAsjckjmuMWvgm0ngQrGXHJerZx3bewF8+zN2vYK3j2rBEwTLmVRUY5FNlaAAbzFFDHjB5PAMbV6/hG8FRsjIk2cJ3XsQer2ZOV7NESo9QqVBihgMiidvQSTItRgoOmiKdgBWsla1XEJesp672MvZAQWowSCBmiagKhlkUOqXAkIAkpjpL1l344IdQVhrM4X0SFpGpxxOWsr5cvTSleNxM36RK18n+GJEwNAYal3QAAAABJRU5ErkJggg==';
    // 调用制作二维码方法
    qr.make();

    var drawModules = qr.getDrawModules();
    // 遍历drawModules创建dom元素
    var qrHtml = '';
    for (var i = 0; i < drawModules.length; i++) {
        var drawModule = drawModules[i];
        switch (drawModule.type) {
        case 'tile':
            /* 绘制小块 */
            qrHtml += `<div style="position: absolute;left: ${drawModule.x}px;top: ${drawModule.y}px;width: ${drawModule.width}px;height: ${drawModule.height}px;background: ${drawModule.color};"></div>`;
            break;
        case 'image':
            /* 绘制图像 */
            qrHtml += `<img style="position: absolute;left: ${drawModule.x}px;top: ${drawModule.y}px;width: ${drawModule.width}px;height: ${drawModule.height}px;" src="${drawModule.imageSrc}" />`;
            break;
        }
    }
    document.getElementById('qrcode').innerHTML = qrHtml;
    </script>
</body>
</html>
  • svg

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>uQRCode二维码生成</title>
    </head>
    <body>
    <svg id="qrcode" width="200" height="200" xmlns="http://www.w3.org/2000/svg" version="1.1"></svg>
    <script type="text/javascript" src="uqrcode.js"></script>
    <script>
    // 引入uQRCode
    var UQRCode = window.UQRCode;
    // 获取uQRCode实例
    var qr = new UQRCode();
    // 设置二维码内容
    qr.data = "https://uqrcode.cn/doc";
    // 设置二维码大小,必须与canvas设置的宽高一致
    qr.size = 200;
    // 设置二维码前景图,可以是路径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WjhAYiINZJ6pkUD018TWB0jor4nVX6TcWCCBv0hZf9V4Y4D0GAG/alx/GX9jgQT+Mn6drqJBMBiIM13FumqA6IQuDQDikdBFJgZzTegiociUR8hfWJbyaGpq6p+6lQSHgRIm5ZEKRCYFQ9bjYlKwGzdu6KRgIWCguCQFQ8K1qpKCqSOt9dyPICHi/uPHj+u0eQEgyALPkHLQmJs2Dx77ps2rlFiy89atW9d870CXsnLz5s1RpXUETiyphi2ZehWtxEq9unnz5mOPHj263+xKLpQyOTn5VWtrKzJp7zPKU6/KrNG+abzVsOWanLijo+OETk7sX+AREjkb7smJZevwDFfOsAVyiG9e6bs7OX33RZ2+2y5K+u5LnL6706hT+m61L1ET3Lca7gnukdRdJ7ivnOC+1QiZ4F6FIkOXhAK6aHKIg+joMWLAkPg1vgHMQrE0gCfjbdY7EZWsXyfX+QB78Kphr1W9xB5tZc/WKjDgqW/f4SxqBy+hoKkh/qGj38QvhriIySOeBADCOs3LfFN7I649XNfdXHd40MGebGWP4NVq9k6F4Ruq3IraUtDEEPfQGYE0wGAsjckjmuMWvgm0ngQrGXHJerZx3bewF8+zN2vYK3j2rBEwTLmVRUY5FNlaAAbzFFDHjB5PAMbV6/hG8FRsjIk2cJ3XsQer2ZOV7NESo9QqVBihgMiidvQSTItRgoOmiKdgBWsla1XEJesp672MvZAQWowSCBmiagKhlkUOqXAkIAkpjpL1l344IdQVhrM4X0SFpGpxxOWsr5cvTSleNxM36RK18n+GJEwNAYal3QAAAABJRU5ErkJggg==';
    // 调用制作二维码方法
    qr.make();
    
    var drawModules = qr.getDrawModules();
    // 遍历drawModules创建svg元素
    var qrHtml = '';
    for (var i = 0; i < drawModules.length; i++) {
        var drawModule = drawModules[i];
        switch (drawModule.type) {
        case 'tile':
            /* 绘制小块 */
            qrHtml += `<rect x="${drawModule.x}" y="${drawModule.y}" width="${drawModule.width}" height="${drawModule.height}" style="fill: ${drawModule.color};" />`;
            break;
        case 'image':
            /* 绘制图像 */
            qrHtml += `<image href="${drawModule.imageSrc}" x="${drawModule.x}" y="${drawModule.y}" width="${drawModule.width}" height="${drawModule.height}" />`;
            break;
        }
    }
    document.getElementById('qrcode').innerHTML = qrHtml;
    </script>
    </body>
    </html>
    

    更多用法大家自行探索咯,期待分享哟~

    导出临时文件路径

    原生方式基于Canvas的,请自行参阅各平台Canvas的导出方式。以下是部分示例:

    • uni-app javascript // 通过uni.createCanvasContext方式创建绘制上下文的,对应导出API为uni.canvasToTempFilePath // 调用完ctx.draw()方法后不能第一时间导出,否则会异常,需要有一定的延时 setTimeout(() => { uni.canvasToTempFilePath( { canvasId: this.canvasId, fileType: this.fileType, width: this.canvasWidth, height: this.canvasHeight, success: res => { console.log(res); }, fail: err => { console.log(err); } }, // this // 组件内使用必传当前实例 ); }, 300);
  • Canvas2D

    // 得到base64
    console.log(canvas.toDataURL());
    // 得到buffer
    console.log(canvas.toBuffer());
    

    保存二维码到本地相册

    必须在导出临时文件路径成功后再执行保存。uni-app通用保存方式(H5除外):

    uni.saveImageToPhotosAlbum({
    filePath: tempFilePath,
    success: res => {
        console.log(res);
    },
    fail: err => {
        console.log(err);
    }
    });
    

H5可以通过设置<a>标签href属性的方式进行保存:

const aEle = document.createElement('a');
aEle.download = 'uQRCode'; // 设置下载的文件名,默认是'下载'
aEle.href = tempFilePath;
document.body.appendChild(aEle);
aEle.click();
aEle.remove(); // 下载之后把创建的元素删除

经过测试,PC端浏览器可以下载,部分安卓自带或第三方浏览器可以下载,安卓微信浏览器不适用,移动端iOS所有浏览器均不适用,差异较大,还是推荐各位导出文件给图片组件显示,然后提示用户通过长按图片进行保存这种方式。

uni-app组件方式

安装

通过uni-app插件市场地址安装:https://ext.dcloud.net.cn/plugin?id=1287。详细配置请移步到:文档 > uni-app组件

引入

uni-app默认为easycom模式,可直接键入<uqrcode>标签。

简单用法

安装uqrcode组件后,在template中键入<uqrcode/>。设置ref属性可使用组件内部方法,canvas-id属性为组件内部的canvas组件标识,value属性为二维码生成对应内容,options为配置选项,可配置二维码样式,绘制Logo等,详见:options

<uqrcode ref="uqrcode" canvas-id="qrcode" value="https://uqrcode.cn/doc" :options="{ margin: 10 }"></uqrcode>

导出临时文件路径

为了保证方法调用成功,请在 complete 事件返回success=true后调用。

// uqrcode为组件的ref名称
this.$refs.uqrcode.toTempFilePath({
  success: res => {
    console.log(res);
  }
});

保存二维码到本地相册

为了保证方法调用成功,请在 complete 事件返回success=true后调用。

// uqrcode为组件的ref名称
this.$refs.uqrcode.save({
  success: () => {
    uni.showToast({
      icon: 'success',
      title: '保存成功'
    });
  }
});

更多使用说明请前往官方文档查看:https://uqrcode.cn/doc