This commit is contained in:
linfso 2024-06-06 07:51:19 +08:00
parent c0a18a0411
commit 91d15405f8
6 changed files with 3533 additions and 2 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because it is too large Load Diff

View File

@ -6,6 +6,62 @@
//蓝牙打印机一次接收的二级制数据有限制,不同的系统不同的蓝牙设备限制可能不同,微信建议一次20个字节,需做递归分包发送
//发送完要打印的内容后,一定要发送一个打印的指令才能顺利打印 (有些指令就不需要)
export function senBlData(uint8Array) {
let that = this
var uint8Buf = Array.from(uint8Array);
function split_array(datas, size) {
var result = {};
var j = 0
for (var i = 0; i < datas.length; i += size) {
result[j] = datas.slice(i, i + size)
j++
}
return result
}
var sendloop = split_array(uint8Buf, 20);
function realWriteData(sendloop, i) {
var data = sendloop[i]
if (typeof(data) == "undefined") {
uni.showLoading({
title: '数据发送完毕,准备打印',
icon: 'loading',
})
setTimeout(function() {
uni.hideLoading()
}, 1000);
return
}
// console.log("第【" + i + "】次写数据"+data)
var buffer = new ArrayBuffer(data.length)
var dataView = new DataView(buffer)
for (var j = 0; j < data.length; j++) {
dataView.setUint8(j, data[j]);
}
uni.writeBLECharacteristicValue({
deviceId: that.deviceId,
serviceId: that.writeServiceId,
characteristicId: that.writeCharaterId,
value: buffer,
success(res) {
realWriteData(sendloop, i + 1);
},
fail() {
uni.showLoading({
title: '发送数据失败',
icon: 'loading',
})
setTimeout(function() {
uni.hideLoading()
}, 1000);
}
})
}
var i = 0;
realWriteData(sendloop, i);
}
// 发送数据(递归分包发送)
/**
@ -21,7 +77,7 @@
lasterSuccess,
}
*/
export function sendDataToDevice(options) {
let byteLength = options.value.byteLength;
// 这里默认一次20个字发送

View File

@ -0,0 +1,114 @@
/**
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())
*/
var encode = require("./encoding.js");
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;
}
};
module.exports.tanexPrinter = tanexPrinter;

View File

@ -25,6 +25,7 @@
<script>
import { showLoading, showToast, hideLoading, canvasGetImageData, getImageInfo, promiseTimeout } from "@/common/untool"
import * as canvasUtil from "@/common/canvasUtil"
import tanexPrinterUniSdk from './print/tanexPrinterUniSdk'
import * as printJob from './print/printJob'
export default {

View File

@ -15,7 +15,8 @@
"license": "MIT",
"dependencies": {
"dayjs": "^1.11.10",
"file-saver": "2.0.5"
"file-saver": "2.0.5",
"pdf-lib": "^1.17.1"
},
"dcloudext": {
"category": [