PrintTool.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. package xn.hxp.utils;
  2. import android.graphics.Bitmap;
  3. import android.util.Log;
  4. import android.widget.Toast;
  5. import com.blankj.utilcode.util.ActivityUtils;
  6. import com.blankj.utilcode.util.LogUtils;
  7. import com.blankj.utilcode.util.ThreadUtils;
  8. import com.caysn.autoreplyprint.AutoReplyPrint;
  9. import com.sun.jna.Pointer;
  10. import xn.hxp.R;
  11. import xn.hxp.ui.PrintBean;
  12. public enum PrintTool {
  13. INSTANCE;
  14. private Pointer pointer = Pointer.NULL;
  15. private AutoReplyPrint.CP_OnPortOpenedEvent_Callback openedEventCallback;
  16. private AutoReplyPrint.CP_OnPortOpenFailedEvent_Callback openFailedEventCallback;
  17. private AutoReplyPrint.CP_OnPrinterStatusEvent_Callback statusEventCallback;
  18. private AutoReplyPrint.CP_OnPrinterReceivedEvent_Callback receivedEventCallback;
  19. public void print(boolean isGR, boolean isBelong, PrintBean printBean) throws Exception {
  20. try {
  21. if (null != openedEventCallback) {
  22. openedEventCallback = (handle, name, private_data) -> LogUtils.d("打印机打开成功");
  23. AutoReplyPrint.INSTANCE.CP_Port_AddOnPortOpenedEvent(openedEventCallback, pointer);
  24. }
  25. if (null != openFailedEventCallback) {
  26. openFailedEventCallback = (handle, name, private_data) -> {
  27. pointer = AutoReplyPrint.INSTANCE.CP_Port_OpenUsb("VID:0x0FE6,PID:0x811E", 1);
  28. LogUtils.d("打开打印机失败");
  29. };
  30. AutoReplyPrint.INSTANCE.CP_Port_AddOnPortOpenFailedEvent(openFailedEventCallback, pointer);
  31. }
  32. if (null != statusEventCallback) {
  33. statusEventCallback = (handle, printer_error_status, printer_info_status, private_data) -> {
  34. AutoReplyPrint.CP_PrinterStatus status = new AutoReplyPrint.CP_PrinterStatus(printer_error_status, printer_info_status);
  35. String error_status_string = String.format(" Printer Error Status: 0x%04X", printer_error_status & 0xffff);
  36. if (status.ERROR_OCCURED()) {
  37. if (status.ERROR_CUTTER())
  38. error_status_string += "[ERROR_CUTTER]";
  39. if (status.ERROR_FLASH())
  40. error_status_string += "[ERROR_FLASH]";
  41. if (status.ERROR_NOPAPER())
  42. error_status_string += "[ERROR_NOPAPER]";
  43. if (status.ERROR_VOLTAGE())
  44. error_status_string += "[ERROR_VOLTAGE]";
  45. if (status.ERROR_MARKER())
  46. error_status_string += "[ERROR_MARKER]";
  47. if (status.ERROR_ENGINE())
  48. error_status_string += "[ERROR_MOVEMENT]";
  49. if (status.ERROR_OVERHEAT())
  50. error_status_string += "[ERROR_OVERHEAT]";
  51. if (status.ERROR_COVERUP())
  52. error_status_string += "[ERROR_COVERUP]";
  53. if (status.ERROR_MOTOR())
  54. error_status_string += "[ERROR_MOTOR]";
  55. }
  56. String info_status_string = String.format(" Printer Info Status: 0x%04X", printer_info_status & 0xffff);
  57. if (status.INFO_LABELMODE())
  58. info_status_string += "[Label Mode]";
  59. if (status.INFO_LABELPAPER())
  60. info_status_string += "[Label Paper]";
  61. if (status.INFO_PAPERNOFETCH())
  62. info_status_string += "[Paper Not Fetch]";
  63. LogUtils.d("打印机状态", error_status_string, info_status_string);
  64. };
  65. AutoReplyPrint.INSTANCE.CP_Printer_AddOnPrinterStatusEvent(statusEventCallback, pointer);
  66. }
  67. if (null != receivedEventCallback) {
  68. receivedEventCallback = (handle, printer_received_byte_count, private_data) -> LogUtils.d("打印机回传", printer_received_byte_count);
  69. AutoReplyPrint.INSTANCE.CP_Printer_AddOnPrinterReceivedEvent(receivedEventCallback, pointer);
  70. }
  71. // 判断是否打开打印机
  72. if (AutoReplyPrint.INSTANCE.CP_Port_IsOpened(pointer)) {
  73. if (isGR) {
  74. startPrintGR(isBelong, printBean);
  75. } else {
  76. startPrint(isBelong, printBean);
  77. }
  78. } else {
  79. pointer = AutoReplyPrint.INSTANCE.CP_Port_OpenUsb("VID:0x0FE6,PID:0x811E", 1);
  80. LogUtils.d("打开打印机", pointer);
  81. if (isGR) {
  82. startPrintGR(isBelong, printBean);
  83. } else {
  84. startPrint(isBelong, printBean);
  85. }
  86. }
  87. } catch (Exception e) {
  88. LogUtils.d(Log.getStackTraceString(e));
  89. }
  90. }
  91. private void startPrintGR(boolean isBelong, PrintBean printBean) {
  92. Bitmap bitmap = BitmapUtils.generateBarBitmap(isBelong, printBean);
  93. if (AutoReplyPrint.INSTANCE.CP_Label_BackPaperToPrintPosition(pointer)) {
  94. LogUtils.d("打印机退纸成功");
  95. if (AutoReplyPrint.CP_Pos_PrintRasterImageFromData_Helper.PrintRasterImageFromBitmap(pointer,
  96. bitmap.getWidth(),
  97. bitmap.getHeight(),
  98. bitmap,
  99. AutoReplyPrint.CP_ImageBinarizationMethod_Thresholding,
  100. AutoReplyPrint.CP_ImageCompressionMethod_None
  101. )) {
  102. LogUtils.d("打印机打印图片成功");
  103. if (AutoReplyPrint.INSTANCE.CP_Label_PagePrint(pointer, 1)) {
  104. LogUtils.d("打印机文字成功");
  105. if (AutoReplyPrint.INSTANCE.CP_Pos_HalfCutPaper(pointer)) {
  106. LogUtils.d("打印机切割成功");
  107. AudioPlayer.getInstance().play(R.raw.label_to_the_package);
  108. } else {
  109. ThreadUtils.runOnUiThread(() -> Toast.makeText(ActivityUtils.getTopActivity(), "打印机切割失败", Toast.LENGTH_LONG).show());
  110. }
  111. } else {
  112. ThreadUtils.runOnUiThread(() -> Toast.makeText(ActivityUtils.getTopActivity(), "打印机文字失败", Toast.LENGTH_LONG).show());
  113. }
  114. } else {
  115. ThreadUtils.runOnUiThread(() -> Toast.makeText(ActivityUtils.getTopActivity(), "打印机打印图片失败", Toast.LENGTH_LONG).show());
  116. }
  117. } else {
  118. ThreadUtils.runOnUiThread(() -> Toast.makeText(ActivityUtils.getTopActivity(), "打印机退纸失败", Toast.LENGTH_LONG).show());
  119. }
  120. }
  121. private void startPrint(boolean isBelong, PrintBean printBean) {
  122. Bitmap bitmap = BitmapUtils.generateBitmap(isBelong, printBean);
  123. if (AutoReplyPrint.INSTANCE.CP_Label_BackPaperToPrintPosition(pointer)) {
  124. LogUtils.d("打印机退纸成功");
  125. if (AutoReplyPrint.CP_Pos_PrintRasterImageFromData_Helper.PrintRasterImageFromBitmap(pointer,
  126. bitmap.getWidth(),
  127. bitmap.getHeight(),
  128. bitmap,
  129. AutoReplyPrint.CP_ImageBinarizationMethod_Thresholding,
  130. AutoReplyPrint.CP_ImageCompressionMethod_None
  131. )) {
  132. LogUtils.d("打印机打印图片成功");
  133. if (AutoReplyPrint.INSTANCE.CP_Label_PagePrint(pointer, 1)) {
  134. LogUtils.d("打印机文字成功");
  135. if (AutoReplyPrint.INSTANCE.CP_Pos_HalfCutPaper(pointer)) {
  136. LogUtils.d("打印机切割成功");
  137. AudioPlayer.getInstance().play(R.raw.label_to_the_package);
  138. } else {
  139. ThreadUtils.runOnUiThread(() -> Toast.makeText(ActivityUtils.getTopActivity(), "打印机切割失败", Toast.LENGTH_LONG).show());
  140. }
  141. } else {
  142. ThreadUtils.runOnUiThread(() -> Toast.makeText(ActivityUtils.getTopActivity(), "打印机文字失败", Toast.LENGTH_LONG).show());
  143. }
  144. } else {
  145. ThreadUtils.runOnUiThread(() -> Toast.makeText(ActivityUtils.getTopActivity(), "打印机打印图片失败", Toast.LENGTH_LONG).show());
  146. }
  147. } else {
  148. ThreadUtils.runOnUiThread(() -> Toast.makeText(ActivityUtils.getTopActivity(), "打印机退纸失败", Toast.LENGTH_LONG).show());
  149. }
  150. }
  151. public void print(String string) {
  152. try {
  153. // 判断是否打开打印机
  154. if (AutoReplyPrint.INSTANCE.CP_Port_IsOpened(pointer)) {
  155. startPrint(string);
  156. } else {
  157. pointer = AutoReplyPrint.INSTANCE.CP_Port_OpenUsb("VID:0x0FE6,PID:0x811E", 1);
  158. AutoReplyPrint.INSTANCE.CP_Port_AddOnPortOpenedEvent(new AutoReplyPrint.CP_OnPortOpenedEvent_Callback() {
  159. @Override
  160. public void CP_OnPortOpenedEvent(Pointer handle, String name, Pointer private_data) {
  161. startPrint(string);
  162. }
  163. }, pointer);
  164. LogUtils.d("打开打印机");
  165. }
  166. } catch (Exception e) {
  167. LogUtils.d(Log.getStackTraceString(e));
  168. }
  169. }
  170. private void startPrint(String string) {
  171. LogUtils.d("打印机打开成功");
  172. Bitmap bitmap = BitmapUtils.airBottlePrint(string);
  173. if (AutoReplyPrint.INSTANCE.CP_Label_BackPaperToPrintPosition(pointer)) {
  174. LogUtils.d("打印机退纸成功");
  175. if (AutoReplyPrint.CP_Pos_PrintRasterImageFromData_Helper.PrintRasterImageFromBitmap(pointer,
  176. bitmap.getWidth(),
  177. bitmap.getHeight(),
  178. bitmap,
  179. AutoReplyPrint.CP_ImageBinarizationMethod_Thresholding,
  180. AutoReplyPrint.CP_ImageCompressionMethod_None
  181. )) {
  182. LogUtils.d("打印机打印图片成功");
  183. if (AutoReplyPrint.INSTANCE.CP_Label_PagePrint(pointer, 1)) {
  184. LogUtils.d("打印机文字成功");
  185. if (AutoReplyPrint.INSTANCE.CP_Pos_HalfCutPaper(pointer)) {
  186. LogUtils.d("打印机切割成功");
  187. AudioPlayer.getInstance().play(R.raw.label_to_the_package);
  188. } else {
  189. ThreadUtils.runOnUiThread(() -> Toast.makeText(ActivityUtils.getTopActivity(), "打印机切割失败", Toast.LENGTH_LONG).show());
  190. }
  191. } else {
  192. ThreadUtils.runOnUiThread(() -> Toast.makeText(ActivityUtils.getTopActivity(), "打印机文字失败", Toast.LENGTH_LONG).show());
  193. }
  194. } else {
  195. ThreadUtils.runOnUiThread(() -> Toast.makeText(ActivityUtils.getTopActivity(), "打印机打印图片失败", Toast.LENGTH_LONG).show());
  196. }
  197. } else {
  198. ThreadUtils.runOnUiThread(() -> Toast.makeText(ActivityUtils.getTopActivity(), "打印机退纸失败", Toast.LENGTH_LONG).show());
  199. }
  200. }
  201. }