微信小程序生成二维码并保存到本地方法 已更新第二种办法可生成条形码
微信小程序生成二维码请保存到本地方法
办法一
官方weapp-qrcode插件 github链接
功能完成样子 wxml
js文件
import drawQrcode from './weapp-qrcode.js';
//二维码生成
QRcodeGeneration() {
return new Promise((resolve, reject) => {
wx.hideLoading();
drawQrcode({
text: 'https://***/**?id=1', //您的生成路径
width: 275, //宽度
height: 275, //高度
typeNumber: 7, //非必须,二维码的计算模式,默认值-1
canvasId: 'myQrcode', //必须,绘制的canvasId
correctLevel: 1, //非必须,二维码纠错级别,默认值为高级,取值:{ L: 1, M: 0, Q: 3, H: 2 }
_this: this, //非必须,若在组件中使用,需要传入,v0.7.0+版本支持
callback: () => {
//绘制完成回调 要加定时不然拿不到 暂不知道啥原因~
setTimeout(() => {
//更多配置请前往官方github地址文档查看
wx.canvasToTempFilePath(
{
canvasId: 'myQrcode',
success: (res) => {
resolve(res); //导出临时二维码图片路径
},
},
this
);
}, 500);
},
});
});
},
//保存二维码
async saveQrcode() {
const {
tempFilePath } = await this.QRcodeGeneration(); //获取二维码路径
wx.getImageInfo({
src: tempFilePath,//传入
success: function (ret) {
var path = ret.path;
wx.saveImageToPhotosAlbum({
filePath: path,
success(result) {
//保存到本地
if (result.errMsg === 'saveImageToPhotosAlbum:ok') {
wx.showToast({
title: '保存成功',
});
}
},
});
},
});
},
/**
* 生命周期函数--监听页面加载
*/
onLoad(options) {
wx.showLoading({
title: '正在努力生成~' });
setTimeout(() => {
this.QRcodeGeneration(); //这里加延迟因为怕拿不到canvas的dom
}, 1000);
},
如果github进不去可以直接复制我这个js到项目里 weapp-qrcode.js
/**
* weapp.qrcode.js v1.0.0 (https://github.com/yingye/weapp-qrcode#readme)
*/
var hasOwn = Object.prototype.hasOwnProperty,
toStr = Object.prototype.toString,
defineProperty = Object.defineProperty,
gOPD = Object.getOwnPropertyDescriptor,
isArray = function (t) {
return 'function' == typeof Array.isArray ? Array.isArray(t) : '[object Array]' === toStr.call(t);
},
isPlainObject = function (t) {
if (!t || '[object Object]' !== toStr.call(t)) return !1;
var e,
r = hasOwn.call(t, 'constructor'),
o = t.constructor && t.constructor.prototype && hasOwn.call(t.constructor.prototype, 'isPrototypeOf');
if (t.constructor && !r && !o) return !1;
for (e in t);
return void 0 === e || hasOwn.call(t, e);
},
setProperty = function (t, e) {
defineProperty && '__proto__' === e.name ? defineProperty(t, e.name, {
enumerable: !0, configurable: !0, value: e.newValue, writable: !0 }) : (t[e.name] = e.newValue);
},
getProperty = function (t, e) {
if ('__proto__' === e) {
if (!hasOwn.call(t, e)) return;
if (gOPD) return gOPD(t, e).value;
}
return t[e];
},
extend = function t() {
var e,
r,
o,
n,
i,
a,
s = arguments[0],
u = 1,
l = arguments.length,
h = !1;
for ('boolean' == typeof s && ((h = s), (s = arguments[1] || {
}), (u = 2)), (null == s || ('object' != typeof s && 'function' != typeof s)) && (s = {
}); u < l; ++u)
if (null != (e = arguments[u]))
for (r in e)
(o = getProperty(s, r)),
s !== (n = getProperty(e, r)) && (h && n && (isPlainObject(n) || (i = isArray(n))) ? (i ? ((i = !1), (a = o && isArray(o) ? o : [])) : (a = o && isPlainObject(o) ? o : {
}), setProperty(s, {
name: r, newValue: t(h, a, n) })) : void 0 !== n && setProperty(s, {
name: r, newValue: n }));
return s;
};
function QR8bitByte(t) {
(this.mode = QRMode.MODE_8BIT_BYTE), (this.data = t);
}
function QRCode(t, e) {
(this.typeNumber = t), (this.errorCorrectLevel = e), (this.modules = null), (this.moduleCount = 0), (this.dataCache = null), (this.dataList = new Array());
}
(QR8bitByte.prototype = {
getLength: function (t) {
return this.data.length;
},
write: function (t) {
for (var e = 0; e < this.data.length; e++) t.put(this.data.charCodeAt(e), 8);
},
}),
(QRCode.prototype = {
addData: function (t) {
var e = new QR8bitByte(t);
this.dataList.push(e), (this.dataCache = null);
},
isDark: function (t, e) {
if (t < 0 || this.moduleCount <= t || e < 0 || this.moduleCount <= e) throw new Error(t + ',' + e);
return this.modules[t][e];
},
getModuleCount: function () {
return this.moduleCount;
},
make: function () {
if (this.typeNumber < 1) {
var t = 1;
for (t = 1; t < 40; t++) {
for (var e = QRRSBlock.getRSBlocks(t, this.errorCorrectLevel), r = new QRBitBuffer(), o = 0, n = 0; n < e.length; n++) o += e[n].dataCount;
for (n = 0; n < this.dataList.length; n++) {
var i = this.dataList[n];
r.put(i.mode, 4), r.put(i.getLength(), QRUtil.getLengthInBits(i.mode, t)), i.write(r);
}
if (r.getLengthInBits() <= 8 * o) break;
}
this.typeNumber = t;
}
this.makeImpl(!1, this.getBestMaskPattern());
},
makeImpl: function (t, e) {
(this.moduleCount = 4 * this.typeNumber + 17), (this.modules = new Array(this.moduleCount));
for (var r = 0; r < this.moduleCount; r++) {
this.modules[r] = new Array(this.moduleCount);
for (var o = 0; o < this.moduleCount; o++) this.modules[r][o] = null;
}
this.setupPositionProbePattern(0, 0),
this.setupPositionProbePattern(this.moduleCount - 7, 0),
this.setupPositionProbePattern(0, this.moduleCount - 7),
this.setupPositionAdjustPattern(),
this.setupTimingPattern(),
this.setupTypeInfo(t, e),
this.typeNumber >= 7 && this.setupTypeNumber(t),
null == this.dataCache && (this.dataCache = QRCode.createData(this.typeNumber, this.errorCorrectLevel, this.dataList)),
this.mapData(this.dataCache, e);
},
setupPositionProbePattern: function (t, e) {
for (var r = -1; r <= 7; r++)
if (!(t + r <= -1 || this.moduleCount <= t + r)) for (var o = -1; o <= 7; o++) e + o <= -1 || this.moduleCount <= e + o || (this.modules[t + r][e + o] = (0 <= r && r <= 6 && (0 == o || 6 == o)) || (0 <= o && o <= 6 && (0 == r || 6 == r)) || (2 <= r && r <= 4 && 2 <= o && o <= 4));
},
getBestMaskPattern: function () {
for (var t = 0, e = 0, r = 0; r < 8; r++) {
this.makeImpl(!0, r);
var o = QRUtil.getLostPoint(this);
(0 == r || t > o) && ((t = o), (e = r));
}
return e;
},
createMovieClip: function (t, e, r) {
var o = t.createEmptyMovieClip(e, r);
this.make();
for (var