现在的需求是链接蓝牙打印机打印二维码,我使用的方案是使用canvas生成二维码,在获取canvasImageData转换成buffer然后分组进行打印,但是这种方式非常慢,有什么更好的方式进行打印么?
以下为生成canvas二维码转换buffer部分代码:
printImgT(text, callback) { //二维码
let tthis = this;
const ctx = wx.createCanvasContext('shareCanvas');
ctx.clearRect(0, 0, 200, 200);
drawQrcode({
canvasId: 'shareCanvas',
text: String(text),
width: 200,
height: 200,
callback(e) {
wx.canvasGetImageData({
canvasId: 'shareCanvas',
x: 0,
y: 0,
width: 200,
height: 200,
success(res) {
let arr = tthis.convert4to2(res.data);
let data = tthis.convert8to1(arr); //宽除8 //高本值
const cmds = [].concat([27, 97, 1], [29, 118, 48, 0, 25, 0, 200, 0], data, [27, 74, 3], [27, 64]);
const buffer1 = toArrayBuffer1(Buffer1.from(cmds, 'gb2312'));
let arrPrint = [];
arrPrint.push(util.sendDirective([0x1B, 0x40]));
arrPrint.push(util.sendDirective([0x1B, 0x61, 0x01])); //居中
for (let i = 0; i < buffer1.byteLength; i = i + 20) { //限制输入数据
arrPrint.push(buffer1.slice(i, i + 20));
}
strArray = [];
strArray[0] = "采购单\n";
strArray[1] = "大白菜 5千克\n";
strArray[2] = "土豆 5千克\n";
strArray[3] = "牛肉 5千克\n";
strArray[4] = "花菜 5千克\n";
strArray[5] = "西红柿 5千克\n";
strArray[6] = "青辣椒 5千克\n";
arrPrint.push(util.hexStringToBuff(`扫码识别单号\n`));
strArray.forEach((el, index) => {
strArray[index] = util.hexStringToBuff(el);
})
console.log(arrPrint);
strArray = strArray.concat(arrPrint)
callback && callback(strArray)
},
fail(res) {
console.log(res);
}
})
}
});
},
有没有有更好的方案或者改进方法,十分感谢!!!