收藏
回答

小程序连接蓝牙打印二维码很慢?

现在的需求是链接蓝牙打印机打印二维码,我使用的方案是使用canvas生成二维码,在获取canvasImageData转换成buffer然后分组进行打印,但是这种方式非常慢,有什么更好的方式进行打印么?

以下为生成canvas二维码转换buffer部分代码:

  printImgT(text, callback) { //二维码
    let tthis = this;
    const ctx = wx.createCanvasContext('shareCanvas');
    ctx.clearRect(00200200);
    drawQrcode({
      canvasId'shareCanvas',
      textString(text),
      width200,
      height200,
      callback(e) {
        wx.canvasGetImageData({
          canvasId'shareCanvas',
          x0,
          y0,
          width200,
          height200,
          success(res) {
            let arr = tthis.convert4to2(res.data);
            let data = tthis.convert8to1(arr); //宽除8 //高本值
            const cmds = [].concat([27971], [291184802502000], data, [27743], [2764]);
            const buffer1 = toArrayBuffer1(Buffer1.from(cmds, 'gb2312'));
            let arrPrint = [];
            arrPrint.push(util.sendDirective([0x1B0x40]));
            arrPrint.push(util.sendDirective([0x1B0x610x01])); //居中
            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);
          }
        })
      }
    });
  },


有没有有更好的方案或者改进方法,十分感谢!!!

回答关注问题邀请回答
收藏
登录 后发表内容