emis-app/components/tpx-bluetooth-print/print/tanexPrinterUniSdk.js

116 lines
4.4 KiB
JavaScript
Raw Normal View History

2024-06-05 23:51:19 +00:00
/**
tanexPrinter 打印指令
var tanexPrinterUniSdk = require('xxxx/tanexPrinterUniSdk.js')
var tanexPrinter = tanexPrinterUniSdk.tanexPrinter.createInstance()
// 设置热敏纸高度 130
tanexPrinter.init(130)
// 渲染位图,添加图片到打印
tanexPrinter.addBitmap(0, 0, res)
// 设置打印指令
tanexPrinter.setPagePrint()
// 数据发送到蓝牙设备
tanexPrinter.senBlData(tanexPrinter.getPrintCmd())
*/
2024-06-06 17:21:48 +00:00
import encode from "./encoding"
2024-06-05 23:51:19 +00:00
var tanexPrinter = {
createInstance: function() {
var tanexPrinter = {};
var data = "";
var command = []
tanexPrinter.name = "tanex-CPCL打印";
/*
[名称] 开始命令 [格式] ! {offset} 200 200 {height} {qty} [说明] {offset}:整个标签的水平偏移量 200:水平分辨率,203 点/英寸(8 点/mm) 200:垂直分辨率,203 点/英寸(8 点/mm) {height}:标签最大高度,点为单位 {qty}:打印标签的数量,最大1024 张 [实例] Input: Output: ! 0 200 200 210 1 TEXT 4 0 30 40 Hello World Hello World FORM PRINT
*/
tanexPrinter.init = function(maxHeight) {
data = "! 0 200 200 " + maxHeight + " 1\r\n"
tanexPrinter.addCommand(data)
};
/**
* 获取面单数据,用于图片预览
* @param {Object} billCode 运单号,多个 逗号隔开
* @param {Object} tplCode 模版代码 多个 逗号隔开
*/
tanexPrinter.getPrintData = function(billCode,tplCode) {
let rstList=[];
return rstList;
};
//指令组装成数组模式
tanexPrinter.addCommand = function(content) {
// console.log(content, 'contentcontentcontent')
var code = new encode.TextEncoder(
'gb18030', {
NONSTANDARD_allowLegacyEncoding: true
}).encode(content)
for (var i = 0; i < code.length; ++i) {
command.push(code[i])
}
}
/*
添加图片,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 的一半。
*/
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.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);
}
// console.log('===>setBitmapCmd:',JSON.stringify(r))
}
/*
[名称] 设置打印命令 [格式] {command} [说明] {command}:PRINT 在CPCL 指令模式下,打印命令终止和打印文件,这是最后的一条命令;执行打印命 令后,打印机退出控制段,在打印命令后,必须跟一个回车。 如果打印中出错,等待错误恢复后重新打印。 [实例] Input: ! 0 200 200 210 1 …………… PRINT
*/
tanexPrinter.setPagePrint = function() {
data = "PRINT\r\n"
tanexPrinter.addCommand(data)
};
tanexPrinter.diyCode = function(content) { //自定义指令
tanexPrinter.addCommand(content)
};
tanexPrinter.pushCode = function(content) { //推送指令
content.forEach((el)=>{
command.push(el)
})
// command = command.concat(content)
};
//获取打印数据
tanexPrinter.getPrintCmd = function() {
return command;
};
return tanexPrinter;
}
};
2024-06-06 17:21:48 +00:00
export default tanexPrinter;