Advertisement
Guest User

Untitled

a guest
May 19th, 2017
453
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.59 KB | None | 0 0
  1. package zj.com.command.sdk;
  2.  
  3. import java.io.UnsupportedEncodingException;
  4.  
  5. import zj.com.customize.sdk.Other;
  6.  
  7. public class PrinterCommand {
  8.  
  9. /**
  10. * 打印机初始化
  11. * @return
  12. */
  13. public static byte[] POS_Set_PrtInit(){
  14.  
  15. byte[] data = Other.byteArraysToBytes(new byte[][] {
  16. Command.ESC_Init});
  17.  
  18. return data;
  19. }
  20.  
  21. /**
  22. * 打印并换行
  23. * @return
  24. */
  25. public static byte[] POS_Set_LF(){
  26. byte[] data = Other.byteArraysToBytes(new byte[][] {
  27. Command.LF});
  28.  
  29. return data;
  30. }
  31.  
  32. /**
  33. * 打印并走纸 (0~255)
  34. * @param feed
  35. * @return
  36. */
  37. public static byte[] POS_Set_PrtAndFeedPaper(int feed){
  38. if(feed > 255 | feed < 0)
  39. return null;
  40.  
  41. Command.ESC_J[2] = (byte)feed;
  42.  
  43. byte[] data = Other.byteArraysToBytes(new byte[][] {
  44. Command.ESC_J});
  45.  
  46. return data;
  47. }
  48.  
  49. /**
  50. * 打印自检页
  51. * @return
  52. */
  53. public static byte[] POS_Set_PrtSelfTest(){
  54.  
  55. byte[] data = Other.byteArraysToBytes(new byte[][]{
  56. Command.US_vt_eot
  57. });
  58. return data;
  59. }
  60.  
  61. /**
  62. * 蜂鸣指令
  63. * @param m 蜂鸣次数
  64. * @param t 每次蜂鸣的时间
  65. * @return
  66. */
  67. public static byte[] POS_Set_Beep(int m, int t){
  68.  
  69. if((m<1 || m>9) | (t<1 || t>9))
  70. return null;
  71.  
  72. Command.ESC_B_m_n[2] = (byte)m;
  73. Command.ESC_B_m_n[3] = (byte)t;
  74.  
  75. byte[] data = Other.byteArraysToBytes(new byte[][]{
  76. Command.ESC_B_m_n
  77. });
  78. return data;
  79. }
  80.  
  81. /**
  82. * 切刀指令(走纸到切刀位置并切纸)
  83. * @param cut 0~255
  84. * @return
  85. */
  86. public static byte[] POS_Set_Cut(int cut){
  87. if(cut>255 | cut < 0)
  88. return null;
  89.  
  90. Command.GS_V_m_n[3] = (byte)cut;
  91. byte[] data = Other.byteArraysToBytes(new byte[][]{
  92. Command.GS_V_m_n
  93. });
  94. return data;
  95. }
  96.  
  97. /**
  98. * 钱箱指令
  99. * @param nMode
  100. * @param nTime1
  101. * @param nTime2
  102. * @return
  103. */
  104. public static byte[] POS_Set_Cashbox(int nMode, int nTime1, int nTime2){
  105.  
  106. if((nMode<0 || nMode>1) | nTime1<0 | nTime1 >255 | nTime2 < 0 | nTime2 > 255)
  107. return null;
  108. Command.ESC_p[2] = (byte)nMode;
  109. Command.ESC_p[3] = (byte)nTime1;
  110. Command.ESC_p[4] = (byte)nTime2;
  111.  
  112. byte[] data = Other.byteArraysToBytes(new byte[][]{
  113. Command.ESC_p
  114. });
  115. return data;
  116. }
  117.  
  118. /**
  119. * 设置绝对打印位置
  120. * @param absolute
  121. * @return
  122. */
  123. public static byte[] POS_Set_Absolute(int absolute){
  124. if(absolute >65535 | absolute <0)
  125. return null;
  126.  
  127. Command.ESC_Relative[2] = (byte)(absolute%0x100);
  128. Command.ESC_Relative[3] = (byte)(absolute/0x100);
  129.  
  130. byte[] data = Other.byteArraysToBytes(new byte[][]{
  131. Command.ESC_Relative
  132. });
  133. return data;
  134. }
  135.  
  136. /**
  137. * 设置相对打印位置
  138. * @param relative
  139. * @return
  140. */
  141. public static byte[] POS_Set_Relative(int relative){
  142. if(relative<0 | relative>65535)
  143. return null;
  144.  
  145. Command.ESC_Absolute[2] = (byte)(relative%0x100);
  146. Command.ESC_Absolute[3] = (byte)(relative/0x100);
  147.  
  148. byte[] data = Other.byteArraysToBytes(new byte[][]{
  149. Command.ESC_Absolute
  150. });
  151. return data;
  152. }
  153.  
  154. /**
  155. * 设置左边距
  156. * @param left
  157. * @return
  158. */
  159. public static byte[] POS_Set_LeftSP(int left){
  160. if(left > 255 | left < 0)
  161. return null;
  162.  
  163. Command.GS_LeftSp[2] = (byte)(left%100);
  164. Command.GS_LeftSp[3] = (byte)(left/100);
  165.  
  166. byte[] data = Other.byteArraysToBytes(new byte[][]{
  167. Command.GS_LeftSp
  168. });
  169. return data;
  170. }
  171.  
  172. /**
  173. * 设置对齐模式
  174. * @param align
  175. * @return
  176. */
  177. public static byte[] POS_S_Align(int align) {
  178. if ((align < 0 || align > 2) | (align <48 || align >50))
  179. return null;
  180.  
  181. byte[] data = Command.ESC_Align;
  182. data[2] = (byte) align;
  183. return data;
  184. }
  185.  
  186. /**
  187. * 设置打印区域宽度
  188. * @param width
  189. * @return
  190. */
  191. public static byte[] POS_Set_PrintWidth(int width){
  192. if(width<0 | width>255)
  193. return null;
  194.  
  195. Command.GS_W[2] = (byte)(width%100);
  196. Command.GS_W[3] = (byte)(width/100);
  197.  
  198. byte[] data = Other.byteArraysToBytes(new byte[][]{
  199. Command.GS_W
  200. });
  201. return data;
  202. }
  203.  
  204. /**
  205. * 设置默认行间距
  206. * @return
  207. */
  208. public static byte[] POS_Set_DefLineSpace(){
  209.  
  210. byte[] data = Command.ESC_Two;
  211. return data;
  212. }
  213.  
  214. /**
  215. * 设置行间距
  216. * @param space
  217. * @return
  218. */
  219. public static byte[] POS_Set_LineSpace(int space){
  220. if(space<0 | space > 255)
  221. return null;
  222.  
  223. Command.ESC_Three[2] = (byte)space;
  224.  
  225. byte[] data = Other.byteArraysToBytes(new byte[][]{
  226. Command.ESC_Three
  227. });
  228. return data;
  229. }
  230.  
  231. /**
  232. * 选择字符代码页
  233. * @param page
  234. * @return
  235. */
  236. public static byte[] POS_Set_CodePage(int page){
  237. if(page > 255)
  238. return null;
  239.  
  240. Command.ESC_t[2] = (byte)page;
  241.  
  242. byte[] data = byteArraysToBytes(new byte[][]{
  243. Command.ESC_t
  244. });
  245.  
  246. return data;
  247. }
  248.  
  249. /**
  250. * 打印文本文档
  251. * @param pszString 要打印的字符串
  252. * @param encoding 打印字符对应编码
  253. * @param codepage 设置代码页(0--255)
  254. * @param nWidthTimes 倍宽(0--4)
  255. * @param nHeightTimes 倍高(0--4)
  256. * @param nFontType 字体类型(只对Ascii码有效)(0,1 48,49)
  257. */
  258. public static byte[] POS_Print_Text(String pszString, String encoding, int codepage,
  259. int nWidthTimes, int nHeightTimes, int nFontType) {
  260.  
  261. if (codepage <0 || codepage >255 || pszString == null || "".equals(pszString) || pszString.length() < 1) {
  262. return null;
  263. }
  264.  
  265. byte[] pbString = null;
  266. try {
  267. pbString = pszString.getBytes(encoding);
  268. } catch (UnsupportedEncodingException e) {
  269. return null;
  270. }
  271.  
  272. byte[] intToWidth = { 0x00, 0x10, 0x20, 0x30 };
  273. byte[] intToHeight = { 0x00, 0x01, 0x02, 0x03 };
  274. Command.GS_ExclamationMark[2] = (byte) (intToWidth[nWidthTimes] + intToHeight[nHeightTimes]);
  275.  
  276. Command.ESC_t[2] = (byte)codepage;
  277.  
  278. Command.ESC_M[2] = (byte)nFontType;
  279.  
  280. if(codepage == 0){
  281. byte[] data = Other.byteArraysToBytes(new byte[][] {
  282. Command.GS_ExclamationMark, Command.ESC_t, Command.FS_and, Command.ESC_M, pbString });
  283.  
  284. return data;
  285. }else{
  286. byte[] data = Other.byteArraysToBytes(new byte[][] {
  287. Command.GS_ExclamationMark, Command.ESC_t, Command.FS_dot, Command.ESC_M, pbString });
  288.  
  289. return data;
  290. }
  291. }
  292.  
  293. /**
  294. * 加粗指令(最低位为1有效)
  295. * @param bold
  296. * @return
  297. */
  298. public static byte[] POS_Set_Bold(int bold){
  299.  
  300. Command.ESC_E[2] = (byte)bold;
  301. Command.ESC_G[2] = (byte)bold;
  302.  
  303. byte[] data = Other.byteArraysToBytes(new byte[][]{
  304. Command.ESC_E, Command.ESC_G
  305. });
  306. return data;
  307. }
  308.  
  309. /**
  310. * 设置倒置打印模式(当最低位为1时有效)
  311. * @param brace
  312. * @return
  313. */
  314. public static byte[] POS_Set_LeftBrace(int brace){
  315.  
  316. Command.ESC_LeftBrace[2] = (byte)brace;
  317. byte[] data = Other.byteArraysToBytes(new byte[][]{
  318. Command.ESC_LeftBrace
  319. });
  320. return data;
  321. }
  322.  
  323. /**
  324. * 设置下划线
  325. * @param line
  326. * @return
  327. */
  328. public static byte[] POS_Set_UnderLine(int line){
  329.  
  330. if((line<0 || line>2))
  331. return null;
  332.  
  333. Command.ESC_Minus[2] = (byte)line;
  334. Command.FS_Minus[2] = (byte)line;
  335.  
  336. byte[] data = Other.byteArraysToBytes(new byte[][]{
  337. Command.ESC_Minus, Command.FS_Minus
  338. });
  339. return data;
  340. }
  341.  
  342. /**
  343. * 选择字体大小(倍高倍宽)
  344. * @param size
  345. * @return
  346. */
  347. public static byte[] POS_Set_FontSize(int size1, int size2){
  348. if(size1<0 | size1>7 | size2<0 | size2>7)
  349. return null;
  350.  
  351. byte[] intToWidth = { 0x00, 0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70 };
  352. byte[] intToHeight = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
  353. Command.GS_ExclamationMark[2] = (byte) (intToWidth[size1] + intToHeight[size2]);
  354. byte[] data = Other.byteArraysToBytes(new byte[][]{
  355. Command.GS_ExclamationMark
  356. });
  357. return data;
  358. }
  359.  
  360. /**
  361. * 设置反显打印
  362. * @param inverse
  363. * @return
  364. */
  365. public static byte[] POS_Set_Inverse(int inverse){
  366.  
  367. Command.GS_B[2] = (byte)inverse;
  368.  
  369. byte[] data = Other.byteArraysToBytes(new byte[][]{
  370. Command.GS_B
  371. });
  372.  
  373. return data;
  374. }
  375.  
  376. /**
  377. * 设置旋转90度打印
  378. * @param rotate
  379. * @return
  380. */
  381. public static byte[] POS_Set_Rotate(int rotate){
  382. if(rotate<0 || rotate>1)
  383. return null;
  384. Command.ESC_V[2] = (byte)rotate;
  385. byte[] data = Other.byteArraysToBytes(new byte[][]{
  386. Command.ESC_V
  387. });
  388. return data;
  389. }
  390.  
  391. /**
  392. * 选择字体字型
  393. * @param font
  394. * @return
  395. */
  396. public static byte[] POS_Set_ChoseFont(int font){
  397. if(font > 1 | font < 0)
  398. return null;
  399.  
  400. Command.ESC_M[2] = (byte)font;
  401. byte[] data = Other.byteArraysToBytes(new byte[][]{
  402. Command.ESC_M
  403. });
  404. return data;
  405.  
  406. }
  407.  
  408. //***********************************以下函数为公开函数***********************************************************//
  409. /**
  410. * 二维码打印函数
  411. * @param str 打印二维码数据
  412. * @param nVersion 二维码类型
  413. * @param nErrorCorrectionLevel 纠错级别
  414. * @param nMagnification 放大倍数
  415. * @return
  416. */
  417. public static byte[] getBarCommand(String str, int nVersion, int nErrorCorrectionLevel,
  418. int nMagnification){
  419.  
  420. if(nVersion<0 | nVersion >19 | nErrorCorrectionLevel<0 | nErrorCorrectionLevel > 3
  421. | nMagnification < 1 | nMagnification > 8){
  422. return null;
  423. }
  424.  
  425. byte[] bCodeData = null;
  426. try
  427. {
  428. bCodeData = str.getBytes("GBK");
  429.  
  430. }
  431. catch (UnsupportedEncodingException e)
  432. {
  433. e.printStackTrace();
  434. return null;
  435. }
  436.  
  437. byte[] command = new byte[bCodeData.length + 7];
  438.  
  439. command[0] = 27;
  440. command[1] = 90;
  441. command[2] = ((byte)nVersion);
  442. command[3] = ((byte)nErrorCorrectionLevel);
  443. command[4] = ((byte)nMagnification);
  444. command[5] = (byte)(bCodeData.length & 0xff);
  445. command[6] = (byte)((bCodeData.length & 0xff00) >> 8);
  446. System.arraycopy(bCodeData, 0, command, 7, bCodeData.length);
  447.  
  448. return command;
  449. }
  450.  
  451. /**
  452. * 打印一维条码
  453. * @param str 打印条码字符
  454. * @param nType 条码类型(65~73)
  455. * @param nWidthX 条码宽度
  456. * @param nHeight 条码高度
  457. * @param nHriFontType HRI字型
  458. * @param nHriFontPosition HRI位置
  459. * @return
  460. */
  461. public static byte[] getCodeBarCommand(String str, int nType, int nWidthX, int nHeight,
  462. int nHriFontType, int nHriFontPosition){
  463.  
  464. if (nType < 0x41 | nType > 0x49 | nWidthX < 2 | nWidthX > 6
  465. | nHeight < 1 | nHeight > 255 | str.length() == 0)
  466. return null;
  467.  
  468. byte[] bCodeData = null;
  469. try
  470. {
  471. bCodeData = str.getBytes("GBK");
  472.  
  473. }
  474. catch (UnsupportedEncodingException e)
  475. {
  476. e.printStackTrace();
  477. return null;
  478. }
  479.  
  480. byte[] command = new byte[bCodeData.length + 16];
  481.  
  482. command[0] = 29;
  483. command[1] = 119;
  484. command[2] = ((byte)nWidthX);
  485. command[3] = 29;
  486. command[4] = 104;
  487. command[5] = ((byte)nHeight);
  488. command[6] = 29;
  489. command[7] = 102;
  490. command[8] = ((byte)(nHriFontType & 0x01));
  491. command[9] = 29;
  492. command[10] = 72;
  493. command[11] = ((byte)(nHriFontPosition & 0x03));
  494. command[12] = 29;
  495. command[13] = 107;
  496. command[14] = ((byte)nType);
  497. command[15] = (byte)(byte) bCodeData.length;
  498. System.arraycopy(bCodeData, 0, command, 16, bCodeData.length);
  499.  
  500.  
  501. return command;
  502. }
  503.  
  504. /**
  505. * 设置打印模式(选择字体(font:A font:B),加粗,字体倍高倍宽(最大4倍高宽))
  506. * @param str 打印的字符串
  507. * @param bold 加粗
  508. * @param font 选择字型
  509. * @param widthsize 倍宽
  510. * @param heigthsize 倍高
  511. * @return
  512. */
  513. public static byte[] POS_Set_Font(String str, int bold, int font, int widthsize, int heigthsize){
  514.  
  515. if(str.length() == 0 | widthsize<0 | widthsize >4 | heigthsize<0 | heigthsize>4
  516. | font<0 | font>1)
  517. return null;
  518.  
  519. byte[] strData = null;
  520. try
  521. {
  522. strData = str.getBytes("GBK");
  523. }
  524. catch (UnsupportedEncodingException e)
  525. {
  526. e.printStackTrace();
  527. return null;
  528. }
  529.  
  530. byte[] command = new byte[strData.length + 9];
  531.  
  532. byte[] intToWidth = { 0x00, 0x10, 0x20, 0x30 };//最大四倍宽
  533. byte[] intToHeight = { 0x00, 0x01, 0x02, 0x03 };//最大四倍高
  534.  
  535. command[0] = 27;
  536. command[1] = 69;
  537. command[2] = ((byte)bold);
  538. command[3] = 27;
  539. command[4] = 77;
  540. command[5] = ((byte)font);
  541. command[6] = 29;
  542. command[7] = 33;
  543. command[8] = (byte) (intToWidth[widthsize] + intToHeight[heigthsize]);
  544.  
  545. System.arraycopy(strData, 0, command, 9, strData.length);
  546. return command;
  547. }
  548. //**********************************************************************************************************//
  549.  
  550. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement