151 lines
5.3 KiB
JavaScript
151 lines
5.3 KiB
JavaScript
/**
|
||
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())
|
||
*/
|
||
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 = {};
|
||
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 的一半。
|
||
|
||
!!注意!!! CG模式: 图片打印不清晰,因为图片被压缩了
|
||
*/
|
||
// 打印压缩图
|
||
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)
|
||
btRes.bitData.map(v=> {
|
||
command.push(v);
|
||
})
|
||
tanexPrinter.addCommand('\r\n')
|
||
}
|
||
|
||
// 打印原图
|
||
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)
|
||
tanexPrinter.addCommand('\r\n')
|
||
}
|
||
|
||
tanexPrinter.addText = function(text) {
|
||
data = `TEXT 24 0 10 10 ${text}\r\n`
|
||
tanexPrinter.addCommand(data)
|
||
};
|
||
|
||
/*
|
||
[名称] 设置打印命令
|
||
[格式] {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;
|
||
}
|
||
};
|
||
|
||
export default tanexPrinter;
|