初始化qrcode对象
function rpx2px(value) {
const rate = wx.getSystemInfoSync().windowWidth / 750
return rate * value
}
let qrcode;
const QRCode = require('../../../utils/weapp-qrcode.js')
const qrcodeWidth = rpx2px(500)
onReady: function () {
qrcode = new QRCode('canvas', {
usingIn: this, // usingIn 如果放到组件里使用需要加这个参数
text: "",
image: '',
width: qrcodeWidth,
height: qrcodeWidth,
colorDark: "#000",
colorLight: "white",
correctLevel: QRCode.CorrectLevel.H,
});
},
生成图片
qrcode.makeCode('https://xxx' + val, () => {
// qrcode.makeCode(val, () => {
console.log('make')
})
长按保存图片
// 长按保存
save: function () {
console.log('save')
wx.showActionSheet({
itemList: ['保存图片'],
success: function (res) {
console.log(res.tapIndex)
if (res.tapIndex == 0) {
qrcode.exportImage(function (path) {
wx.saveImageToPhotosAlbum({
filePath: path,
})
})
}
}
})
},
参考:https://www.npmjs.com/package/weapp-qrcode