From 2e18b1bcd96b96b2a45cf303d6a59c4f0383fb20 Mon Sep 17 00:00:00 2001 From: aihe <961836564@qq.com> Date: Sun, 23 Jun 2024 01:40:04 +0800 Subject: [PATCH] uu --- components/tpx-bluetooth-print/print/print.js | 5 +- .../print/tanexPrinterUniSdk.js | 98 +++++++++---------- .../render-template-native.vue | 5 +- 3 files changed, 49 insertions(+), 59 deletions(-) diff --git a/components/tpx-bluetooth-print/print/print.js b/components/tpx-bluetooth-print/print/print.js index 1a90f35..a1a5b7d 100644 --- a/components/tpx-bluetooth-print/print/print.js +++ b/components/tpx-bluetooth-print/print/print.js @@ -67,8 +67,8 @@ export const print = (mac_address, data, isBytes) => { if (bluetoothSocket.isConnected()) { var outputStream = bluetoothSocket.getOutputStream() plus.android.importClass(outputStream) - // outputStream.write([0x1b, 0x40]) //打印复位 - // outputStream.flush() + outputStream.write([0x1b, 0x40]) //打印复位 + outputStream.flush() let bytes console.log("plus.android.invoke data getBytes======= before") if(!isBytes) { @@ -78,7 +78,6 @@ export const print = (mac_address, data, isBytes) => { } console.log("plus.android.invoke data getBytes======= end", bytes) outputStream.write(bytes) - // outputStream.flush() // TODO 打印完 断掉连接 // device = null //这里关键 diff --git a/components/tpx-bluetooth-print/print/tanexPrinterUniSdk.js b/components/tpx-bluetooth-print/print/tanexPrinterUniSdk.js index 57cb987..57814d1 100644 --- a/components/tpx-bluetooth-print/print/tanexPrinterUniSdk.js +++ b/components/tpx-bluetooth-print/print/tanexPrinterUniSdk.js @@ -14,6 +14,32 @@ tanexPrinter.senBlData(tanexPrinter.getPrintCmd()) */ import encode from "./encoding" +const tools = { + getBitmapData: (x, y, res) => { + var w = res.width + var width = parseInt((res.width + 7) / 8 * 8 / 8) + var height = res.height; + var bitData = [] + var bits = new Uint8Array(height * width); + for (y = 0; y < height; y++) { + for (x = 0; x < w; x++) { + var color = res.data[(y * w + x) * 4 + 1]; + if (color > 128) { + bits[parseInt(y * width + x / 8)] |= (0x80 >> (x % 8)); + } + } + } + for (var i = 0; i < bits.length; i++) { + bitData.push((~bits[i]) & 0xFF); + } + return { + width, + height, + bitData + } + } +} + var tanexPrinter = { createInstance: function() { var tanexPrinter = {}; @@ -57,66 +83,34 @@ var tanexPrinter = { /* 添加图片,res为画布参数:{height:height,width:width,data:data} - [名称] 图形命令 [格式] {command} {width} {height} {x} {y} {data} [说明] {command}:选择下面指令 EXPANDED-GRAPHICS (or EG): 打印扩展图形 COMPRESSED-GRAPHICS (or CG): 打印压缩图形 { width }:图像宽度,以字节为单位 { height }:图像高度,以点为单位 {x}: 水平起始位置 {y}: 垂直起始位置 {data}: 图形数据 二值位图可以通过图形命令进行打印。使用扩展图形指令(EXPANDED-GRAPHICS), 图形数据为ASCII 码,如实例所示。如果想要图形数据减半则需要使用压缩图形指令 (COMPRESSED-GRAPHICS),图形数据为直接的十六进制数据。当使用CG 指令时, 每一个8 位的字节表示8 点的图形数据,当使用EG 指令时,每两个字节(16 位)表示 8 点的图形数据。EG 的效率为CG 的一半。 + [名称] 图形命令 [格式] {command} {width} {height} {x} {y} {data} [说明] {command}:选择下面指令 EXPANDED-GRAPHICS (or EG): 打印扩展图形 COMPRESSED-GRAPHICS (or CG): 打印压缩图形 { width }:图像宽度,以字节为单位 { height }:图像高度,以点为单位 {x}: 水平起始位置 {y}: 垂直起始位置 {data}: 图形数据 二值位图可以通过图形命令进行打印。使用扩展图形指令(EXPANDED-GRAPHICS), 图形数据为ASCII 码,如实例所示。如果想要图形数据减半则需要使用压缩图形指令 (COMPRESSED-GRAPHICS),图形数据为直接的十六进制数据。当使用CG 指令时, 每一个8 位的字节表示8 点的图形数据,当使用EG 指令时,每两个字节(16 位)表示 8 点的图形数据。EG 的效率为CG 的一半。 + + !!注意!!! CG模式: 图片打印不清晰,因为图片被压缩了 */ - tanexPrinter.addBitmap = function(x, y, res) { - var w = res.width - var width = parseInt((res.width + 7) / 8 * 8 / 8) - var height = res.height; - // console.log(width + "--" + height) - data = `CG ${width} ${height} ${x} ${y} ` + // 打印压缩图 + tanexPrinter.addBitmapByCG = function(x, y, res) { + let btRes = tools.getBitmapData(x, y, res) + data = `CG ${btRes.width} ${btRes.height} ${x} ${y} ` tanexPrinter.addCommand(data) // console.log(command) - var r = [] - var bits = new Uint8Array(height * width); - for (y = 0; y < height; y++) { - for (x = 0; x < w; x++) { - var color = res.data[(y * w + x) * 4 + 1]; - if (color > 128) { - bits[parseInt(y * width + x / 8)] |= (0x80 >> (x % 8)); - } - } - } - for (var i = 0; i < bits.length; i++) { - command.push((~bits[i]) & 0xFF); - r.push((~bits[i]) & 0xFF); - } + btRes.bitData.map(v=> { + command.push(v); + }) tanexPrinter.addCommand('\r\n') - console.log('===>setBitmapCmd:', r) } - tanexPrinter.addBitmap2 = function(x, y, res) { //添加图片,res为画布参数 - var w = res.width - var width = parseInt((res.width + 7) / 8 * 8 / 8) - var height = res.height; - // console.log(width + "--" + height) - data = `EG ${width} ${height} ${x} ${y} ` - // console.log(command) - var r = [] - var bits = new Uint8Array(height * width); - for (y = 0; y < height; y++) { - for (x = 0; x < w; x++) { - var color = res.data[(y * w + x) * 4 + 1]; - if (color > 128) { - bits[parseInt(y * width + x / 8)] |= (0x80 >> (x % 8)); - } - } - } - for (var i = 0; i < bits.length; i++) { - r.push((~bits[i]) & 0xFF); - } - let egIData = `` - r.map(v=> { - // 转成16位的ASCII - let a16 = (v).toString(16) - if(v < 16) { - a16 = `0${a16}` - } - a16 = a16.toUpperCase() - data += `${a16}` + // 打印原图 + tanexPrinter.addBitmapByEG = function(x, y, res) { //添加图片,res为画布参数 + let btRes = tools.getBitmapData(x, y, res) + data = `EG ${btRes.width} ${btRes.height} ${x} ${y} ` + btRes.bitData.map(v=> { + // 转成16位的ASCII字符串 + let asc16 = (v).toString(16) + v < 16 && (asc16 = `0${asc16}`) + asc16 = asc16.toUpperCase() + data += `${asc16}` }) tanexPrinter.addCommand(data) - console.log("addBitmap2 EG data =====================", data) tanexPrinter.addCommand('\r\n') } diff --git a/components/tpx-bluetooth-print/render-template-native.vue b/components/tpx-bluetooth-print/render-template-native.vue index 0cf2f3e..574df42 100644 --- a/components/tpx-bluetooth-print/render-template-native.vue +++ b/components/tpx-bluetooth-print/render-template-native.vue @@ -66,10 +66,7 @@ export default { let prtInstance = tanexPrinterUniSdk.createInstance() prtInstance.init(that.canvasHeight) prtInstance.diyCode(`PAGE-WIDTH 600\r\n`) - // prtInstance.diyCode(`EG 2 16 90 45 F0F0F0F0F0F0F0F00F0F0F0F0F0F0F0F`) - // prtInstance.diyCode(`F0F0F0F0F0F0F0F00F0F0F0F0F0F0F0F\r\n`) - // prtInstance.addBitmap2(0, 0, that.imgData) - prtInstance.addBitmap2(0, 0, that.imgData) + prtInstance.addBitmapByEG(0, 0, that.imgData) prtInstance.setPagePrint() let commond = prtInstance.getPrintCmd() console.log("打印命令组装完成,开始打印=====", commond)