這篇文章主要介紹了基于原生javaScript生成帶圖片二維碼的方法,具有一定借鑒價(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 = '/file/tupian/20230213/404.html' //二維碼中間圖標(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
}感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“基于原生javaScript生成帶圖片二維碼的方法”這篇文章對大家有幫助,同時(shí)也希望大家多多支持創(chuàng)新互聯(lián),關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關(guān)知識等著你來學(xué)習(xí)!
分享標(biāo)題:基于原生javaScript生成帶圖片二維碼的方法-創(chuàng)新互聯(lián)
路徑分享:http://chinadenli.net/article14/ecoge.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)建站、網(wǎng)站維護(hù)、服務(wù)器托管、網(wǎng)站策劃、微信公眾號、品牌網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容