二维码和条形码的生成方法

1. 通过pure-svg-code插件生成二维码和条形码

<!--npm下载插件-->
npm install pure-svg-code --save-dev
<img :src="barCode" alt="条形码">
<img :src="qrCode" alt="二维码">

import { barcode, qrcode, svg2url } from 'pure-svg-code';

getBarCode() {
    <!--svg-->
    const svgString = barcode("1234567890", "code128", {width:'50', barWidth:1, barHeight:50});
    <!--imgUrl-->
    this.barCode = svg2url(svgString);
    
},
getQrCode() {
    <!--svg-->
    const svgString = qrcode({
      content: "1234567890",
      padding: 0,
      width: 100,
      height: 100,
      color: "#000000",
      background: "#ffffff",
      ecl: "M"    
    });
    <!--imgUrl-->
    this.qrCode = svg2url(svgString);
}

2. 使用JsBarcode生成条形码

npm install jsbarcode --save

<img id="barcode"/>

$("#barcode").JsBarcode("Hi!");

 3. 生成带logo的二维码,使用vue-qr插件

npm install vue-qr --save
// main.js
import VueQr from 'vue-qr';
Vue.use(VueQr);
// 应用
<vue-qr :logoSrc="imgUrl" text="hello" :size="360" :callback="callback" />

callback(dataUrl, id) {
    const link = document.createElement('a');
    link.href = dataUrl;
    link.download = 'picName';
    link.click();
}

 

posted @ 2021-11-25 16:39  远看山有色  阅读(1396)  评论(0)    收藏  举报