diff --git a/components/tpx-bluetooth-print/print/printerjobs.js b/components/tpx-bluetooth-print/print/printerjobs.js index 78fe091..2eebb44 100644 --- a/components/tpx-bluetooth-print/print/printerjobs.js +++ b/components/tpx-bluetooth-print/print/printerjobs.js @@ -44,11 +44,10 @@ printerJobs.prototype.printArray = function(array) { return this; }; - -printerJobs.prototype.printQrcode = function(content) { +printerJobs.prototype.printQrcode = function(content, width) { if (content) { - const cmds = [].concat([27, 97, 1], [29, 118, 48, 0, 30, 0, 240, 0], content, [27, 74, 3], [27, 64]); + const cmds = [].concat(...printerUtil.makeImageCode(content, width)); this._enqueue(cmds); this._enqueue(commands.LF); } diff --git a/components/tpx-bluetooth-print/print/printerutil.js b/components/tpx-bluetooth-print/print/printerutil.js index fcce98e..afa6dc3 100644 --- a/components/tpx-bluetooth-print/print/printerutil.js +++ b/components/tpx-bluetooth-print/print/printerutil.js @@ -82,10 +82,26 @@ function ab2hex(buffer) { return hexArr.join(',') } +// 打印图片,需要根据图片宽度算出rate, rate = 图片宽度/imgRateSize +const imgRateSize = 8 + +// 默认240宽度 注意!! 图片宽度必须是8的倍数 +const makeImageCode = (data, width = 240)=> { + //我的图片宽度是240,那么拼接的指令就是[29, 118, 48, 0, 30, 0, 240, 0] + //我的图片宽度是160,那么拼接的指令就是[29, 118, 48, 0, 20, 0, 160, 0] + //补充一点,打印非二维码的图片,宽度一定要是24的倍数,不然打印也会出现乱码 + let rate = width / imgRateSize + + return [ + [27, 97, 1], [29, 118, 48, 0, 1*rate, 0, 8*rate, 0], data, [27, 74, 3], [27, 64] + ] +} export { inline, fillLine, fillAround, ab2hex, + imgRateSize, + makeImageCode, }; \ No newline at end of file diff --git a/components/tpx-bluetooth-print/print/util.js b/components/tpx-bluetooth-print/print/util.js index 8b09054..3aa8118 100644 --- a/components/tpx-bluetooth-print/print/util.js +++ b/components/tpx-bluetooth-print/print/util.js @@ -1,3 +1,5 @@ +import { makeImageCode } from "./printerutil.js" + const formatTime = date => { const year = date.getFullYear() const month = date.getMonth() + 1 @@ -46,10 +48,10 @@ function convert8to1(arr) { //我的图片宽度是240,那么拼接的指令就是[29, 118, 48, 0, 30, 0, 240, 0] //我的图片宽度是160,那么拼接的指令就是[29, 118, 48, 0, 20, 0, 160, 0] //补充一点,打印非二维码的图片,宽度一定要是24的倍数,不然打印也会出现乱码 -function toArrayBuffer(res) { +function toArrayBuffer(res, width) { let arr = convert4to1(res.data); let data = convert8to1(arr); - let cmds = [].concat([27, 97, 1], [29, 118, 48, 0, 30, 0, 240, 0], data, [27, 74, 3], [27, 64]); + let cmds = [].concat(...makeImageCode(data, width)); return new Uint8Array(cmds).buffer; } diff --git a/components/tpx-bluetooth-print/tpx-bluetooth-print.vue b/components/tpx-bluetooth-print/tpx-bluetooth-print.vue index 224325a..07700b5 100644 --- a/components/tpx-bluetooth-print/tpx-bluetooth-print.vue +++ b/components/tpx-bluetooth-print/tpx-bluetooth-print.vue @@ -20,8 +20,8 @@ export default { data () { return { canvasId: 'shareCanvas', - canvas_width: 240, - canvas_height: 240, + canvas_width: 480, + canvas_height: 480, blueToothData: { deviceId: '', serviceId: '', @@ -51,7 +51,7 @@ export default { // .setBold(true) // .print(`叫那位非常漂亮的小姐姐送来,其他人送来拒收`) // .print(printerUtil.fillLine()) - .printQrcode(pic_res) + .printQrcode(pic_res, this.canvas_width) // .setSize(1, 1) // .setBold(false) // .print(printerUtil.fillLine('*')) @@ -157,7 +157,7 @@ export default { // 必须加延迟返回,防止高频率调用 setTimeout(()=> { resolve() - }, 5) + }, 10) }, fail:(res)=> { console.log('writeBLECharacteristicValue fail', res.errMsg)