EpsonCommands.java 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074
  1. package com.gzlh.utils;
  2. import java.lang.annotation.Retention;
  3. import java.lang.annotation.Target;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6. import static java.lang.annotation.ElementType.ANNOTATION_TYPE;
  7. import static java.lang.annotation.RetentionPolicy.SOURCE;
  8. public class EpsonCommands {
  9. public static final int HEIGHT_PARTING_DEFAULT = 255;
  10. private static String hexStr = "0123456789ABCDEF";
  11. private static String[] binaryArray = {"0000", "0001", "0010", "0011",
  12. "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011",
  13. "1100", "1101", "1110", "1111"};
  14. public static final byte ESC = 0x1B; // 指令换码
  15. public static final byte FS = 0x1C; // 文本分隔符
  16. public static final byte GS = 0x1D; // 组分隔符
  17. public static final byte DLE = 16; // 数据连接换码
  18. public static final byte EOT = 4; // 传输结束
  19. public static final byte ENQ = 5; // 询问字符
  20. public static final byte SP = 32; // 空格
  21. public static final byte LF = 0x0A; // 打印并换行(水平定位)
  22. public static final byte HT = 9; // 水平制表(横向列表)
  23. public static final byte CR = 13; // 归位键
  24. public static final byte FF = 12;// 走纸控制(打印并回到标准模式(在页模式下) )
  25. public static final byte CAN = 24; // 作废(页模式下取消打印数据 )
  26. private static final byte EXCLAMATION = 0x21; // ASCII 33
  27. /**
  28. * CodePage table
  29. */
  30. public static final byte CODE_PAGE_PC437 = 0;
  31. public static final byte CODE_PAGE_KATAKANA = 1;
  32. public static final byte CODE_PAGE_PC850 = 2;
  33. public static final byte CODE_PAGE_PC860 = 3;
  34. public static final byte CODE_PAGE_PC863 = 4;
  35. public static final byte CODE_PAGE_PC865 = 5;
  36. public static final byte CODE_PAGE_WPC1252 = 16;
  37. public static final byte CODE_PAGE_PC866 = 17;
  38. public static final byte CODE_PAGE_PC852 = 18;
  39. public static final byte CODE_PAGE_PC858 = 19;
  40. public static final byte CODE_PAGE_THAI_42 = 20;
  41. public static final byte CODE_PAGE_THAI_11 = 21;
  42. public static final byte CODE_PAGE_THAI_13 = 22;
  43. public static final byte CODE_PAGE_THAI_14 = 23;
  44. public static final byte CODE_PAGE_THAI_16 = 24;
  45. public static final byte CODE_PAGE_THAI_17 = 25;
  46. public static final byte CODE_PAGE_THAI_18 = 26;
  47. @Retention(SOURCE)
  48. @Target({ANNOTATION_TYPE})
  49. public @interface ByteDef {
  50. /**
  51. * Defines the allowed constants for this element
  52. */
  53. byte[] value() default {};
  54. /**
  55. * Defines whether the constants can be used as a flag, or just as an enum (the default)
  56. */
  57. boolean flag() default false;
  58. }
  59. @ByteDef(flag = true, value = {
  60. CODE_PAGE_PC437,
  61. CODE_PAGE_KATAKANA,
  62. CODE_PAGE_PC850,
  63. CODE_PAGE_PC860,
  64. CODE_PAGE_PC863,
  65. CODE_PAGE_PC865,
  66. CODE_PAGE_WPC1252,
  67. CODE_PAGE_PC866,
  68. CODE_PAGE_PC852,
  69. CODE_PAGE_PC858,
  70. CODE_PAGE_THAI_42,
  71. CODE_PAGE_THAI_11,
  72. CODE_PAGE_THAI_13,
  73. CODE_PAGE_THAI_14,
  74. CODE_PAGE_THAI_16,
  75. CODE_PAGE_THAI_17,
  76. CODE_PAGE_THAI_18
  77. })
  78. public @interface CodePage {
  79. }
  80. /**
  81. * BarCode table
  82. */
  83. public static class BarCode {
  84. public static final byte UPC_A = 0;
  85. public static final byte UPC_E = 1;
  86. public static final byte EAN13 = 2;
  87. public static final byte EAN8 = 3;
  88. public static final byte CODE39 = 4;
  89. public static final byte ITF = 5;
  90. public static final byte NW7 = 6;
  91. //public static final byte CODE93 = 72;
  92. public static final byte CODE128 = 73;
  93. }
  94. /**
  95. * <p>
  96. * Initialize printer
  97. * <p>
  98. * Clears the data in the print buffer and resets the printer modes to the modes that were
  99. * in effect when the power was turned on.
  100. * <p>
  101. * Format
  102. * ASCII : ESC @
  103. * HEX : 0x1B 0x40
  104. * Decimal : 27 64
  105. */
  106. public static final byte[] RESET = {ESC, 0x40};
  107. /**
  108. * Select justification
  109. * <p>
  110. * Aligns all the data in one line to the position specified by n as follows.<p>
  111. * <p>
  112. * Format <p>
  113. * ASCII : ESC a n
  114. * HEX : 0x1B 0x61 n
  115. * Decimal : 27 97 n
  116. * <p>
  117. * Range 0 <= n <= 2 or 48 <= n <= 50 <p>
  118. * Default n = 0 <p>
  119. * Left justification : 0 or 48 <p>
  120. * Centering : 1 or 49 <p>
  121. * Right justification : 2 or 50 <p>
  122. */
  123. public static final byte[] ALIGN_LEFT = {ESC, 0x61, 0x00};
  124. public static final byte[] ALIGN_CENTER = {ESC, 0x61, 0x01};
  125. public static final byte[] ALIGN_RIGHT = {ESC, 0x61, 0x02};
  126. /**
  127. * Print and line feed
  128. * <p>
  129. * Prints the data in the print buffer and feeds one line based on the current line spacing.
  130. * <p>
  131. * Format
  132. * ASCII : LF
  133. * HEX : 0x0A
  134. * Decimal : 10
  135. */
  136. public static final byte[] LINE_FEED = {LF};
  137. /**
  138. * Print and feed n lines.
  139. * <p>
  140. * Prints the data in the print buffer and feeds n lines .
  141. * <p>
  142. * Format <p>
  143. * ASCII : ESC d n
  144. * HEX : 0x1B 0x64 n
  145. * Decimal : 27 100 n
  146. * <p>
  147. * Range 0 <= n <= 255 <p>
  148. * Default n = 1 <p>
  149. */
  150. public static final byte[] LINE_FEED_N = {ESC, 0x64, 0x01};
  151. /**
  152. * Print and reverse feed n lines.
  153. * <p>
  154. * Prints the data in the print buffer and feeds n lines in the reverse direction.
  155. * <p>
  156. * Format <p>
  157. * ASCII : ESC e n
  158. * HEX : 0x1B 0x65 n
  159. * Decimal : 27 101 n
  160. * <p>
  161. * Range 0 <= n <= 255 <p>
  162. * Default n = 0 <p>
  163. * <p>
  164. * Note: <p>
  165. * With the some printer,the range of n is 0 <= n <= 2.
  166. */
  167. public static final byte[] LINE_FEED_REVERSE = {ESC, 0x65, 0x01};
  168. /**
  169. * Turn emphasized mode on/off
  170. * <p>
  171. * Turns emphasized mode on or off.<p>
  172. * When the LBS of n is 0, emphasized mode is turned off.<p>
  173. * When the LBS of n is 1, emphasized mode is turned on.<p>
  174. * <p>
  175. * Format <p>
  176. * ASCII : ESC E n
  177. * HEX : 0x1B 0x45 n
  178. * Decimal : 27 69 n
  179. * <p>
  180. * Range 0 <= n <= 255 <p>
  181. * Default n = 0 <p>
  182. */
  183. public static final byte[] EMPHASIZED_OFF = {ESC, 0x45, 0x00};
  184. public static final byte[] EMPHASIZED_ON = {ESC, 0x45, 0xF};
  185. /**
  186. * Select character font
  187. * <p>
  188. * Select character fonts.<p>
  189. * <p>
  190. * Format <p>
  191. * ASCII : ESC M n
  192. * HEX : 0x1B 0x4D n
  193. * Decimal : 27 77 n
  194. * <p>
  195. * Range 0 <= n <= 2 or 48 <= n <= 50 <p>
  196. * Default n = 0 <p>
  197. * font A : 0 or 48 <p>
  198. * font B : 1 or 49 <p>
  199. * font C : 2 or 50 <p>
  200. * Notes: <p>
  201. * 1. Some printers do not have font C , See the ESC/POS Application Programming Guide
  202. * (ESC/POS APG ).<p>
  203. * 2. With the TM-U220, the range of n is n = 0,1,48 and 49. The default value is 1.<p>
  204. */
  205. public static final byte[] FONT_A = {ESC, 0x4D, 0x00};
  206. public static final byte[] FONT_B = {ESC, 0x4D, 0x01};
  207. public static final byte[] FONT_C = {ESC, 0x4D, 0x02};
  208. /**
  209. * Select CN font
  210. * <p>
  211. * Select CN fonts.<p>
  212. * <p>
  213. * <p>
  214. * Format <p>
  215. * ASCII : FS ! n
  216. * HEX : 0x1C 0x21 n
  217. * Decimal : 28 33 n
  218. * <p>
  219. * Range 0 <= n <= 1 <p>
  220. * Default n = 0 <p>
  221. * font A : 0 <p>
  222. * font B : 1 <p>
  223. */
  224. public static final byte[] FONT_CN_A = {FS, 0x21, 0x00};
  225. public static final byte[] FONT_CN_B = {FS, 0x21, 0x01};
  226. /**
  227. * Select print modes
  228. * <p>
  229. * Selects the character font and styles (emphasize ,double-height,double-width,and
  230. * underline) together.<p>
  231. * <p>
  232. * Format <p>
  233. * ASCII : ESC ! n
  234. * HEX : 0x1B 0x21 n
  235. * Decimal : 27 33 n
  236. * <p>
  237. * Range 0 <= n <= 255 <p>
  238. * Default n = 0 <p>
  239. * <p>
  240. */
  241. public static final byte[] FONT_DOUBLE_HEIGHT_OFF = {ESC, 0x21, 0x00};
  242. public static final byte[] FONT_DOUBLE_HEIGHT_ON = {ESC, 0x21, 0xF};
  243. public static final byte[] FONT_DOUBLE_HEIGHT_WIDTH_ON = {ESC, 0x21, 0x38};
  244. public static final byte[] HORIZONTAL_TAB = {ESC, 0x2C, 0x14, 0x1C, 0x00};
  245. public static final byte[] HT_NEXT = {HT};
  246. public static final byte[] LINE_HEIGHT_NORMAL = {ESC, 0x32};
  247. public static final byte[] LINE_HEIGHT_N = {ESC, 0x33, 0x08};
  248. /**
  249. * Generate pulse
  250. * <p>
  251. * Outputs the pulse specified by t1 and t2 to connector pin m to open the chash drawer,
  252. * as follows.<p>
  253. * <p>
  254. * Format <p>
  255. * ASCII : ESC p m t1 t2
  256. * HEX : 0x1B 0x70 m t1 t2
  257. * Decimal : 27 112 m t1 t2
  258. * <p>
  259. * Range : <br>
  260. * m = 0,1,48,49 <br>
  261. * 0 <= t1 <= 255 <br>
  262. * 0 <= t2 <= 255 <br>
  263. * <p>
  264. * t1 specifies the pulse ON time as [t1 x 2ms]
  265. * t1 specifies the pulse OFF time as [t2 x 2ms]
  266. * <p>
  267. * Note : <br>
  268. * some printer , if t2 < 50 ,t2 should be 50.<br>
  269. * some printer , if t1 < 50 ,t1 should be 50. If t2 < 50 , t2 should be 50<br>
  270. */
  271. public static final byte[] GENERATE_PULSE = {ESC, 0x70, 0x00, 0x3C, 0x78};
  272. /**
  273. * Select printing color
  274. * <p>
  275. * Select the printing color specified by n .<br>
  276. * When the n is 0,48 , color 1 is selected.<br>
  277. * When the n is 1,49 , color 2 is selected.<br>
  278. * <p>
  279. * Format <br>
  280. * ASCII : ESC r n <br>
  281. * HEX : 0x1B 0x72 n <br>
  282. * Decimal : 27 114 n <br>
  283. * <p>
  284. * Range <br>
  285. * n = 0,1,48,49 <br>
  286. * <p>
  287. * Default <br>
  288. * n = 0 <br>
  289. * <p>
  290. * Note: <br>
  291. * Some printer , it is recommended to obtain the ESC/POS Application programming
  292. * Guide (ESC/POS APG), which describes the recommended operation for 2 color printing
  293. * control;
  294. */
  295. public static final byte[] COLOR_SET_1 = {ESC, 0x72, 0x00};
  296. public static final byte[] COLOR_SET_2 = {ESC, 0x72, 0x01};
  297. /**
  298. * Select character code table
  299. * <p>
  300. * Selects a page n from the character code table .
  301. * <p>
  302. * Format <br>
  303. * ASCII : ESC t n
  304. * HEX : 0x1B 0x74 n
  305. * Decimal : 27 116 n
  306. * <p>
  307. * Range : <br>
  308. * Except for Thai model : 0 <= n <= 5 ,16 <= n <= 19 ,0 = 254,255 <br>
  309. * For Thai model : 0 <= n <= 5 ,16 <= n <= 26 ,0 = 254,255 <p>
  310. * Default :<br>
  311. * Except for Thai model : n = 0 <br>
  312. * For Thai model : n = 20 <br>
  313. * <p>
  314. *
  315. * @see CodePage
  316. * @see #CODE_PAGE_PC437
  317. * @see #CODE_PAGE_KATAKANA
  318. * @see #CODE_PAGE_PC850
  319. * @see #CODE_PAGE_PC860
  320. * @see #CODE_PAGE_PC863
  321. * @see #CODE_PAGE_PC865
  322. * @see #CODE_PAGE_WPC1252
  323. * @see #CODE_PAGE_PC866
  324. * @see #CODE_PAGE_PC852
  325. * @see #CODE_PAGE_PC858
  326. */
  327. public static final byte[] SELECT_CODE_TAB = {ESC, 0x74, CODE_PAGE_WPC1252};
  328. /**
  329. * Turn white/black reverse printing mode on/off
  330. * <p>
  331. * Turns white/black reverse printing mode on or off.<p>
  332. * When the LBS of n is 0, white/black reverse mode is turned off.<p>
  333. * When the LBS of n is 1, white/black reverse mode is turned on.<p>
  334. * <p>
  335. * Format <p>
  336. * ASCII : GS B n
  337. * HEX : 0x1D 0x45 n
  338. * Decimal : 29 66 n
  339. * <p>
  340. * Range 0 <= n <= 255 <p>
  341. * Default n = 0 <p>
  342. */
  343. public static final byte[] PRINTING_MODE_OFF = {GS, 0x42, 0x00};
  344. public static final byte[] PRINTING_MODE_ON = {GS, 0x42, (byte) 0x80};
  345. /**
  346. * Select bar code height
  347. * <p>
  348. * Selects the height of the bar code as n dots.<p>
  349. * <p>
  350. * Format <br>
  351. * ASCII : GS h n <br>
  352. * HEX : 0x1D 0x68 n <br>
  353. * Decimal : 29 104 n <br>
  354. * <p>
  355. * Range <br>
  356. * 0 <= n <= 255
  357. * <p>
  358. * Default <br>
  359. * n = 0 <p>
  360. */
  361. public static final byte[] BARCODE_HEIGHT = {GS, 0x68, (byte) 0xA2};
  362. /**
  363. * 设置字体大小
  364. * @param widthMultiplier 宽度倍数(1-8)
  365. * @param heightMultiplier 高度倍数(1-8)
  366. * @return 字体大小设置指令
  367. */
  368. public static byte[] setFontSize(int widthMultiplier, int heightMultiplier) {
  369. // 验证参数范围
  370. if (widthMultiplier < 1 || widthMultiplier > 8 ||
  371. heightMultiplier < 1 || heightMultiplier > 8) {
  372. throw new IllegalArgumentException("倍数范围必须是1-8");
  373. }
  374. // 计算参数n
  375. int n = ((heightMultiplier - 1) << 4) | (widthMultiplier - 1);
  376. // 返回指令字节数组
  377. return new byte[]{GS, EXCLAMATION, (byte) n};
  378. }
  379. /**
  380. * 初始化打印机
  381. *
  382. * @return bytes for this command
  383. * @see #RESET
  384. */
  385. public static byte[] initPrinter() {
  386. return RESET;
  387. }
  388. /**
  389. * 左对齐
  390. * ESC a n
  391. *
  392. * @return bytes for this command
  393. */
  394. public static byte[] alignLeft() {
  395. return ALIGN_LEFT;
  396. }
  397. /**
  398. * 居中对齐
  399. * ESC a n
  400. *
  401. * @return bytes for this command
  402. */
  403. public static byte[] alignCenter() {
  404. return ALIGN_CENTER;
  405. }
  406. /**
  407. * 右对齐
  408. * ESC a n
  409. *
  410. * @return bytes for this command
  411. */
  412. public static byte[] alignRight() {
  413. return ALIGN_RIGHT;
  414. }
  415. /**
  416. * 打印并换行
  417. *
  418. * @return bytes for this command
  419. * @see #LINE_FEED
  420. */
  421. public static byte[] printLineFeed() {
  422. return LINE_FEED;
  423. }
  424. /**
  425. * 打印并走纸n行
  426. * Prints the data in the print buffer and feeds n lines
  427. * ESC d n
  428. *
  429. * @param n lines
  430. * @return bytes for this command
  431. */
  432. public static byte[] printLineFeed(byte n) {
  433. LINE_FEED_N[2] = n;
  434. return LINE_FEED_N;
  435. }
  436. /**
  437. * 打印并反向走纸n行(不一定有效)
  438. * Prints the data in the print buffer and feeds n lines in the reserve direction
  439. * ESC e n
  440. *
  441. * @param n lines
  442. * @return bytes for this command
  443. * @see #LINE_FEED_REVERSE
  444. */
  445. public static byte[] printAndReverseFeedLines(byte n) {
  446. LINE_FEED_REVERSE[2] = n;
  447. return LINE_FEED_REVERSE;
  448. }
  449. /**
  450. * 加粗设置
  451. *
  452. * @param isEmphasized on / off ,true is on
  453. * @return bytes for this command
  454. * @see #EMPHASIZED_OFF
  455. * @see #EMPHASIZED_ON
  456. */
  457. public static byte[] emphasizedSetting(boolean isEmphasized) {
  458. return isEmphasized ? EMPHASIZED_ON : EMPHASIZED_OFF;
  459. }
  460. /**
  461. * Select Font A
  462. *
  463. * @return bytes for this command
  464. * @see #FONT_A
  465. */
  466. public static byte[] selectFontA() {
  467. return FONT_A;
  468. }
  469. /**
  470. * Select Font B
  471. *
  472. * @return bytes for this command
  473. * @see #FONT_B
  474. */
  475. public static byte[] selectFontB() {
  476. return FONT_B;
  477. }
  478. /**
  479. * Select Font C ( some printers don't have font C )
  480. *
  481. * @return bytes for this command
  482. * @see #FONT_C
  483. */
  484. public static byte[] selectFontC() {
  485. return FONT_C;
  486. }
  487. /**
  488. * Select CN Font A
  489. * FS ! n
  490. *
  491. * @return bytes for this command
  492. * @see #FONT_CN_A
  493. */
  494. public static byte[] selectCNFontA() {
  495. return FONT_CN_A;
  496. }
  497. /**
  498. * Select CN Font B
  499. * FS ! n
  500. *
  501. * @return bytes for this command
  502. * @see #FONT_CN_B
  503. */
  504. public static byte[] selectCNFontB() {
  505. return FONT_CN_B;
  506. }
  507. /**
  508. * 关闭双倍字高 chart
  509. * ESC ! n
  510. *
  511. * @return bytes for this command
  512. * @see #FONT_DOUBLE_HEIGHT_OFF
  513. */
  514. public static byte[] doubleHeightWidthOff() {
  515. return FONT_DOUBLE_HEIGHT_OFF;
  516. }
  517. /**
  518. * 双倍字高(仅英文字体有效)
  519. * ESC ! n
  520. *
  521. * @return bytes for this command
  522. * @see #FONT_DOUBLE_HEIGHT_ON
  523. */
  524. public static byte[] doubleHeightOn() {
  525. return FONT_DOUBLE_HEIGHT_ON;
  526. }
  527. /**
  528. * 双倍字体高宽(仅英文字体有效)
  529. * ESC ! n
  530. *
  531. * @return bytes for this command
  532. * @see #FONT_DOUBLE_HEIGHT_ON
  533. */
  534. public static byte[] doubleHeightWidthOn() {
  535. return FONT_DOUBLE_HEIGHT_WIDTH_ON;
  536. }
  537. public static byte[] printHorizontalTab() {
  538. return HORIZONTAL_TAB;
  539. }
  540. public static byte[] printHTNext() {
  541. return HT_NEXT;
  542. }
  543. public static byte[] printLineNormalHeight() {
  544. return LINE_HEIGHT_NORMAL;
  545. }
  546. public static byte[] printLineHeight(byte height) {
  547. LINE_HEIGHT_N[2] = height;
  548. return LINE_HEIGHT_N;
  549. }
  550. /**
  551. * 弹开纸箱
  552. * Drawer kick-out connector pin 2
  553. * ESC p m t1 t2
  554. *
  555. * @return bytes for this command
  556. */
  557. public static byte[] drawerKick() {
  558. return GENERATE_PULSE;
  559. }
  560. /**
  561. * 选择打印颜色1(不一定有效)
  562. * ESC r n
  563. *
  564. * @return bytes for this command
  565. */
  566. public static byte[] selectColor1() {
  567. return COLOR_SET_1;
  568. }
  569. /**
  570. * 选择打印颜色2(不一定有效)
  571. * ESC r n
  572. *
  573. * @return bytes for this command
  574. */
  575. public static byte[] selectColor2() {
  576. return COLOR_SET_2;
  577. }
  578. /**
  579. * Select character code table
  580. * ESC t n
  581. *
  582. * @param cp CODE_PAGE_WPC1252
  583. * @return bytes for this command
  584. * @see CodePage
  585. */
  586. public static byte[] selectCodeTab(@CodePage byte cp) {
  587. SELECT_CODE_TAB[2] = cp;
  588. return SELECT_CODE_TAB;
  589. }
  590. /**
  591. * select bar code height
  592. * Select the height of the bar code as n dots
  593. * default dots = 162
  594. * <p>
  595. * GS h n
  596. *
  597. * @param dots ( height of the bar code )
  598. * @return bytes for this command
  599. */
  600. public static byte[] barcode_height(byte dots) {
  601. BARCODE_HEIGHT[2] = dots;
  602. return BARCODE_HEIGHT;
  603. }
  604. /**
  605. * select font hri
  606. * Selects a font for the Human Readable Interpretation (HRI) characters when printing a barcode, using n as follows:
  607. *
  608. * @param n Font
  609. * 0, 48 Font A
  610. * 1, 49 Font B
  611. * @return bytes for this command
  612. */
  613. public static byte[] select_font_hri(byte n) {
  614. byte[] result = new byte[3];
  615. result[0] = GS;
  616. result[1] = 102;
  617. result[2] = n;
  618. return result;
  619. }
  620. /**
  621. * select position_hri
  622. * Selects the print position of Human Readable Interpretation (HRI) characters when printing a barcode, using n as follows:
  623. *
  624. * @param n Print position
  625. * 0, 48 Not printed
  626. * 1, 49 Above the barcode
  627. * 2, 50 Below the barcode
  628. * 3, 51 Both above and below the barcode
  629. * @return bytes for this command
  630. */
  631. public static byte[] select_position_hri(byte n) {
  632. byte[] result = new byte[3];
  633. result[0] = GS;
  634. result[1] = 72;
  635. result[2] = n;
  636. return result;
  637. }
  638. /**
  639. * Set horizontal tab positions
  640. *
  641. * @param col ( coulumn )
  642. * @return bytes for this command
  643. */
  644. public static byte[] set_HT_position(byte col) {
  645. byte[] result = new byte[4];
  646. result[0] = ESC;
  647. result[1] = 68;
  648. result[2] = col;
  649. result[3] = 0;
  650. return result;
  651. }
  652. /**
  653. * 字体变大为标准的n倍
  654. *
  655. * @param num 倍数
  656. * @return bytes for this command
  657. */
  658. public static byte[] fontSizeSetBig(int num) {
  659. byte realSize = 0;
  660. switch (num) {
  661. case 0: // 宽度|高度
  662. realSize = 0; // 0000|0000
  663. break;
  664. case 1:
  665. realSize = 13; // 0001|0001
  666. break;
  667. case 2:
  668. realSize = 34; // 0010|0010
  669. break;
  670. case 3:
  671. realSize = 51; // 0011|0011
  672. break;
  673. case 4:
  674. realSize = 68; // 0100|0100
  675. break;
  676. case 5:
  677. realSize = 85; // 0101|0101
  678. break;
  679. case 6:
  680. realSize = 102; // 0110|0110
  681. break;
  682. case 7:
  683. realSize = 119; // 0111|0111
  684. break;
  685. }
  686. byte[] result = new byte[3];
  687. result[0] = GS;
  688. result[1] = 33;
  689. result[2] = realSize;
  690. return result;
  691. }
  692. /**
  693. * 字体变大为标准的n倍
  694. *
  695. * @param num 倍数
  696. * @return bytes for this command
  697. */
  698. public static byte[] fontHeightSizeSetBig(int num) {
  699. byte realSize = 0;
  700. switch (num) {
  701. case 0: // 宽度|高度
  702. realSize = 0; // 0000|0000
  703. break;
  704. case 1:
  705. realSize = 1; // 0000|0001
  706. break;
  707. case 2:
  708. realSize = 2; // 0000|0010
  709. break;
  710. case 3:
  711. realSize = 3; // 0000|0011
  712. break;
  713. case 4:
  714. realSize = 4; // 0000|0100
  715. break;
  716. case 5:
  717. realSize = 5; // 0000|0101
  718. break;
  719. case 6:
  720. realSize = 6; // 0000|0110
  721. break;
  722. case 7:
  723. realSize = 7; // 0000|0111
  724. break;
  725. }
  726. byte[] result = new byte[3];
  727. result[0] = GS;
  728. result[1] = 33;
  729. result[2] = realSize;
  730. return result;
  731. }
  732. // ------------------------条形码---------------------------
  733. // 设置条码的位置--与设置文本对齐方式相同
  734. // 设置条码的宽度GS w n
  735. public static byte[] setBarCodeWith(int width) {
  736. byte[] result = new byte[3];
  737. result[0] = GS;// 0x1D
  738. result[1] = 0x77;// 'W'
  739. result[2] = (byte) width;
  740. return result;
  741. }
  742. // 设置条码的高度GS h n
  743. public static byte[] setBarCodeHeight(int height) {
  744. byte[] result = new byte[3];
  745. result[0] = GS;
  746. result[1] = 0x68;
  747. result[2] = (byte) height /*0xA2*/;
  748. return result;
  749. }
  750. // 条码注释打印在条码位置GS H n
  751. // 打印条码时, 为HRI字符选择打印位置(HRI 是对条码内容注释的字符)
  752. // 0, 48 不打印;1, 49 在条码上方;2, 50 在条码下方;3, 51 在条码上方及下方
  753. public static byte[] setHRILocation(int loc) {
  754. byte[] result = new byte[3];
  755. result[0] = GS;
  756. result[1] = 0x48;
  757. result[2] = (byte) loc;
  758. return result;
  759. }
  760. /**
  761. * 选定条形码系统(m值)并打印条码
  762. * <p>
  763. * 【GS k m d1...dk NUL】 该命令在这种格式下以 NUL 结束 0 UPC-A 11 ≤ k ≤ 12 48 ≤ d ≤ 57 1
  764. * UPC-E 11 ≤ k ≤ 12 48 ≤ d ≤ 57 2 JAN13 (EAN13) 12 ≤ k ≤ 13 48 ≤ d ≤ 57 3 JAN 8
  765. * (EAN8) 7 ≤ k ≤ 8 48 ≤ d ≤ 57 4 CODE39 1 ≤ k 48 ≤ d ≤ 57, 65 ≤ d ≤ 90, 32, 36,
  766. * 37, 43, 45, 46, 47 5 ITF 1 ≤ k (偶数) 48 ≤ d ≤ 57 6 CODABAR 1 ≤ k 48 ≤ d ≤ 57,
  767. * 65 ≤ d ≤ 68, 36, 43, 45, 46, 47, 58
  768. * <p>
  769. * 【GS k m n d1...dn】 n用来指示条码数据的个数, 打印机将其后边 n 字节数据作为条码数据处理
  770. * 65 UPC-A 11 ≤ n ≤ 12
  771. * 48 ≤ d ≤ 57
  772. * 66 UPC-E 11 ≤ n ≤ 12 48 ≤ d ≤ 57
  773. * 67 JAN13 (EAN13) 12 ≤ n ≤ 13 48 ≤ d ≤ 57
  774. * 68 JAN 8 (EAN8) 7 ≤ n ≤ 8 48 ≤ d ≤ 57
  775. * 69 CODE39 1 ≤ n ≤ 255 48 ≤ d ≤ 57, 65 ≤ d ≤ 90, 32, 36, 37, 43, 45, 46, 47
  776. * 70 ITF 1 ≤ n ≤ 255(偶数) 48 ≤ d ≤ 57
  777. * 71 CODABAR 1 ≤ n ≤ 255 48 ≤ d ≤ 57, 65 ≤ d ≤ 68, 36, 43, 45, 46, 47, 58
  778. * 72 CODE93 1 ≤ n ≤ 255 0 ≤ d ≤ 127
  779. * [实例] 打印 GS k 72 7 67 111 100 101 13 57 51
  780. * 73 CODE128 2 ≤ n ≤ 255 0 ≤ d ≤ 127
  781. * [实例] 打印"No. 123456"的实例数据,首先用CODE B打印"No."
  782. * 然后用CODE C 打印下列数字。GS k 73 10 123 66 78 111 46 123 67 12 34 56
  783. */
  784. public static byte[] printBarCode(int m, byte[] dk) {
  785. byte[] result = new byte[3 + dk.length + 1];
  786. result[0] = GS;
  787. result[1] = 0x6B;
  788. result[2] = (byte) m;// 选定条形码系统
  789. for (int i = 0; i < dk.length; i++) {
  790. result[3 + i] = dk[i];
  791. }
  792. result[result.length - 1] = 0;
  793. return result;
  794. }
  795. public static byte[] printBarCode(int m, int n, byte[] dn) {
  796. byte[] result = new byte[4 + n];
  797. result[0] = GS;
  798. result[1] = 0x6B;
  799. result[2] = (byte) m;// 选定条形码系统
  800. result[3] = (byte) n;// 条码数据的个数
  801. for (int i = 0; i < n; i++) {
  802. result[4 + i] = dn[i];
  803. }
  804. return result;
  805. }
  806. public static byte[] printBarCodeGP(int m, int n, String dn) {
  807. n = 8;
  808. byte[] result = new byte[6 + n];
  809. result[0] = GS;
  810. result[1] = 0x6B;
  811. result[2] = (byte) m;// 选定条形码系统
  812. result[3] = (byte) (n + 2);// 条码数据的个数
  813. result[4] = (byte) 123;
  814. result[5] = (byte) 67;
  815. for (int i = 0; i < n; i++) {
  816. result[6 + i] = (byte) (10 + i);
  817. }
  818. return result;
  819. }
  820. /**
  821. * print bar code
  822. *
  823. * @param barcode_typ ( Barcode.CODE39, Barcode.EAN8 ,...)
  824. * @param barcode2print value
  825. * @return bytes for this command
  826. */
  827. public static byte[] print_bar_code(byte barcode_typ, String barcode2print) {
  828. byte[] barcodeBytes = barcode2print.getBytes();
  829. byte[] result = new byte[3 + barcodeBytes.length + 1];
  830. result[0] = GS;
  831. result[1] = 107;
  832. result[2] = barcode_typ;
  833. int idx = 3;
  834. for (byte b : barcodeBytes) {
  835. result[idx] = b;
  836. idx++;
  837. }
  838. result[idx] = 0;
  839. return result;
  840. }
  841. /**
  842. * print bar code
  843. *
  844. * @param barcode_typ(Barcode.CODE39,Barcode.EAN8,...)
  845. * @param barcode2print
  846. * @return bytes for this command
  847. */
  848. public static byte[] print_bar_code128(byte barcode_typ, String barcode2print) {
  849. byte[] barcodebytes = barcode2print.getBytes();
  850. byte[] result = new byte[4 + barcodebytes.length];
  851. result[0] = GS;
  852. result[1] = 107;
  853. result[2] = barcode_typ;
  854. result[3] = (byte) barcodebytes.length;
  855. int idx = 4;
  856. for (int i = 0; i < barcodebytes.length; i++) {
  857. result[idx] = barcodebytes[i];
  858. idx++;
  859. }
  860. return result;
  861. }
  862. /**
  863. * 进纸切割
  864. * Feeds paper to ( cutting position + n x vertical motion unit )
  865. * and executes a full cut ( cuts the paper completely )
  866. *
  867. * @return bytes for this command
  868. */
  869. public static byte[] feedPaperCut() {
  870. byte[] result = new byte[4];
  871. result[0] = GS;
  872. result[1] = 86;
  873. result[2] = 65;
  874. result[3] = 0;
  875. return result;
  876. }
  877. /**
  878. * 进纸切割(留部分)
  879. * Feeds paper to ( cutting position + n x vertical motion unit )
  880. * and executes a partial cut ( one point left uncut )
  881. *
  882. * @return bytes for this command
  883. */
  884. public static byte[] feedPaperCutPartial() {
  885. byte[] result = new byte[4];
  886. result[0] = GS;
  887. result[1] = 86;
  888. result[2] = 66;
  889. result[3] = 0;
  890. return result;
  891. }
  892. /**
  893. * 合并byte数组
  894. *
  895. * @param byteArray byte数组
  896. * @return 一个byte数组
  897. */
  898. public static byte[] mergerByteArray(byte[]... byteArray) {
  899. int length = 0;
  900. for (byte[] item : byteArray) {
  901. length += item.length;
  902. }
  903. byte[] result = new byte[length];
  904. int index = 0;
  905. for (byte[] item : byteArray) {
  906. for (byte b : item) {
  907. result[index] = b;
  908. index++;
  909. }
  910. }
  911. return result;
  912. }
  913. /**
  914. * 2进制转成16进制
  915. *
  916. * @param binaryStr 2进制串
  917. * @return 16进制串
  918. */
  919. public static String binaryStrToHexString(String binaryStr) {
  920. String hex = "";
  921. String f4 = binaryStr.substring(0, 4);
  922. String b4 = binaryStr.substring(4, 8);
  923. for (int i = 0; i < binaryArray.length; i++) {
  924. if (f4.equals(binaryArray[i]))
  925. hex += hexStr.substring(i, i + 1);
  926. }
  927. for (int i = 0; i < binaryArray.length; i++) {
  928. if (b4.equals(binaryArray[i]))
  929. hex += hexStr.substring(i, i + 1);
  930. }
  931. return hex;
  932. }
  933. /**
  934. * 16进制指令list转换为byte[]指令
  935. *
  936. * @param list 指令集
  937. * @return byte[]指令
  938. */
  939. public static byte[] hexListToByte(List<String> list) {
  940. ArrayList<byte[]> commandList = new ArrayList<>();
  941. for (String hexStr : list) {
  942. commandList.add(hexStringToBytes(hexStr));
  943. }
  944. int len = 0;
  945. for (byte[] srcArray : commandList) {
  946. len += srcArray.length;
  947. }
  948. byte[] destArray = new byte[len];
  949. int destLen = 0;
  950. for (byte[] srcArray : commandList) {
  951. System.arraycopy(srcArray, 0, destArray, destLen, srcArray.length);
  952. destLen += srcArray.length;
  953. }
  954. return destArray;
  955. }
  956. /**
  957. * 16进制串转byte数组
  958. *
  959. * @param hexString 16进制串
  960. * @return byte数组
  961. */
  962. public static byte[] hexStringToBytes(String hexString) {
  963. if (hexString == null || hexString.equals("")) {
  964. return null;
  965. }
  966. hexString = hexString.toUpperCase();
  967. int length = hexString.length() / 2;
  968. char[] hexChars = hexString.toCharArray();
  969. byte[] d = new byte[length];
  970. for (int i = 0; i < length; i++) {
  971. int pos = i * 2;
  972. d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
  973. }
  974. return d;
  975. }
  976. /**
  977. * 16进制char 转 byte
  978. *
  979. * @param c char
  980. * @return byte
  981. */
  982. private static byte charToByte(char c) {
  983. return (byte) hexStr.indexOf(c);
  984. }
  985. public static void main(String[] args) {
  986. }
  987. }