cpcl.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /* 16hex insert 0 */
  2. function Hex2Str(num) {
  3. if (num.toString(16).length < 2) return "0" + num.toString(16);
  4. else
  5. return num.toString(16);
  6. }
  7. /*****CPCL指令接口****/
  8. /**
  9. * 配置项如下
  10. *
  11. * width: 标签纸的宽度,单位像素點
  12. * height: 标签纸的高度,单位像素點
  13. * 8像素=1mm
  14. * printNum: 打印张数,默认为1
  15. * rotation:页面整体旋转 1-90度 2-180度 3-270度 其他-不旋转
  16. */
  17. export function CreatCPCLPage(width, height, printNum, rotation = 0, offset = 0) {
  18. var strCmd = '! ' + offset + ' 203 203 ' + height + ' ' + printNum + '\n';
  19. strCmd += "PAGE-WIDTH " + width + '\n';
  20. if (rotation == 1)
  21. strCmd += "ZPROTATE90\n";
  22. else if (rotation == 2)
  23. strCmd += "ZPROTATE180\n";
  24. else if (rotation == 3)
  25. strCmd += "ZPROTATE270\n";
  26. else
  27. strCmd += "ZPROTATE0\n";
  28. return strCmd;
  29. }
  30. /**
  31. * 打印文字
  32. * x: 文字方块左上角X座标,单位dot
  33. * y: 文字方块左上角Y座标,单位dot
  34. * fontName,fontSize: 字体,取值: 參考文檔
  35. * rotation: 旋转 1-90度 2-180度 3-270度 其他-不旋转
  36. * content: 文字内容
  37. */
  38. export function addCPCLText(x, y, fontName, fontSize, rotation, content) {
  39. //console.log(fontName,fontSize,rotation, content);
  40. var strCmd = '';
  41. if (rotation == 1) {
  42. strCmd += 'T90 ';
  43. }
  44. if (rotation == 2) {
  45. strCmd += 'T180 ';
  46. }
  47. if (rotation == 3) {
  48. strCmd += 'T270 ';
  49. } else {
  50. strCmd += 'T ';
  51. }
  52. strCmd += fontName + ' ' + fontSize + ' ' + x + ' ' + y + ' ' + content + '\n';
  53. return strCmd;
  54. };
  55. /**
  56. * 打印一维码
  57. *
  58. * x: 文字方块左上角X座标,单位dot
  59. * y: 文字方块左上角Y座标,单位dot
  60. * codeType: 条码类型,取值为128、UPCA、UPCA2、UPCA5、UPCE、UPCE2、UPC5、EAN13、EAN13+2、EAN13+5、
  61. * EAN8、EAN8+2、EAN8+5、39、39C、F39、F39C、93、CODABAR、CODABAR16、ITF、I2OF5
  62. * h: 条码高度,单位dot
  63. * rotation: 顺时针旋转角度,取值如下:
  64. * - 0 不旋转
  65. * - 1 顺时针旋转90度
  66. *
  67. * narrow: 窄条码比例因子(dot) 取值: 參考文檔
  68. * wide: 宽条码比例因子(dot) 取值: 參考文檔
  69. * content: 文字内容
  70. *
  71. */
  72. export function addCPCLBarCode(x, y, codeType, h, rotation, narrow, wide, content) {
  73. var strCmd = '';
  74. if (rotation == 0)
  75. strCmd += 'B ';
  76. else
  77. strCmd += 'VB ';
  78. strCmd += codeType + ' ' + narrow + ' ' + wide + ' ' + h + ' ' + x + ' ' + y + ' ' + content + '\n'
  79. return strCmd;
  80. };
  81. /**
  82. * 打印二维码
  83. *
  84. * x: 文字方块左上角X座标,单位dot
  85. * y: 文字方块左上角Y座标,单位dot
  86. * level: 错误纠正能力等级,取值为L(7%)、M(15%)、Q(25%)、H(30%)
  87. * ver: 1-10 版本,根据内容调整以获取合适容量
  88. * scale: 1-10 放大倍数
  89. * content: 文字内容
  90. *
  91. */
  92. export function addCPCLQRCode(x, y, level, ver, scale, content) {
  93. var strCmd = 'B QR ' + x + ' ' + y + ' M ' + ver + ' U ' + scale + '\n' + level + 'A,' + content + '\n';
  94. strCmd += 'ENDQR\n';
  95. return strCmd;
  96. };
  97. /**
  98. * 放大指令
  99. * scaleX: 横向放大倍数 1,2,3等整数
  100. * scaleY: 纵向放大倍数 1,2,3等整数
  101. */
  102. export function addCPCLSETMAG(scaleX, scaleY) {
  103. var strCmd = 'SETMAG ' + scaleX + ' ' + scaleY + '\n';
  104. return strCmd;
  105. };
  106. /**
  107. * 对齐指令 0-左对齐 1-右对齐 2-居中
  108. */
  109. export function addCPCLLocation(set) {
  110. var strCmd = '';
  111. if (set == 1) {
  112. strCmd += 'RIGHT\n';
  113. } else if (set == 2) {
  114. strCmd += 'CENTER\n';
  115. } else {
  116. strCmd += 'LEFT\n';
  117. }
  118. return strCmd;
  119. };
  120. /**
  121. * 反白线 x0,y0,x1,y1,width
  122. */
  123. export function addCPCLInverseLine(x0, y0, x1, y1, width) {
  124. var strCmd = 'IL ' + x0 + ' ' + y0 + ' ' + x1 + ' ' + y1 + ' ' + width + '\n';
  125. return strCmd;
  126. };
  127. /**
  128. * 画线 x0,y0,x1,y1,width
  129. */
  130. export function addCPCLLine(x0, y0, x1, y1, width) {
  131. var strCmd = 'L ' + x0 + ' ' + y0 + ' ' + x1 + ' ' + y1 + ' ' + width + '\n';
  132. return strCmd;
  133. };
  134. /**
  135. * 画框 x0,y0,x1,y1,width
  136. */
  137. export function addCPCLBox(x0, y0, x1, y1, width) {
  138. var strCmd = 'BOX ' + x0 + ' ' + y0 + ' ' + x1 + ' ' + y1 + ' ' + width + '\n';
  139. return strCmd;
  140. };
  141. /**
  142. * 字体加粗
  143. */
  144. export function addCPCLSETBOLD(bold) {
  145. var strCmd = 'SETBOLD ' + bold + '\n';
  146. return strCmd;
  147. };
  148. /**
  149. * 字体下划线
  150. */
  151. export function addCPCLUNDERLINE(c) {
  152. var strCmd = 'UNDERLINE ';
  153. if (c) strCmd += 'ON\n';
  154. else if (c) strCmd += 'OFF\n';
  155. return strCmd;
  156. };
  157. /**
  158. * 水印打印灰度等级 0-255
  159. */
  160. export function addCPCLBACKGROUND(level) {
  161. var strCmd = 'BACKGROUND ';
  162. if (level > 255 || level < 0) level = 255;
  163. strCmd += level + '\n';
  164. return strCmd;
  165. };
  166. /**
  167. * 打印水印文字
  168. * x: 文字方块左上角X座标,单位dot
  169. * y: 文字方块左上角Y座标,单位dot
  170. * fontName,fontSize: 字体,取值: 參考文檔
  171. * rotation: 旋转 1-90度 2-180度 3-270度 其他-不旋转
  172. * content: 文字内容
  173. */
  174. export function addCPCLBKVText(x, y, fontName, fontSize, rotation, content) {
  175. //console.log(fontName,fontSize,rotation, content);
  176. var strCmd = '';
  177. if (rotation == 1) {
  178. strCmd += 'BKT90 ';
  179. }
  180. if (rotation == 2) {
  181. strCmd += 'BKT180 ';
  182. }
  183. if (rotation == 3) {
  184. strCmd += 'BKT270 ';
  185. } else {
  186. strCmd += 'BKT ';
  187. }
  188. strCmd += fontName + ' ' + fontSize + ' ' + x + ' ' + y + ' ' + content + '\n';
  189. return strCmd;
  190. };
  191. /**
  192. * 标签缝隙定位指令
  193. */
  194. export function addCPCLGAP() {
  195. var strCmd = 'GAP-SENSE\nFORM\n';
  196. return strCmd;
  197. };
  198. /**
  199. * 标签右黑标检测指令
  200. */
  201. export function addCPCLSENSE() {
  202. var strCmd = 'BAR-SENSE\nFORM\n';
  203. return strCmd;
  204. };
  205. /**
  206. * 标签左黑标检测指令
  207. */
  208. export function addCPCLSENSELEFT() {
  209. var strCmd = 'BAR-SENSE LEFT\nFORM\n';
  210. return strCmd;
  211. };
  212. /**
  213. * 打印指令
  214. */
  215. export function addCPCLPrint() {
  216. var strCmd = 'PRINT\n';
  217. return strCmd;
  218. };
  219. /**
  220. * 图片打印指令
  221. * x: 文字方块左上角X座标,单位dot
  222. * y: 文字方块左上角Y座标,单位dot
  223. * data{
  224. threshold,//0/1提取的灰度级
  225. width,//图像宽度
  226. height,//图像高度
  227. imageData , //图像数据
  228. }
  229. */
  230. export function addCPCLImageCmd(x, y, data) {
  231. var strImgCmd = '';
  232. const threshold = data.threshold || 180;
  233. let myBitmapWidth = data.width,
  234. myBitmapHeight = data.height;
  235. let len = parseInt((myBitmapWidth + 7) / 8); //一行的数据长度
  236. //console.log('len=',len);
  237. //console.log('myBitmapWidth=',myBitmapWidth);
  238. //console.log('myBitmapHeight=',myBitmapHeight);
  239. let ndata = 0;
  240. let i = 0;
  241. let j = 0;
  242. let sendImageData = new ArrayBuffer(len * myBitmapHeight);
  243. sendImageData = new Uint8Array(sendImageData);
  244. let pix = data.imageData;
  245. console.log('pix=', pix);
  246. for (i = 0; i < myBitmapHeight; i++) {
  247. for (j = 0; j < len; j++) {
  248. sendImageData[ndata + j] = 0;
  249. }
  250. for (j = 0; j < myBitmapWidth; j++) {
  251. const grayPixle1 = grayPixle(pix.slice((i * myBitmapWidth + j) * 4, (i * myBitmapWidth + j) * 4 + 3));
  252. if (grayPixle1 < threshold)
  253. sendImageData[ndata + parseInt(j / 8)] |= (0x80 >> (j % 8));
  254. }
  255. ndata += len;
  256. }
  257. //console.log('sendImageData=',sendImageData);
  258. //CPCL指令图片数据
  259. strImgCmd += 'EG ' + len + ' ' + myBitmapHeight + ' ' + x + ' ' + y + ' ';
  260. for (i = 0; i < sendImageData.length; i++) {
  261. strImgCmd += Hex2Str(sendImageData[i]);
  262. }
  263. strImgCmd += '\n';
  264. //console.log(strImgCmd);
  265. return strImgCmd;
  266. }
  267. module.exports.type = 'cpcl';