最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

如何基于原生javaScript生成帶圖片的二維碼

 更新時(shí)間:2019年11月21日 10:57:37   作者:木子沐沐  
這篇文章主要介紹了如何基于原生javaScript生成帶圖片的二維碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

這篇文章主要介紹了如何基于原生javaScript生成帶圖片的二維碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

使用鏈接生成二維碼主要是使用qr.js或者其他,把鏈接轉(zhuǎn)化為二維碼的形式,在使用canvas時(shí)需要設(shè)置畫布的尺寸,生成的顏色。

<div class="qr_code">
  <img src="" id="imgcode" />
  <canvas ref="canvas" hidden></canvas>
<div>

js

  function createQr () { // 生成帶圖片二維碼
   const qrcode = qr('http://baidu.com') // 轉(zhuǎn)化鏈接
   const canvas = this.$refs.canvas
   const ctx = canvas.getContext('2d')
   const size = 128 / qrcode.moduleCount //128設(shè)置的二維碼尺寸
   const scale = window.devicePixelRatio / getPixelRatio(ctx)
   canvas.height = canvas.width = 128e * scale
   ctx.scale(scale, scale)
   qrcode.modules.forEach((row, rdx) => {
    row.forEach((cell, cdx) => {
     ctx.fillStyle = cell ? '#000' : '#fff' // 設(shè)置二維碼顏色和背景顏色
     var w = (Math.ceil((cdx + 1) * size) - Math.floor(cdx * size))
     ctx.fillRect(Math.round(cdx * size), Math.round(rdx * size), w, w)
    })
   })
   var image = document.createElement('img')
   var imgcode =  document.getElementById('imgcode')
   image.src = 'http://baidu/logo.png' //二維碼中間圖標(biāo)
   image.onload = () => {
    var dwidth = 128 * 0.2 // 設(shè)置圖片大小
    var dx = (128 - dwidth) / 2
    var dheight = image.height / image.width * dwidth
    var dy = (this.size - dheight) / 2
    image.width = dwidth
    image.height = dheight
    ctx.drawImage(image, dx, dy, dwidth, dheight)
    imgcode.src = canvas.toDataURL()
   }
 },
 getPixelRatio (ctx) {
   return ctx.webkitBackingStorePixelRatio || ctx.backingStorePixelRatio || 1
 }

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

乐昌市| 昌黎县| 台南市| 资源县| 苏尼特右旗| 南投县| 定结县| 刚察县| 怀化市| 湘潭县| 郯城县| 阿合奇县| 汉寿县| 靖宇县| 台山市| 南和县| 浦县| 辽阳市| 凤凰县| 寻甸| 云和县| 天柱县| 慈溪市| 招远市| 墨竹工卡县| 木兰县| 上高县| 惠来县| 岐山县| 大安市| 菏泽市| 龙山县| 宜昌市| 仁寿县| 阳西县| 宜州市| 延边| 贵南县| 玛曲县| 黔西县| 定安县|