This commit is contained in:
aihe 2024-06-23 01:40:04 +08:00
parent f9755a4222
commit 2e18b1bcd9
3 changed files with 49 additions and 59 deletions

View File

@ -67,8 +67,8 @@ export const print = (mac_address, data, isBytes) => {
if (bluetoothSocket.isConnected()) { if (bluetoothSocket.isConnected()) {
var outputStream = bluetoothSocket.getOutputStream() var outputStream = bluetoothSocket.getOutputStream()
plus.android.importClass(outputStream) plus.android.importClass(outputStream)
// outputStream.write([0x1b, 0x40]) //打印复位 outputStream.write([0x1b, 0x40]) //打印复位
// outputStream.flush() outputStream.flush()
let bytes let bytes
console.log("plus.android.invoke data getBytes======= before") console.log("plus.android.invoke data getBytes======= before")
if(!isBytes) { if(!isBytes) {
@ -78,7 +78,6 @@ export const print = (mac_address, data, isBytes) => {
} }
console.log("plus.android.invoke data getBytes======= end", bytes) console.log("plus.android.invoke data getBytes======= end", bytes)
outputStream.write(bytes) outputStream.write(bytes)
// outputStream.flush()
// TODO 打印完 断掉连接 // TODO 打印完 断掉连接
// device = null //这里关键 // device = null //这里关键

View File

@ -14,6 +14,32 @@ tanexPrinter.senBlData(tanexPrinter.getPrintCmd())
*/ */
import encode from "./encoding" 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 = { var tanexPrinter = {
createInstance: function() { createInstance: function() {
var tanexPrinter = {}; var tanexPrinter = {};
@ -57,66 +83,34 @@ var tanexPrinter = {
/* /*
添加图片,res为画布参数:{height:height,width:width,data:data} 添加图片,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 tanexPrinter.addBitmapByCG = function(x, y, res) {
var width = parseInt((res.width + 7) / 8 * 8 / 8) let btRes = tools.getBitmapData(x, y, res)
var height = res.height; data = `CG ${btRes.width} ${btRes.height} ${x} ${y} `
// console.log(width + "--" + height)
data = `CG ${width} ${height} ${x} ${y} `
tanexPrinter.addCommand(data) tanexPrinter.addCommand(data)
// console.log(command) // console.log(command)
var r = [] btRes.bitData.map(v=> {
var bits = new Uint8Array(height * width); command.push(v);
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);
}
tanexPrinter.addCommand('\r\n') tanexPrinter.addCommand('\r\n')
console.log('===>setBitmapCmd:', r)
} }
tanexPrinter.addBitmap2 = function(x, y, res) { //添加图片,res为画布参数 // 打印原图
var w = res.width tanexPrinter.addBitmapByEG = function(x, y, res) { //添加图片,res为画布参数
var width = parseInt((res.width + 7) / 8 * 8 / 8) let btRes = tools.getBitmapData(x, y, res)
var height = res.height; data = `EG ${btRes.width} ${btRes.height} ${x} ${y} `
// console.log(width + "--" + height) btRes.bitData.map(v=> {
data = `EG ${width} ${height} ${x} ${y} ` // 转成16位的ASCII字符串
// console.log(command) let asc16 = (v).toString(16)
var r = [] v < 16 && (asc16 = `0${asc16}`)
var bits = new Uint8Array(height * width); asc16 = asc16.toUpperCase()
for (y = 0; y < height; y++) { data += `${asc16}`
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.addCommand(data) tanexPrinter.addCommand(data)
console.log("addBitmap2 EG data =====================", data)
tanexPrinter.addCommand('\r\n') tanexPrinter.addCommand('\r\n')
} }

View File

@ -66,10 +66,7 @@ export default {
let prtInstance = tanexPrinterUniSdk.createInstance() let prtInstance = tanexPrinterUniSdk.createInstance()
prtInstance.init(that.canvasHeight) prtInstance.init(that.canvasHeight)
prtInstance.diyCode(`PAGE-WIDTH 600\r\n`) prtInstance.diyCode(`PAGE-WIDTH 600\r\n`)
// prtInstance.diyCode(`EG 2 16 90 45 F0F0F0F0F0F0F0F00F0F0F0F0F0F0F0F`) prtInstance.addBitmapByEG(0, 0, that.imgData)
// prtInstance.diyCode(`F0F0F0F0F0F0F0F00F0F0F0F0F0F0F0F\r\n`)
// prtInstance.addBitmap2(0, 0, that.imgData)
prtInstance.addBitmap2(0, 0, that.imgData)
prtInstance.setPagePrint() prtInstance.setPagePrint()
let commond = prtInstance.getPrintCmd() let commond = prtInstance.getPrintCmd()
console.log("打印命令组装完成,开始打印=====", commond) console.log("打印命令组装完成,开始打印=====", commond)