|
@@ -1,122 +1,288 @@
|
|
|
+/* 16hex insert 0 */
|
|
|
+function Hex2Str(num) {
|
|
|
+ if (num.toString(16).length < 2) return "0" + num.toString(16);
|
|
|
+ else
|
|
|
+ return num.toString(16);
|
|
|
+}
|
|
|
+/*****CPCL指令接口****/
|
|
|
+
|
|
|
/**
|
|
|
- * cpcl 命令打印工具类
|
|
|
- * 2021.04.26 uni-app版本
|
|
|
- * @auth boolTrue
|
|
|
- */
|
|
|
-var gbk = require("./printUtil-GBK.js");
|
|
|
-var jpPrinter = {
|
|
|
- createNew: function() {
|
|
|
- var jpPrinter = {};
|
|
|
- var data = "";
|
|
|
- var command = []
|
|
|
- var rawCommand = ''
|
|
|
-
|
|
|
- jpPrinter.name = "标签模式";
|
|
|
-
|
|
|
- jpPrinter.addCommand = function(content) { //将指令转成数组装起
|
|
|
- rawCommand += content
|
|
|
- }
|
|
|
-
|
|
|
- jpPrinter.init = function(width, height, printNum, rotation = 0, offset = 0) {
|
|
|
- var strCmd = '! ' + offset + ' 200 200 ' + height + ' ' + printNum + '\n';
|
|
|
- strCmd += "PAGE-WIDTH " + width + '\n';
|
|
|
- if (rotation == 1)
|
|
|
- strCmd += "ZPROTATE90\n";
|
|
|
- else if (rotation == 2)
|
|
|
- strCmd += "ZPROTATE180\n";
|
|
|
- else if (rotation == 3)
|
|
|
- strCmd += "ZPROTATE270\n";
|
|
|
- else
|
|
|
- strCmd += "ZPROTATE0\n";
|
|
|
- jpPrinter.addCommand(strCmd)
|
|
|
- };
|
|
|
-
|
|
|
- jpPrinter.setText = function(x, y, font, xsize, ysize, str) { //横向打印文字
|
|
|
- data = "T " + "4" + " " + xsize + " " + x + " " + y + " " + str + "\n"
|
|
|
- jpPrinter.addCommand(data)
|
|
|
- };
|
|
|
-
|
|
|
- jpPrinter.setGap = function(pageWidth) { //设置标签模式
|
|
|
- data = "GAP-SENSE" + "\n";
|
|
|
- jpPrinter.addCommand(data)
|
|
|
- };
|
|
|
-
|
|
|
- jpPrinter.setSpeed = function(printSpeed) { //设置打印机速度0 - 5
|
|
|
- data = "SPEED " + printSpeed + "\n";
|
|
|
- jpPrinter.addCommand(data)
|
|
|
- };
|
|
|
-
|
|
|
- jpPrinter.setDensity = function(printDensity) { //设置打印机浓度最亮的打印输出为对比度级别 0。最暗的对比度级别为 3。
|
|
|
- data = "CONTRAST " + printDensity + "\n";
|
|
|
- jpPrinter.addCommand(data)
|
|
|
- };
|
|
|
-
|
|
|
- jpPrinter.setMag = function(w,h) { //字体放大指定的放大倍数。
|
|
|
- data = "SETMAG " + w + " " + h + "\n"
|
|
|
- jpPrinter.addCommand(data)
|
|
|
- };
|
|
|
-
|
|
|
- jpPrinter.setLine = function(x0, y0, x1, y1, width) { //绘制线条
|
|
|
- data = "LINE " + x0 + " " + y0 + " " + x1 + " " + y1 + " " + width + "\n"
|
|
|
- jpPrinter.addCommand(data)
|
|
|
- };
|
|
|
-
|
|
|
- jpPrinter.setBox = function(x_start, y_start, x_end, y_end, thickness) { //绘制方框
|
|
|
- data = "BOX " + x_start + " " + y_start + " " + x_end + " " + y_end + " " + thickness + "\n";
|
|
|
- jpPrinter.addCommand(data)
|
|
|
- };
|
|
|
-
|
|
|
- jpPrinter.setFeed = function(feed) { //将纸向前推出n
|
|
|
- data = "PREFEED " + feed + "\n";
|
|
|
- jpPrinter.addCommand(data)
|
|
|
- };
|
|
|
-
|
|
|
- jpPrinter.setQR = function(x, y, level, ver, scale, content) { //打印二维码
|
|
|
- var strCmd = 'B QR ' + x + ' ' + y + ' M ' + ver + ' U ' + scale + '\n' + level + 'A,' + content + '\n';
|
|
|
- strCmd += 'ENDQR\n';
|
|
|
- jpPrinter.addCommand(strCmd)
|
|
|
- };
|
|
|
-
|
|
|
- jpPrinter.setBar = function(type, width, ratio, height, x, y, content) { //打印条形码
|
|
|
- data = "BARCODE " + type + " " + width + " " + ratio + " " + height + " " + x + " " + y + " " + content + "\n"
|
|
|
- jpPrinter.addCommand(data)
|
|
|
- };
|
|
|
-
|
|
|
- jpPrinter.setSound = function(interval) { //控制蜂鸣器,蜂鸣持续时间
|
|
|
- data = "BEEP " + interval + "\n";
|
|
|
- jpPrinter.addCommand(data)
|
|
|
- };
|
|
|
-
|
|
|
- jpPrinter.setPageForm = function() { //换页
|
|
|
- data = "FORM "+"\n"
|
|
|
- jpPrinter.addCommand(data)
|
|
|
- };
|
|
|
-
|
|
|
- jpPrinter.setPagePrint = function() { //打印页面
|
|
|
- data = "PRINT "+"\n"
|
|
|
- jpPrinter.addCommand(data)
|
|
|
- };
|
|
|
-
|
|
|
- // 添加命令
|
|
|
- jpPrinter.RawCommand = function(data) {
|
|
|
- jpPrinter.addCommand(data)
|
|
|
- }
|
|
|
-
|
|
|
- //获取打印数据
|
|
|
- jpPrinter.getData = function() {
|
|
|
- let buffer = gbk.strToGBKByte(rawCommand)
|
|
|
- return buffer;
|
|
|
- };
|
|
|
-
|
|
|
- jpPrinter.getRawData = function() {
|
|
|
- return rawCommand;
|
|
|
- };
|
|
|
- jpPrinter.clearCommand = function() {
|
|
|
- rawCommand = ''
|
|
|
- };
|
|
|
-
|
|
|
- return jpPrinter;
|
|
|
+ * 配置项如下
|
|
|
+ *
|
|
|
+ * width: 标签纸的宽度,单位像素點
|
|
|
+ * height: 标签纸的高度,单位像素點
|
|
|
+ * 8像素=1mm
|
|
|
+ * printNum: 打印张数,默认为1
|
|
|
+ * rotation:页面整体旋转 1-90度 2-180度 3-270度 其他-不旋转
|
|
|
+ */
|
|
|
+export function CreatCPCLPage(width, height, printNum, rotation = 0, offset = 0) {
|
|
|
+ var strCmd = '! ' + offset + ' 203 203 ' + height + ' ' + printNum + '\n';
|
|
|
+ strCmd += "PAGE-WIDTH " + width + '\n';
|
|
|
+ if (rotation == 1)
|
|
|
+ strCmd += "ZPROTATE90\n";
|
|
|
+ else if (rotation == 2)
|
|
|
+ strCmd += "ZPROTATE180\n";
|
|
|
+ else if (rotation == 3)
|
|
|
+ strCmd += "ZPROTATE270\n";
|
|
|
+ else
|
|
|
+ strCmd += "ZPROTATE0\n";
|
|
|
+ return strCmd;
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * 打印文字
|
|
|
+ * x: 文字方块左上角X座标,单位dot
|
|
|
+ * y: 文字方块左上角Y座标,单位dot
|
|
|
+ * fontName,fontSize: 字体,取值: 參考文檔
|
|
|
+ * rotation: 旋转 1-90度 2-180度 3-270度 其他-不旋转
|
|
|
+ * content: 文字内容
|
|
|
+ */
|
|
|
+export function addCPCLText(x, y, fontName, fontSize, rotation, content) {
|
|
|
+ //console.log(fontName,fontSize,rotation, content);
|
|
|
+ var strCmd = '';
|
|
|
+ if (rotation == 1) {
|
|
|
+ strCmd += 'T90 ';
|
|
|
+ }
|
|
|
+ if (rotation == 2) {
|
|
|
+ strCmd += 'T180 ';
|
|
|
+ }
|
|
|
+ if (rotation == 3) {
|
|
|
+ strCmd += 'T270 ';
|
|
|
+ } else {
|
|
|
+ strCmd += 'T ';
|
|
|
+ }
|
|
|
+ strCmd += fontName + ' ' + fontSize + ' ' + x + ' ' + y + ' ' + content + '\n';
|
|
|
+ return strCmd;
|
|
|
+};
|
|
|
+
|
|
|
+/**
|
|
|
+ * 打印一维码
|
|
|
+ *
|
|
|
+ * x: 文字方块左上角X座标,单位dot
|
|
|
+ * y: 文字方块左上角Y座标,单位dot
|
|
|
+ * codeType: 条码类型,取值为128、UPCA、UPCA2、UPCA5、UPCE、UPCE2、UPC5、EAN13、EAN13+2、EAN13+5、
|
|
|
+ * EAN8、EAN8+2、EAN8+5、39、39C、F39、F39C、93、CODABAR、CODABAR16、ITF、I2OF5
|
|
|
+ * h: 条码高度,单位dot
|
|
|
+ * rotation: 顺时针旋转角度,取值如下:
|
|
|
+ * - 0 不旋转
|
|
|
+ * - 1 顺时针旋转90度
|
|
|
+ *
|
|
|
+ * narrow: 窄条码比例因子(dot) 取值: 參考文檔
|
|
|
+ * wide: 宽条码比例因子(dot) 取值: 參考文檔
|
|
|
+ * content: 文字内容
|
|
|
+ *
|
|
|
+ */
|
|
|
+export function addCPCLBarCode(x, y, codeType, h, rotation, narrow, wide, content) {
|
|
|
+ var strCmd = '';
|
|
|
+ if (rotation == 0)
|
|
|
+ strCmd += 'B ';
|
|
|
+ else
|
|
|
+ strCmd += 'VB ';
|
|
|
+ strCmd += codeType + ' ' + narrow + ' ' + wide + ' ' + h + ' ' + x + ' ' + y + ' ' + content + '\n'
|
|
|
+ return strCmd;
|
|
|
+};
|
|
|
+
|
|
|
+/**
|
|
|
+ * 打印二维码
|
|
|
+ *
|
|
|
+ * x: 文字方块左上角X座标,单位dot
|
|
|
+ * y: 文字方块左上角Y座标,单位dot
|
|
|
+ * level: 错误纠正能力等级,取值为L(7%)、M(15%)、Q(25%)、H(30%)
|
|
|
+ * ver: 1-10 版本,根据内容调整以获取合适容量
|
|
|
+ * scale: 1-10 放大倍数
|
|
|
+ * content: 文字内容
|
|
|
+ *
|
|
|
+ */
|
|
|
+export function addCPCLQRCode(x, y, level, ver, scale, content) {
|
|
|
+ var strCmd = 'B QR ' + x + ' ' + y + ' M ' + ver + ' U ' + scale + '\n' + level + 'A,' + content + '\n';
|
|
|
+ strCmd += 'ENDQR\n';
|
|
|
+ return strCmd;
|
|
|
+};
|
|
|
+
|
|
|
+/**
|
|
|
+ * 放大指令
|
|
|
+ * scaleX: 横向放大倍数 1,2,3等整数
|
|
|
+ * scaleY: 纵向放大倍数 1,2,3等整数
|
|
|
+ */
|
|
|
+export function addCPCLSETMAG(scaleX, scaleY) {
|
|
|
+ var strCmd = 'SETMAG ' + scaleX + ' ' + scaleY + '\n';
|
|
|
+ return strCmd;
|
|
|
+};
|
|
|
+
|
|
|
+/**
|
|
|
+ * 对齐指令 0-左对齐 1-右对齐 2-居中
|
|
|
+ */
|
|
|
+export function addCPCLLocation(set) {
|
|
|
+ var strCmd = '';
|
|
|
+ if (set == 1) {
|
|
|
+ strCmd += 'RIGHT\n';
|
|
|
+ } else if (set == 2) {
|
|
|
+ strCmd += 'CENTER\n';
|
|
|
+ } else {
|
|
|
+ strCmd += 'LEFT\n';
|
|
|
+ }
|
|
|
+ return strCmd;
|
|
|
+};
|
|
|
+
|
|
|
+/**
|
|
|
+ * 反白线 x0,y0,x1,y1,width
|
|
|
+ */
|
|
|
+export function addCPCLInverseLine(x0, y0, x1, y1, width) {
|
|
|
+ var strCmd = 'IL ' + x0 + ' ' + y0 + ' ' + x1 + ' ' + y1 + ' ' + width + '\n';
|
|
|
+ return strCmd;
|
|
|
+};
|
|
|
+
|
|
|
+/**
|
|
|
+ * 画线 x0,y0,x1,y1,width
|
|
|
+ */
|
|
|
+export function addCPCLLine(x0, y0, x1, y1, width) {
|
|
|
+ var strCmd = 'L ' + x0 + ' ' + y0 + ' ' + x1 + ' ' + y1 + ' ' + width + '\n';
|
|
|
+ return strCmd;
|
|
|
+};
|
|
|
+
|
|
|
+/**
|
|
|
+ * 画框 x0,y0,x1,y1,width
|
|
|
+ */
|
|
|
+export function addCPCLBox(x0, y0, x1, y1, width) {
|
|
|
+ var strCmd = 'BOX ' + x0 + ' ' + y0 + ' ' + x1 + ' ' + y1 + ' ' + width + '\n';
|
|
|
+ return strCmd;
|
|
|
+};
|
|
|
+
|
|
|
+/**
|
|
|
+ * 字体加粗
|
|
|
+ */
|
|
|
+export function addCPCLSETBOLD(bold) {
|
|
|
+ var strCmd = 'SETBOLD ' + bold + '\n';
|
|
|
+ return strCmd;
|
|
|
+};
|
|
|
+
|
|
|
+/**
|
|
|
+ * 字体下划线
|
|
|
+ */
|
|
|
+export function addCPCLUNDERLINE(c) {
|
|
|
+ var strCmd = 'UNDERLINE ';
|
|
|
+ if (c) strCmd += 'ON\n';
|
|
|
+ else if (c) strCmd += 'OFF\n';
|
|
|
+ return strCmd;
|
|
|
+};
|
|
|
+
|
|
|
+/**
|
|
|
+ * 水印打印灰度等级 0-255
|
|
|
+ */
|
|
|
+export function addCPCLBACKGROUND(level) {
|
|
|
+ var strCmd = 'BACKGROUND ';
|
|
|
+ if (level > 255 || level < 0) level = 255;
|
|
|
+ strCmd += level + '\n';
|
|
|
+ return strCmd;
|
|
|
+};
|
|
|
+
|
|
|
+/**
|
|
|
+ * 打印水印文字
|
|
|
+ * x: 文字方块左上角X座标,单位dot
|
|
|
+ * y: 文字方块左上角Y座标,单位dot
|
|
|
+ * fontName,fontSize: 字体,取值: 參考文檔
|
|
|
+ * rotation: 旋转 1-90度 2-180度 3-270度 其他-不旋转
|
|
|
+ * content: 文字内容
|
|
|
+ */
|
|
|
+export function addCPCLBKVText(x, y, fontName, fontSize, rotation, content) {
|
|
|
+ //console.log(fontName,fontSize,rotation, content);
|
|
|
+ var strCmd = '';
|
|
|
+ if (rotation == 1) {
|
|
|
+ strCmd += 'BKT90 ';
|
|
|
+ }
|
|
|
+ if (rotation == 2) {
|
|
|
+ strCmd += 'BKT180 ';
|
|
|
+ }
|
|
|
+ if (rotation == 3) {
|
|
|
+ strCmd += 'BKT270 ';
|
|
|
+ } else {
|
|
|
+ strCmd += 'BKT ';
|
|
|
}
|
|
|
+ strCmd += fontName + ' ' + fontSize + ' ' + x + ' ' + y + ' ' + content + '\n';
|
|
|
+ return strCmd;
|
|
|
+};
|
|
|
+
|
|
|
+
|
|
|
+/**
|
|
|
+ * 标签缝隙定位指令
|
|
|
+ */
|
|
|
+export function addCPCLGAP() {
|
|
|
+ var strCmd = 'GAP-SENSE\nFORM\n';
|
|
|
+ return strCmd;
|
|
|
};
|
|
|
|
|
|
-module.exports.jpPrinter = jpPrinter;
|
|
|
+/**
|
|
|
+ * 标签右黑标检测指令
|
|
|
+ */
|
|
|
+export function addCPCLSENSE() {
|
|
|
+ var strCmd = 'BAR-SENSE\nFORM\n';
|
|
|
+ return strCmd;
|
|
|
+};
|
|
|
+
|
|
|
+/**
|
|
|
+ * 标签左黑标检测指令
|
|
|
+ */
|
|
|
+export function addCPCLSENSELEFT() {
|
|
|
+ var strCmd = 'BAR-SENSE LEFT\nFORM\n';
|
|
|
+ return strCmd;
|
|
|
+};
|
|
|
+
|
|
|
+/**
|
|
|
+ * 打印指令
|
|
|
+ */
|
|
|
+export function addCPCLPrint() {
|
|
|
+ var strCmd = 'PRINT\n';
|
|
|
+ return strCmd;
|
|
|
+};
|
|
|
+
|
|
|
+/**
|
|
|
+ * 图片打印指令
|
|
|
+ * x: 文字方块左上角X座标,单位dot
|
|
|
+ * y: 文字方块左上角Y座标,单位dot
|
|
|
+ * data{
|
|
|
+ threshold,//0/1提取的灰度级
|
|
|
+ width,//图像宽度
|
|
|
+ height,//图像高度
|
|
|
+ imageData , //图像数据
|
|
|
+ }
|
|
|
+ */
|
|
|
+export function addCPCLImageCmd(x, y, data) {
|
|
|
+ var strImgCmd = '';
|
|
|
+ const threshold = data.threshold || 180;
|
|
|
+ let myBitmapWidth = data.width,
|
|
|
+ myBitmapHeight = data.height;
|
|
|
+ let len = parseInt((myBitmapWidth + 7) / 8); //一行的数据长度
|
|
|
+ //console.log('len=',len);
|
|
|
+ //console.log('myBitmapWidth=',myBitmapWidth);
|
|
|
+ //console.log('myBitmapHeight=',myBitmapHeight);
|
|
|
+ let ndata = 0;
|
|
|
+ let i = 0;
|
|
|
+ let j = 0;
|
|
|
+ let sendImageData = new ArrayBuffer(len * myBitmapHeight);
|
|
|
+ sendImageData = new Uint8Array(sendImageData);
|
|
|
+ let pix = data.imageData;
|
|
|
+ console.log('pix=', pix);
|
|
|
+
|
|
|
+ for (i = 0; i < myBitmapHeight; i++) {
|
|
|
+ for (j = 0; j < len; j++) {
|
|
|
+ sendImageData[ndata + j] = 0;
|
|
|
+ }
|
|
|
+ for (j = 0; j < myBitmapWidth; j++) {
|
|
|
+ const grayPixle1 = grayPixle(pix.slice((i * myBitmapWidth + j) * 4, (i * myBitmapWidth + j) * 4 + 3));
|
|
|
+ if (grayPixle1 < threshold)
|
|
|
+ sendImageData[ndata + parseInt(j / 8)] |= (0x80 >> (j % 8));
|
|
|
+
|
|
|
+ }
|
|
|
+ ndata += len;
|
|
|
+ }
|
|
|
+ //console.log('sendImageData=',sendImageData);
|
|
|
+ //CPCL指令图片数据
|
|
|
+ strImgCmd += 'EG ' + len + ' ' + myBitmapHeight + ' ' + x + ' ' + y + ' ';
|
|
|
+ for (i = 0; i < sendImageData.length; i++) {
|
|
|
+ strImgCmd += Hex2Str(sendImageData[i]);
|
|
|
+ }
|
|
|
+ strImgCmd += '\n';
|
|
|
+ //console.log(strImgCmd);
|
|
|
+ return strImgCmd;
|
|
|
+}
|
|
|
+module.exports.type = 'cpcl';
|