taro-bluetooth-print 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +201 -0
- package/dist/bluetooth/adapter.d.ts +31 -0
- package/dist/bluetooth/adapter.js +47 -0
- package/dist/bluetooth/adapter.js.map +1 -0
- package/dist/bluetooth/h5.d.ts +66 -0
- package/dist/bluetooth/h5.js +302 -0
- package/dist/bluetooth/h5.js.map +1 -0
- package/dist/bluetooth/harmony.d.ts +82 -0
- package/dist/bluetooth/harmony.js +342 -0
- package/dist/bluetooth/harmony.js.map +1 -0
- package/dist/bluetooth/index.d.ts +45 -0
- package/dist/bluetooth/index.js +175 -0
- package/dist/bluetooth/index.js.map +1 -0
- package/dist/bluetooth/rn.d.ts +35 -0
- package/dist/bluetooth/rn.js +368 -0
- package/dist/bluetooth/rn.js.map +1 -0
- package/dist/bluetooth/weapp.d.ts +16 -0
- package/dist/bluetooth/weapp.js +177 -0
- package/dist/bluetooth/weapp.js.map +1 -0
- package/dist/index.d.ts +16 -0
- package/dist/index.js +35 -0
- package/dist/index.js.map +1 -0
- package/dist/printer/commands.d.ts +28 -0
- package/dist/printer/commands.js +90 -0
- package/dist/printer/commands.js.map +1 -0
- package/dist/printer/image.d.ts +59 -0
- package/dist/printer/image.js +258 -0
- package/dist/printer/image.js.map +1 -0
- package/dist/printer/index.d.ts +87 -0
- package/dist/printer/index.js +362 -0
- package/dist/printer/index.js.map +1 -0
- package/dist/printer/template.d.ts +95 -0
- package/dist/printer/template.js +262 -0
- package/dist/printer/template.js.map +1 -0
- package/dist/utils/buffer.d.ts +35 -0
- package/dist/utils/buffer.js +96 -0
- package/dist/utils/buffer.js.map +1 -0
- package/dist/utils/encoding.d.ts +26 -0
- package/dist/utils/encoding.js +149 -0
- package/dist/utils/encoding.js.map +1 -0
- package/dist/utils/logger.d.ts +18 -0
- package/dist/utils/logger.js +91 -0
- package/dist/utils/logger.js.map +1 -0
- package/package.json +57 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Commands = exports.PrinterManager = exports.BluetoothManager = void 0;
|
|
4
|
+
const bluetooth_1 = require("./bluetooth");
|
|
5
|
+
Object.defineProperty(exports, "BluetoothManager", { enumerable: true, get: function () { return bluetooth_1.BluetoothManager; } });
|
|
6
|
+
const printer_1 = require("./printer");
|
|
7
|
+
Object.defineProperty(exports, "PrinterManager", { enumerable: true, get: function () { return printer_1.PrinterManager; } });
|
|
8
|
+
const commands_1 = require("./printer/commands");
|
|
9
|
+
Object.defineProperty(exports, "Commands", { enumerable: true, get: function () { return commands_1.Commands; } });
|
|
10
|
+
class TaroBluePrint {
|
|
11
|
+
constructor() {
|
|
12
|
+
this.bluetooth = new bluetooth_1.BluetoothManager();
|
|
13
|
+
this.printer = new printer_1.PrinterManager(this.bluetooth);
|
|
14
|
+
this.commands = commands_1.Commands;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* 快速打印文本
|
|
18
|
+
* @param text 文本内容
|
|
19
|
+
* @param deviceId 蓝牙设备ID
|
|
20
|
+
*/
|
|
21
|
+
async quickPrint(text, deviceId) {
|
|
22
|
+
try {
|
|
23
|
+
await this.bluetooth.connect(deviceId);
|
|
24
|
+
const result = await this.printer.printText(text);
|
|
25
|
+
await this.bluetooth.disconnect();
|
|
26
|
+
return result;
|
|
27
|
+
}
|
|
28
|
+
catch (error) {
|
|
29
|
+
console.error('打印失败:', error);
|
|
30
|
+
return false;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
exports.default = TaroBluePrint;
|
|
35
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,2CAA+C;AAK7C,iGALO,4BAAgB,OAKP;AAJlB,uCAA2C;AAKzC,+FALO,wBAAc,OAKP;AAJhB,iDAA8C;AAK5C,yFALO,mBAAQ,OAKP;AAGV,MAAqB,aAAa;IAKhC;QACE,IAAI,CAAC,SAAS,GAAG,IAAI,4BAAgB,EAAE,CAAC;QACxC,IAAI,CAAC,OAAO,GAAG,IAAI,wBAAc,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;QAClD,IAAI,CAAC,QAAQ,GAAG,mBAAQ,CAAC;IAC3B,CAAC;IAED;;;;OAIG;IACH,KAAK,CAAC,UAAU,CAAC,IAAY,EAAE,QAAgB;QAC7C,IAAI;YACF,MAAM,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC;YACvC,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,OAAO,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC;YAClD,MAAM,IAAI,CAAC,SAAS,CAAC,UAAU,EAAE,CAAC;YAClC,OAAO,MAAM,CAAC;SACf;QAAC,OAAO,KAAK,EAAE;YACd,OAAO,CAAC,KAAK,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC;YAC9B,OAAO,KAAK,CAAC;SACd;IACH,CAAC;CACF;AA3BD,gCA2BC"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ESC/POS命令集
|
|
3
|
+
* 参考: https://reference.epson-biz.com/modules/ref_escpos/index.php
|
|
4
|
+
*/
|
|
5
|
+
export declare class Commands {
|
|
6
|
+
static INIT: Uint8Array;
|
|
7
|
+
static LF: Uint8Array;
|
|
8
|
+
static CRLF: Uint8Array;
|
|
9
|
+
static CUT: Uint8Array;
|
|
10
|
+
static BEEP: Uint8Array;
|
|
11
|
+
static ALIGN_LEFT: Uint8Array;
|
|
12
|
+
static ALIGN_CENTER: Uint8Array;
|
|
13
|
+
static ALIGN_RIGHT: Uint8Array;
|
|
14
|
+
static TEXT_NORMAL: Uint8Array;
|
|
15
|
+
static TEXT_MEDIUM: Uint8Array;
|
|
16
|
+
static TEXT_LARGE: Uint8Array;
|
|
17
|
+
static BOLD_ON: Uint8Array;
|
|
18
|
+
static BOLD_OFF: Uint8Array;
|
|
19
|
+
static UNDERLINE_ON: Uint8Array;
|
|
20
|
+
static UNDERLINE_OFF: Uint8Array;
|
|
21
|
+
static INVERT_ON: Uint8Array;
|
|
22
|
+
static INVERT_OFF: Uint8Array;
|
|
23
|
+
static setCharacterSpacing(spacing: number): Uint8Array;
|
|
24
|
+
static setLineSpacing(spacing: number): Uint8Array;
|
|
25
|
+
static setBarcodeHeight(height: number): Uint8Array;
|
|
26
|
+
static printBarcode(content: string, type?: number): Uint8Array;
|
|
27
|
+
static printQRCode(content: string, size?: number): Uint8Array[];
|
|
28
|
+
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Commands = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* ESC/POS命令集
|
|
6
|
+
* 参考: https://reference.epson-biz.com/modules/ref_escpos/index.php
|
|
7
|
+
*/
|
|
8
|
+
class Commands {
|
|
9
|
+
// 字符间距
|
|
10
|
+
static setCharacterSpacing(spacing) {
|
|
11
|
+
return new Uint8Array([0x1B, 0x20, spacing]);
|
|
12
|
+
}
|
|
13
|
+
// 行间距
|
|
14
|
+
static setLineSpacing(spacing) {
|
|
15
|
+
return new Uint8Array([0x1B, 0x33, spacing]);
|
|
16
|
+
}
|
|
17
|
+
// 设置条码高度
|
|
18
|
+
static setBarcodeHeight(height) {
|
|
19
|
+
return new Uint8Array([0x1D, 0x68, height]);
|
|
20
|
+
}
|
|
21
|
+
// 打印条码
|
|
22
|
+
static printBarcode(content, type = 73) {
|
|
23
|
+
const encoder = new TextEncoder();
|
|
24
|
+
const contentBytes = encoder.encode(content);
|
|
25
|
+
const result = new Uint8Array(4 + contentBytes.length);
|
|
26
|
+
result[0] = 0x1D; // GS
|
|
27
|
+
result[1] = 0x6B; // k
|
|
28
|
+
result[2] = type; // 条码类型 (默认73为CODE128)
|
|
29
|
+
result[3] = contentBytes.length; // 内容长度
|
|
30
|
+
result.set(contentBytes, 4);
|
|
31
|
+
return result;
|
|
32
|
+
}
|
|
33
|
+
// 打印二维码
|
|
34
|
+
static printQRCode(content, size = 6) {
|
|
35
|
+
const encoder = new TextEncoder();
|
|
36
|
+
const contentBytes = encoder.encode(content);
|
|
37
|
+
const contentLength = contentBytes.length;
|
|
38
|
+
const pL = contentLength % 256;
|
|
39
|
+
const pH = Math.floor(contentLength / 256);
|
|
40
|
+
// QR码模型
|
|
41
|
+
const modelCommand = new Uint8Array([0x1D, 0x28, 0x6B, 0x04, 0x00, 0x31, 0x41, 0x32, 0x00]);
|
|
42
|
+
// QR码大小
|
|
43
|
+
const sizeCommand = new Uint8Array([0x1D, 0x28, 0x6B, 0x03, 0x00, 0x31, 0x43, size]);
|
|
44
|
+
// QR码纠错
|
|
45
|
+
const errorCommand = new Uint8Array([0x1D, 0x28, 0x6B, 0x03, 0x00, 0x31, 0x45, 0x31]);
|
|
46
|
+
// QR码数据
|
|
47
|
+
const dataCommand = new Uint8Array(contentLength + 8);
|
|
48
|
+
dataCommand[0] = 0x1D;
|
|
49
|
+
dataCommand[1] = 0x28;
|
|
50
|
+
dataCommand[2] = 0x6B;
|
|
51
|
+
dataCommand[3] = pL;
|
|
52
|
+
dataCommand[4] = pH;
|
|
53
|
+
dataCommand[5] = 0x31;
|
|
54
|
+
dataCommand[6] = 0x50;
|
|
55
|
+
dataCommand[7] = 0x30;
|
|
56
|
+
dataCommand.set(contentBytes, 8);
|
|
57
|
+
// 打印QR码
|
|
58
|
+
const printCommand = new Uint8Array([0x1D, 0x28, 0x6B, 0x03, 0x00, 0x31, 0x51, 0x30]);
|
|
59
|
+
return [modelCommand, sizeCommand, errorCommand, dataCommand, printCommand];
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
exports.Commands = Commands;
|
|
63
|
+
// 初始化打印机
|
|
64
|
+
Commands.INIT = new Uint8Array([0x1B, 0x40]);
|
|
65
|
+
// 换行
|
|
66
|
+
Commands.LF = new Uint8Array([0x0A]);
|
|
67
|
+
// 回车+换行
|
|
68
|
+
Commands.CRLF = new Uint8Array([0x0D, 0x0A]);
|
|
69
|
+
// 切纸
|
|
70
|
+
Commands.CUT = new Uint8Array([0x1D, 0x56, 0x41, 0x00]);
|
|
71
|
+
// 蜂鸣
|
|
72
|
+
Commands.BEEP = new Uint8Array([0x1B, 0x42, 0x05, 0x05]);
|
|
73
|
+
// 设置对齐方式
|
|
74
|
+
Commands.ALIGN_LEFT = new Uint8Array([0x1B, 0x61, 0x00]);
|
|
75
|
+
Commands.ALIGN_CENTER = new Uint8Array([0x1B, 0x61, 0x01]);
|
|
76
|
+
Commands.ALIGN_RIGHT = new Uint8Array([0x1B, 0x61, 0x02]);
|
|
77
|
+
// 字体大小
|
|
78
|
+
Commands.TEXT_NORMAL = new Uint8Array([0x1B, 0x21, 0x00]); // 正常大小
|
|
79
|
+
Commands.TEXT_MEDIUM = new Uint8Array([0x1B, 0x21, 0x08]); // 2倍高
|
|
80
|
+
Commands.TEXT_LARGE = new Uint8Array([0x1B, 0x21, 0x30]); // 2倍宽高
|
|
81
|
+
// 加粗
|
|
82
|
+
Commands.BOLD_ON = new Uint8Array([0x1B, 0x45, 0x01]);
|
|
83
|
+
Commands.BOLD_OFF = new Uint8Array([0x1B, 0x45, 0x00]);
|
|
84
|
+
// 下划线
|
|
85
|
+
Commands.UNDERLINE_ON = new Uint8Array([0x1B, 0x2D, 0x01]);
|
|
86
|
+
Commands.UNDERLINE_OFF = new Uint8Array([0x1B, 0x2D, 0x00]);
|
|
87
|
+
// 反白
|
|
88
|
+
Commands.INVERT_ON = new Uint8Array([0x1D, 0x42, 0x01]);
|
|
89
|
+
Commands.INVERT_OFF = new Uint8Array([0x1D, 0x42, 0x00]);
|
|
90
|
+
//# sourceMappingURL=commands.js.map
|
|
@@ -0,0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AC,IAAI,EAAE,IAAI,EAAE,IAAI,CAAC,CAAC,CAAC,CAAC,OAAO;AACzD,oBAAW,GAAG,IAAI,UAAU,CAAC,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,CAAC,CAAC,CAAC,CAAC,MAAM;AACxD,mBAAU,GAAG,IAAI,UAAU,CAAC,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,CAAC,CAAC,CAAC,CAAC,OAAO;AAE/D,KAAK;AACE,gBAAO,GAAG,IAAI,UAAU,CAAC,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,CAAC,CAAC,CAAC;AAC7C,iBAAQ,GAAG,IAAI,UAAU,CAAC,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,CAAC,CAAC,CAAC;AAErD,MAAM;AACC,qBAAY,GAAG,IAAI,UAAU,CAAC,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,CAAC,CAAC,CAAC;AAClD,sBAAa,GAAG,IAAI,UAAU,CAAC,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,CAAC,CAAC,CAAC;AAE1D,KAAK;AACE,kBAAS,GAAG,IAAI,UAAU,CAAC,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,CAAC,CAAC,CAAC;AAC/C,mBAAU,GAAG,IAAI,UAAU,CAAC,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,CAAC,CAAC,CAAC"}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 图片处理工具,用于将图片转换为点阵数据供打印机打印
|
|
3
|
+
*/
|
|
4
|
+
export declare class PrinterImage {
|
|
5
|
+
/**
|
|
6
|
+
* 将图片URL转换为ImageData
|
|
7
|
+
* @param imageUrl 图片URL或Base64
|
|
8
|
+
* @param maxWidth 最大宽度(像素)
|
|
9
|
+
*/
|
|
10
|
+
static urlToImageData(imageUrl: string, maxWidth?: number): Promise<{
|
|
11
|
+
data: Uint8ClampedArray;
|
|
12
|
+
width: number;
|
|
13
|
+
height: number;
|
|
14
|
+
}>;
|
|
15
|
+
/**
|
|
16
|
+
* 将图片数据转换为打印机可用的点阵数据
|
|
17
|
+
* @param imageData 图片数据
|
|
18
|
+
* @param width 图片宽度
|
|
19
|
+
* @param threshold 阈值,0-255,默认128
|
|
20
|
+
*/
|
|
21
|
+
static bitmapToRasterData(imageData: Uint8ClampedArray, width: number, threshold?: number): Uint8Array;
|
|
22
|
+
/**
|
|
23
|
+
* 生成打印图片的ESC/POS指令
|
|
24
|
+
* @param rasterData 光栅图像数据
|
|
25
|
+
* @param width 图片宽度(像素)
|
|
26
|
+
* @param height 图片高度(像素)
|
|
27
|
+
*/
|
|
28
|
+
static generateRasterCommand(rasterData: Uint8Array, width: number, height: number): Uint8Array;
|
|
29
|
+
/**
|
|
30
|
+
* 使用Dithering算法处理图像,提高打印效果
|
|
31
|
+
* Floyd-Steinberg抖动算法
|
|
32
|
+
* @param imageData 原始图像数据
|
|
33
|
+
* @param width 图像宽度
|
|
34
|
+
* @param height 图像高度
|
|
35
|
+
* @param threshold 阈值 (0-255)
|
|
36
|
+
* @returns 处理后的图像数据
|
|
37
|
+
*/
|
|
38
|
+
static ditheringProcess(imageData: Uint8ClampedArray, width: number, height: number, threshold?: number): Uint8ClampedArray;
|
|
39
|
+
/**
|
|
40
|
+
* 综合处理图片并生成打印命令
|
|
41
|
+
* @param imageUrl 图片URL或Base64
|
|
42
|
+
* @param options 图片处理选项
|
|
43
|
+
*/
|
|
44
|
+
static processImage(imageUrl: string, options?: {
|
|
45
|
+
maxWidth?: number;
|
|
46
|
+
threshold?: number;
|
|
47
|
+
dithering?: boolean;
|
|
48
|
+
}): Promise<Uint8Array>;
|
|
49
|
+
/**
|
|
50
|
+
* 处理Base64图片数据
|
|
51
|
+
* @param base64 Base64编码的图片数据
|
|
52
|
+
* @param options 图片处理选项
|
|
53
|
+
*/
|
|
54
|
+
static processBase64Image(base64: string, options?: {
|
|
55
|
+
maxWidth?: number;
|
|
56
|
+
threshold?: number;
|
|
57
|
+
dithering?: boolean;
|
|
58
|
+
}): Promise<Uint8Array>;
|
|
59
|
+
}
|
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.PrinterImage = void 0;
|
|
4
|
+
const logger_1 = require("../utils/logger");
|
|
5
|
+
/**
|
|
6
|
+
* 图片处理工具,用于将图片转换为点阵数据供打印机打印
|
|
7
|
+
*/
|
|
8
|
+
class PrinterImage {
|
|
9
|
+
/**
|
|
10
|
+
* 将图片URL转换为ImageData
|
|
11
|
+
* @param imageUrl 图片URL或Base64
|
|
12
|
+
* @param maxWidth 最大宽度(像素)
|
|
13
|
+
*/
|
|
14
|
+
static async urlToImageData(imageUrl, maxWidth = 384) {
|
|
15
|
+
return new Promise((resolve, reject) => {
|
|
16
|
+
const img = new Image();
|
|
17
|
+
img.onload = () => {
|
|
18
|
+
try {
|
|
19
|
+
// 计算调整后的宽高,保持比例
|
|
20
|
+
let width = img.width;
|
|
21
|
+
let height = img.height;
|
|
22
|
+
if (width > maxWidth) {
|
|
23
|
+
const ratio = maxWidth / width;
|
|
24
|
+
width = maxWidth;
|
|
25
|
+
height = Math.floor(height * ratio);
|
|
26
|
+
}
|
|
27
|
+
// 创建canvas并绘制图像
|
|
28
|
+
const canvas = document.createElement('canvas');
|
|
29
|
+
canvas.width = width;
|
|
30
|
+
canvas.height = height;
|
|
31
|
+
const ctx = canvas.getContext('2d');
|
|
32
|
+
if (!ctx) {
|
|
33
|
+
reject(new Error('无法创建2D上下文'));
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
// 绘制图像
|
|
37
|
+
ctx.fillStyle = 'white';
|
|
38
|
+
ctx.fillRect(0, 0, width, height);
|
|
39
|
+
ctx.drawImage(img, 0, 0, width, height);
|
|
40
|
+
// 获取图像数据
|
|
41
|
+
const imageData = ctx.getImageData(0, 0, width, height);
|
|
42
|
+
resolve({
|
|
43
|
+
data: imageData.data,
|
|
44
|
+
width,
|
|
45
|
+
height
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
catch (error) {
|
|
49
|
+
reject(error);
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
img.onerror = () => {
|
|
53
|
+
reject(new Error('图片加载失败'));
|
|
54
|
+
};
|
|
55
|
+
// 设置图片源
|
|
56
|
+
img.crossOrigin = 'Anonymous'; // 允许跨域
|
|
57
|
+
img.src = imageUrl;
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* 将图片数据转换为打印机可用的点阵数据
|
|
62
|
+
* @param imageData 图片数据
|
|
63
|
+
* @param width 图片宽度
|
|
64
|
+
* @param threshold 阈值,0-255,默认128
|
|
65
|
+
*/
|
|
66
|
+
static bitmapToRasterData(imageData, width, threshold = 128) {
|
|
67
|
+
const height = imageData.length / 4 / width;
|
|
68
|
+
// 计算每行字节数(8个点一个字节)
|
|
69
|
+
const bytesPerLine = Math.ceil(width / 8);
|
|
70
|
+
const result = new Uint8Array(bytesPerLine * height);
|
|
71
|
+
for (let y = 0; y < height; y++) {
|
|
72
|
+
for (let x = 0; x < width; x++) {
|
|
73
|
+
const pos = (y * width + x) * 4;
|
|
74
|
+
// 获取像素的灰度值
|
|
75
|
+
const r = imageData[pos];
|
|
76
|
+
const g = imageData[pos + 1];
|
|
77
|
+
const b = imageData[pos + 2];
|
|
78
|
+
const gray = 0.299 * r + 0.587 * g + 0.114 * b;
|
|
79
|
+
// 根据阈值决定是否为黑点
|
|
80
|
+
const isBlack = gray < threshold;
|
|
81
|
+
if (isBlack) {
|
|
82
|
+
// 计算点在结果数组中的位置
|
|
83
|
+
const bytePos = Math.floor(x / 8) + y * bytesPerLine;
|
|
84
|
+
const bitPos = 7 - (x % 8);
|
|
85
|
+
// 设置对应位为1
|
|
86
|
+
result[bytePos] |= (1 << bitPos);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
return result;
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* 生成打印图片的ESC/POS指令
|
|
94
|
+
* @param rasterData 光栅图像数据
|
|
95
|
+
* @param width 图片宽度(像素)
|
|
96
|
+
* @param height 图片高度(像素)
|
|
97
|
+
*/
|
|
98
|
+
static generateRasterCommand(rasterData, width, height) {
|
|
99
|
+
// 每行字节数
|
|
100
|
+
const bytesPerLine = Math.ceil(width / 8);
|
|
101
|
+
// 命令序列
|
|
102
|
+
// 1. 初始化打印机
|
|
103
|
+
// 2. 设置行间距为0 (使图片连续)
|
|
104
|
+
// 3. 对于每一行:
|
|
105
|
+
// - 发送位图数据命令
|
|
106
|
+
// - 发送该行的数据
|
|
107
|
+
// 4. 重置行间距
|
|
108
|
+
// 计算总命令长度
|
|
109
|
+
const headerSize = 4; // ESC @ ESC 3 0
|
|
110
|
+
const lineCommandSize = 4; // ESC * m nL nH
|
|
111
|
+
const resetSize = 2; // ESC 2
|
|
112
|
+
const totalSize = headerSize + (lineCommandSize + bytesPerLine) * height + resetSize;
|
|
113
|
+
const result = new Uint8Array(totalSize);
|
|
114
|
+
let offset = 0;
|
|
115
|
+
// 初始化
|
|
116
|
+
result[offset++] = 0x1B; // ESC
|
|
117
|
+
result[offset++] = 0x40; // @
|
|
118
|
+
// 设置行间距为0
|
|
119
|
+
result[offset++] = 0x1B; // ESC
|
|
120
|
+
result[offset++] = 0x33; // 3
|
|
121
|
+
result[offset++] = 0x00; // 0
|
|
122
|
+
// 计算位图模式
|
|
123
|
+
// 根据打印机不同,选择合适的模式:
|
|
124
|
+
// m=0: 8点单密度
|
|
125
|
+
// m=1: 8点双密度
|
|
126
|
+
// m=32: 24点单密度
|
|
127
|
+
// m=33: 24点双密度
|
|
128
|
+
const mode = 33; // 使用24点双密度模式
|
|
129
|
+
for (let y = 0; y < height; y++) {
|
|
130
|
+
// 位图命令: ESC * m nL nH
|
|
131
|
+
result[offset++] = 0x1B; // ESC
|
|
132
|
+
result[offset++] = 0x2A; // *
|
|
133
|
+
result[offset++] = mode; // 模式
|
|
134
|
+
// 计算宽度字节
|
|
135
|
+
// 由于使用24点,每3个字节表示24个点
|
|
136
|
+
// 需要根据实际宽度计算nL和nH
|
|
137
|
+
const lineWidth = bytesPerLine;
|
|
138
|
+
result[offset++] = lineWidth & 0xFF; // nL
|
|
139
|
+
result[offset++] = (lineWidth >> 8) & 0xFF; // nH
|
|
140
|
+
// 复制该行的图像数据
|
|
141
|
+
const lineOffset = y * bytesPerLine;
|
|
142
|
+
for (let i = 0; i < bytesPerLine; i++) {
|
|
143
|
+
result[offset++] = rasterData[lineOffset + i];
|
|
144
|
+
}
|
|
145
|
+
// 添加换行
|
|
146
|
+
result[offset++] = 0x0A; // LF
|
|
147
|
+
}
|
|
148
|
+
// 重置行间距为默认值
|
|
149
|
+
result[offset++] = 0x1B; // ESC
|
|
150
|
+
result[offset++] = 0x32; // 2
|
|
151
|
+
return result.slice(0, offset); // 返回实际使用的部分
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* 使用Dithering算法处理图像,提高打印效果
|
|
155
|
+
* Floyd-Steinberg抖动算法
|
|
156
|
+
* @param imageData 原始图像数据
|
|
157
|
+
* @param width 图像宽度
|
|
158
|
+
* @param height 图像高度
|
|
159
|
+
* @param threshold 阈值 (0-255)
|
|
160
|
+
* @returns 处理后的图像数据
|
|
161
|
+
*/
|
|
162
|
+
static ditheringProcess(imageData, width, height, threshold = 128) {
|
|
163
|
+
// 创建副本,避免修改原始数据
|
|
164
|
+
const result = new Uint8ClampedArray(imageData.length);
|
|
165
|
+
for (let i = 0; i < imageData.length; i++) {
|
|
166
|
+
result[i] = imageData[i];
|
|
167
|
+
}
|
|
168
|
+
// 创建灰度图像
|
|
169
|
+
const gray = new Uint8Array(width * height);
|
|
170
|
+
for (let y = 0; y < height; y++) {
|
|
171
|
+
for (let x = 0; x < width; x++) {
|
|
172
|
+
const pos = (y * width + x) * 4;
|
|
173
|
+
const r = result[pos];
|
|
174
|
+
const g = result[pos + 1];
|
|
175
|
+
const b = result[pos + 2];
|
|
176
|
+
gray[y * width + x] = Math.round(0.299 * r + 0.587 * g + 0.114 * b);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
// Floyd-Steinberg抖动算法
|
|
180
|
+
for (let y = 0; y < height; y++) {
|
|
181
|
+
for (let x = 0; x < width; x++) {
|
|
182
|
+
const idx = y * width + x;
|
|
183
|
+
const oldPixel = gray[idx];
|
|
184
|
+
// 阈值化处理
|
|
185
|
+
const newPixel = oldPixel < threshold ? 0 : 255;
|
|
186
|
+
gray[idx] = newPixel;
|
|
187
|
+
// 计算误差
|
|
188
|
+
const error = oldPixel - newPixel;
|
|
189
|
+
// 将误差分散到周围像素
|
|
190
|
+
if (x + 1 < width) {
|
|
191
|
+
gray[idx + 1] += error * (7 / 16);
|
|
192
|
+
}
|
|
193
|
+
if (y + 1 < height) {
|
|
194
|
+
if (x > 0) {
|
|
195
|
+
gray[idx + width - 1] += error * (3 / 16);
|
|
196
|
+
}
|
|
197
|
+
gray[idx + width] += error * (5 / 16);
|
|
198
|
+
if (x + 1 < width) {
|
|
199
|
+
gray[idx + width + 1] += error * (1 / 16);
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
// 更新结果数据
|
|
205
|
+
for (let y = 0; y < height; y++) {
|
|
206
|
+
for (let x = 0; x < width; x++) {
|
|
207
|
+
const pos = (y * width + x) * 4;
|
|
208
|
+
const value = gray[y * width + x];
|
|
209
|
+
result[pos] = value;
|
|
210
|
+
result[pos + 1] = value;
|
|
211
|
+
result[pos + 2] = value;
|
|
212
|
+
// Alpha通道保持不变
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
return result;
|
|
216
|
+
}
|
|
217
|
+
/**
|
|
218
|
+
* 综合处理图片并生成打印命令
|
|
219
|
+
* @param imageUrl 图片URL或Base64
|
|
220
|
+
* @param options 图片处理选项
|
|
221
|
+
*/
|
|
222
|
+
static async processImage(imageUrl, options = {}) {
|
|
223
|
+
try {
|
|
224
|
+
const maxWidth = options.maxWidth || 384;
|
|
225
|
+
const threshold = options.threshold || 128;
|
|
226
|
+
const dithering = options.dithering !== undefined ? options.dithering : true;
|
|
227
|
+
// 加载图片并转换为ImageData
|
|
228
|
+
const { data, width, height } = await this.urlToImageData(imageUrl, maxWidth);
|
|
229
|
+
// 应用抖动算法(如果启用)
|
|
230
|
+
let processedData = data;
|
|
231
|
+
if (dithering) {
|
|
232
|
+
processedData = this.ditheringProcess(data, width, height, threshold);
|
|
233
|
+
}
|
|
234
|
+
// 转换为点阵数据
|
|
235
|
+
const rasterData = this.bitmapToRasterData(processedData, width, threshold);
|
|
236
|
+
// 生成打印命令
|
|
237
|
+
return this.generateRasterCommand(rasterData, width, height);
|
|
238
|
+
}
|
|
239
|
+
catch (error) {
|
|
240
|
+
logger_1.logger.error('处理图片失败', error);
|
|
241
|
+
throw error;
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
/**
|
|
245
|
+
* 处理Base64图片数据
|
|
246
|
+
* @param base64 Base64编码的图片数据
|
|
247
|
+
* @param options 图片处理选项
|
|
248
|
+
*/
|
|
249
|
+
static async processBase64Image(base64, options = {}) {
|
|
250
|
+
// 确保是有效的Base64
|
|
251
|
+
if (!base64.startsWith('data:image')) {
|
|
252
|
+
base64 = `data:image/png;base64,${base64}`;
|
|
253
|
+
}
|
|
254
|
+
return await this.processImage(base64, options);
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
exports.PrinterImage = PrinterImage;
|
|
258
|
+
//# sourceMappingURL=image.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"image.js","sourceRoot":"","sources":["../../src/printer/image.ts"],"names":[],"mappings":";;;AAAA,4CAAyC;AAEzC;;GAEG;AACH,MAAa,YAAY;IACvB;;;;OAIG;IACH,MAAM,CAAC,KAAK,CAAC,cAAc,CAAC,QAAgB,EAAE,WAAmB,GAAG;QAKlE,OAAO,IAAI,O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ACV,MAAM,UAAU,GAAG,IAAI,CAAC,kBAAkB,CAAC,aAAa,EAAE,KAAK,EAAE,SAAS,CAAC,CAAC;YAE5E,SAAS;YACT,OAAO,IAAI,CAAC,qBAAqB,CAAC,UAAU,EAAE,KAAK,EAAE,MAAM,CAAC,CAAC;SAC9D;QAAC,OAAO,KAAK,EAAE;YACd,eAAM,CAAC,KAAK,CAAC,QAAQ,EAAE,KAAK,CAAC,CAAC;YAC9B,MAAM,KAAK,CAAC;SACb;IACH,CAAC;IAED;;;;OAIG;IACH,MAAM,CAAC,KAAK,CAAC,kBAAkB,CAAC,MAAc,EAAE,UAI5C,EAAE;QACJ,eAAe;QACf,IAAI,CAAC,MAAM,CAAC,UAAU,CAAC,YAAY,CAAC,EAAE;YACpC,MAAM,GAAG,yBAAyB,MAAM,EAAE,CAAC;SAC5C;QAED,OAAO,MAAM,IAAI,CAAC,YAAY,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAClD,CAAC;CACF;AA9SD,oCA8SC"}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { BluetoothManager } from '../bluetooth';
|
|
2
|
+
export interface PrintOptions {
|
|
3
|
+
align?: 'left' | 'center' | 'right';
|
|
4
|
+
bold?: boolean;
|
|
5
|
+
doubleHeight?: boolean;
|
|
6
|
+
doubleWidth?: boolean;
|
|
7
|
+
underline?: boolean;
|
|
8
|
+
cut?: boolean;
|
|
9
|
+
feed?: number;
|
|
10
|
+
}
|
|
11
|
+
export interface QRCodeOptions {
|
|
12
|
+
size?: number;
|
|
13
|
+
errorCorrection?: number;
|
|
14
|
+
align?: 'left' | 'center' | 'right';
|
|
15
|
+
}
|
|
16
|
+
export interface BarcodeOptions {
|
|
17
|
+
type?: number;
|
|
18
|
+
height?: number;
|
|
19
|
+
width?: number;
|
|
20
|
+
align?: 'left' | 'center' | 'right';
|
|
21
|
+
position?: 'none' | 'above' | 'below' | 'both';
|
|
22
|
+
}
|
|
23
|
+
export interface ImageOptions {
|
|
24
|
+
maxWidth?: number;
|
|
25
|
+
threshold?: number;
|
|
26
|
+
dithering?: boolean;
|
|
27
|
+
align?: 'left' | 'center' | 'right';
|
|
28
|
+
}
|
|
29
|
+
export declare class PrinterManager {
|
|
30
|
+
private bluetooth;
|
|
31
|
+
private lastError;
|
|
32
|
+
constructor(bluetooth: BluetoothManager);
|
|
33
|
+
/**
|
|
34
|
+
* 获取最后一次错误
|
|
35
|
+
*/
|
|
36
|
+
getLastError(): Error | null;
|
|
37
|
+
/**
|
|
38
|
+
* 发送指令
|
|
39
|
+
* @param commands 指令数组
|
|
40
|
+
*/
|
|
41
|
+
sendCommands(commands: Uint8Array[]): Promise<boolean>;
|
|
42
|
+
/**
|
|
43
|
+
* 打印文本
|
|
44
|
+
* @param text 文本内容
|
|
45
|
+
* @param options 打印选项
|
|
46
|
+
*/
|
|
47
|
+
printText(text: string, options?: PrintOptions): Promise<boolean>;
|
|
48
|
+
/**
|
|
49
|
+
* 打印图片
|
|
50
|
+
* @param imageUrlOrBase64 图片URL或Base64字符串
|
|
51
|
+
* @param options 图片打印选项
|
|
52
|
+
*/
|
|
53
|
+
printImage(imageUrlOrBase64: string, options?: ImageOptions): Promise<boolean>;
|
|
54
|
+
/**
|
|
55
|
+
* 打印条形码
|
|
56
|
+
* @param content 条码内容
|
|
57
|
+
* @param options 条码选项
|
|
58
|
+
*/
|
|
59
|
+
printBarcode(content: string, options?: BarcodeOptions): Promise<boolean>;
|
|
60
|
+
/**
|
|
61
|
+
* 打印二维码
|
|
62
|
+
* @param content 二维码内容
|
|
63
|
+
* @param options 二维码选项
|
|
64
|
+
*/
|
|
65
|
+
printQRCode(content: string, options?: QRCodeOptions): Promise<boolean>;
|
|
66
|
+
/**
|
|
67
|
+
* 打印收据
|
|
68
|
+
* @param data 收据数据
|
|
69
|
+
*/
|
|
70
|
+
printReceipt(data: {
|
|
71
|
+
title: string;
|
|
72
|
+
merchant: string;
|
|
73
|
+
items: Array<{
|
|
74
|
+
name: string;
|
|
75
|
+
price: number;
|
|
76
|
+
quantity: number;
|
|
77
|
+
}>;
|
|
78
|
+
total: number;
|
|
79
|
+
date: string;
|
|
80
|
+
footer?: string;
|
|
81
|
+
logo?: string;
|
|
82
|
+
}): Promise<boolean>;
|
|
83
|
+
/**
|
|
84
|
+
* 打印测试页
|
|
85
|
+
*/
|
|
86
|
+
printTestPage(): Promise<boolean>;
|
|
87
|
+
}
|