PrintLabelFruit.java 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. package com.printer.demo.utils;
  2. import android.content.Context;
  3. import android.util.Log;
  4. import com.printer.sdk.PrinterConstants;
  5. import com.printer.sdk.PrinterConstants.PAlign;
  6. import com.printer.sdk.PrinterConstants.PBarcodeType;
  7. import com.printer.sdk.PrinterConstants.TwoDarCodeType;
  8. import com.printer.sdk.PrinterInstance;
  9. import com.printer.sdk.exception.ParameterErrorException;
  10. import com.printer.sdk.exception.PrinterPortNullException;
  11. import com.printer.sdk.exception.WriteException;
  12. public class PrintLabelFruit {
  13. public synchronized static void doPrintTSPL(final PrinterInstance iPrinter, final Context mContext) {
  14. int left = PrefUtils.getInt(mContext, "leftmargin", 0);
  15. int top = PrefUtils.getInt(mContext, "topmargin", 0);
  16. int numbers = PrefUtils.getInt(mContext, "printnumbers", 1);
  17. int isBeep = PrefUtils.getInt(mContext, "isBeep", 0);
  18. int isOpenCash = PrefUtils.getInt(mContext, "isOpenCash", 0);
  19. Log.i("yxz", "左边距------------" + left);
  20. Log.i("yxz", "上边距------------" + top);
  21. Log.i("yxz", "打印份数------------" + numbers);
  22. try {
  23. // 设置标签纸大小
  24. iPrinter.pageSetupTSPL(PrinterConstants.SIZE_58mm, 56 * 8, 45 * 8);
  25. // 清除缓存区内容
  26. iPrinter.printText("CLS\r\n");
  27. // 设置标签的参考坐标原点
  28. if (left == 0 || top == 0) {
  29. // 不做设置,默认
  30. } else {
  31. iPrinter.sendStrToPrinterTSPL("REFERENCE " + left * 8 + "," + top * 8 + "\r\n");
  32. }
  33. // 横线上方区域
  34. // 横线上方区域左上方的文字((0,0)(40*8,7*8))
  35. iPrinter.drawTextTSPL(0, 0, 35 * 8, 7 * 8, PAlign.START, PAlign.START, true, 1, 1, null, "HanBu");
  36. // 横线上方区域左下方的文字 ((0,0)(40*8,7*8))
  37. iPrinter.drawTextTSPL(0, 0, 35 * 8, 7 * 8, PAlign.START, PAlign.END, true, 1, 1, null, "汉步");
  38. // 横线上方区域右侧的文字 ((100,0)(40*8,7*8))
  39. iPrinter.drawTextTSPL(100, 0, 35 * 8, 7 * 8, PAlign.CENTER, PAlign.CENTER, true, 1, 2, null,
  40. "热敏不干胶纸");
  41. // 横线 (0,7*8+2);
  42. iPrinter.drawLineTSPL(0, 7 * 8 + 2, 35 * 8, 5);
  43. // 横线下方区域
  44. // 左侧的二维码
  45. iPrinter.draw2DBarCodeTSPL(0, 7 * 8 + 15, TwoDarCodeType.QR, 1, 4, null, "热敏不干胶纸");
  46. // 右侧第一行汉字
  47. iPrinter.drawTextTSPL(12 * 8, 7 * 8 + 10, true, 2, 2, null, "SIZE:");
  48. // 右侧第二行汉字
  49. iPrinter.drawTextTSPL(12 * 8, 7 * 8 + 55, true, 2, 2, null, "40MM*30MM");
  50. // 二维码下方的一维条码
  51. iPrinter.drawBarCodeTSPL(0, 20 * 8 + 4, PBarcodeType.CODE128, 5 * 8, false, null, 1, 2,
  52. "123456789012");
  53. iPrinter.drawTextTSPL(0, 25 * 8, 40 * 8, 30 * 8, PAlign.CENTER, PAlign.CENTER, true, 1, 1, null,
  54. "123456789012");
  55. // // 判断是否响应钱箱
  56. // if (isOpenCash == 1) {
  57. // // 打印前打开钱箱
  58. // iPrinter.openCashBoxTSPL(1, 2);
  59. // Thread.sleep(3000);
  60. // // 打印
  61. // iPrinter.printTSPL(numbers, 1);
  62. // } else if (isBeep == 2) {
  63. // // 打印
  64. // iPrinter.printTSPL(numbers, 1);
  65. // // 打印后后打开钱箱
  66. // iPrinter.openCashBoxTSPL(1, 2);
  67. // } else {
  68. // // 打印
  69. // iPrinter.printTSPL(numbers, 1);
  70. // }
  71. // 判断是否响应蜂鸣器
  72. if (isBeep == 1) {
  73. // 打印前响
  74. iPrinter.beepTSPL(1, 1000);
  75. Thread.sleep(3000);
  76. // 打印
  77. iPrinter.printTSPL(numbers, 1);
  78. } else if (isBeep == 2) {
  79. // 打印
  80. iPrinter.printTSPL(numbers, 1);
  81. // 打印后响
  82. // Thread.sleep(3000);
  83. iPrinter.beepTSPL(1, 1000);
  84. } else {
  85. // 打印
  86. iPrinter.printTSPL(numbers, 1);
  87. }
  88. // 备份
  89. // // 设置标签纸大小
  90. // iPrinter.pageSetupTSPL(PrinterConstants.SIZE_58mm, 40 *
  91. // 8, 30 *
  92. // 8);
  93. // // 清楚缓存区内容
  94. // iPrinter.printText("CLS\r\n");
  95. // // 横线上方区域
  96. // // 横线上方区域左上方的文字((0,0)(40*8,7*8))
  97. // iPrinter.drawTextTSPL(0 + left, 0 + top, 40 * 8 + left, 7
  98. // * 8 +
  99. // top, PAlign.START, PAlign.START, true, 1, 1,
  100. // null, "汉 步");
  101. // // 横线上方区域左下方的文字 ((0,0)(40*8,7*8))
  102. // iPrinter.drawTextTSPL(0 + left, 0 + top, 40 * 8 + left, 7
  103. // * 8 +
  104. // top, PAlign.START, PAlign.END, true, 1, 1,
  105. // null, "HanBu");
  106. // // 横线上方区域右侧的文字 ((100,0)(40*8,7*8))
  107. // iPrinter.drawTextTSPL(100 + left, 0 + top, 40 * 8 + left,
  108. // 7 * 8 +
  109. // top, PAlign.CENTER, PAlign.CENTER, true,
  110. // 1, 2, null, "热敏不干胶纸");
  111. //
  112. // // 横线 (0,7*8+2);
  113. // iPrinter.drawLineTSPL(0 + left, 7 * 8 + 2 + top, 40 * 8,
  114. // 5);
  115. // // 横线下方区域
  116. // // 左侧的二维码
  117. // iPrinter.draw2DBarCodeTSPL(0 + left, 7 * 8 + 15 + top,
  118. // TwoDarCodeType.QR, 1, 4, null, "热敏不干胶纸");
  119. // // 右侧第一行汉字
  120. // iPrinter.drawTextTSPL(14 * 8 + left, 7 * 8 + 10 + top,
  121. // true, 2,
  122. // 2, null, "SIZE:");
  123. // // 右侧第二行汉字
  124. // iPrinter.drawTextTSPL(14 * 8 + left, 7 * 8 + 55 + top,
  125. // true, 2,
  126. // 2, null, "40MM*30MM");
  127. // // 二维码下方的一维条码
  128. // iPrinter.drawBarCodeTSPL(0 + left, 20 * 8 + 4 + top,
  129. // PBarcodeType.CODE128, 5 * 8, false, null, 1, 2,
  130. // "123456789012");
  131. // iPrinter.drawTextTSPL(0 + left, 25 * 8 + top, 40 * 8 +
  132. // left, 30 *
  133. // 8 + top, PAlign.CENTER, PAlign.CENTER,
  134. // true, 1, 1, null, "123456789012");
  135. // // 打印
  136. // iPrinter.printTSPL(numbers, 1);
  137. } catch (WriteException e) {
  138. e.printStackTrace();
  139. } catch (PrinterPortNullException e) {
  140. e.printStackTrace();
  141. } catch (ParameterErrorException e) {
  142. // TODO Auto-generated catch block
  143. e.printStackTrace();
  144. } catch (Exception e) {
  145. // TODO Auto-generated catch block
  146. e.printStackTrace();
  147. }
  148. }
  149. }