BTPPrintUtil.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. package io.flutter.plugins.utils;
  2. import android.graphics.Bitmap;
  3. import android.graphics.BitmapFactory;
  4. import android.util.Log;
  5. import androidx.annotation.NonNull;
  6. import com.snbc.sdk.LabelPrinter;
  7. import java.io.ByteArrayOutputStream;
  8. import java.nio.ByteBuffer;
  9. import java.util.ArrayList;
  10. import java.util.List;
  11. import io.flutter.plugin.common.MethodChannel;
  12. public class BTPPrintUtil {
  13. /**
  14. * 单例实例,使用 volatile 保证多线程可见性和有序性
  15. */
  16. private static volatile LabelPrinter labelPrinter;
  17. private static int nLang = 2;
  18. /**
  19. * 获取 LabelPrinter 单例实例
  20. *
  21. * @return LabelPrinter 实例
  22. */
  23. public static LabelPrinter getInstance() {
  24. // 双重检查锁定以确保只初始化一次实例
  25. if (labelPrinter == null) {
  26. synchronized (BTPPrintUtil.class) {
  27. if (labelPrinter == null) {
  28. labelPrinter = new LabelPrinter();
  29. }
  30. }
  31. }
  32. return labelPrinter;
  33. }
  34. /**
  35. * 通过打印机mac地址进行蓝牙连接开启打印机(同步)
  36. *
  37. * @param address 打印机地址
  38. * @return 成功与否
  39. */
  40. public static int connectBluetoothPrinter(String address) {
  41. // return 1;
  42. // 获取单例实例以确保线程安全
  43. return getInstance().ConnectPrinter(7, address, 4);
  44. }
  45. public static void printBitMap(Bitmap bitmap, String deviceName, @NonNull MethodChannel.Result result) {
  46. int x = 0;
  47. if(deviceName.contains("BTP-UP321")) {
  48. x = 0;
  49. } else if(deviceName.contains("BTP-P398PLUS")) {
  50. x = 40;
  51. }
  52. int bytes = bitmap.getByteCount();
  53. ByteBuffer buf = ByteBuffer.allocate(bytes);
  54. bitmap.copyPixelsToBuffer(buf);
  55. ByteArrayOutputStream LionData=new ByteArrayOutputStream();
  56. bitmap.compress(Bitmap.CompressFormat.PNG, 100, LionData);
  57. getInstance().SetLabelSize(560,640);
  58. int code = getInstance().PrintImageData(x, 0, LionData.toByteArray());
  59. getInstance().PrintLabel(1,1);
  60. result.success(code == 0 ? true : false);
  61. }
  62. public static void printText(ArrayList<String> textList,@NonNull MethodChannel.Result result) {
  63. int width = 560;
  64. int height = 640;
  65. getInstance().SetLabelSize(width,height);
  66. int marginX = 2;
  67. int marginY = 40;
  68. //矩形框类型
  69. int rectangleWidth = width - marginX * 2;
  70. int rectangleHeight = height - marginY * 2;
  71. int lineWidth = 1;
  72. int lineHeight = rectangleHeight / 6;
  73. int titleWidth = rectangleWidth / 2;
  74. float fontSize = 3;
  75. // 设置初始偏移量
  76. int offsetY = 0;
  77. int offsetX = 0;
  78. // 计算绘制线条的 y 坐标
  79. int secondLineY = marginY + lineHeight * 2 - lineWidth + offsetY;
  80. int thirdLineY = marginY + lineHeight * 3 - lineWidth + offsetY;
  81. int fourthLineY = marginY + lineHeight * 4 - lineWidth + offsetY;
  82. int fiveLineY = marginY + lineHeight * 5 - lineWidth + offsetY;
  83. int textAlignHorizontal = 0; // 左对齐
  84. labelPrinter.PrintText(marginX * 2 + offsetX, marginY + offsetY, "3", textList.get(0), 0, 7, 0, 0);
  85. labelPrinter.PrintText(marginX * 2 + titleWidth + offsetX, marginY + offsetY, "3", textList.get(1), 0, 7, 0, 0);
  86. labelPrinter.PrintText(marginX * 2 + offsetX, marginY + lineHeight - lineWidth + offsetY, "3", textList.get(2), 0, 7, 0, 0);
  87. labelPrinter.PrintText(marginX * 2 + offsetX, secondLineY, "3", textList.get(3), 0, 7, 0, 0);
  88. labelPrinter.PrintText(marginX * 2 + offsetX, thirdLineY, "3", textList.get(4), 0, 7, 0, 0);
  89. labelPrinter.PrintText(marginX * 2 + titleWidth + offsetX, thirdLineY, "3", textList.get(5), 0, 7, 0, 0);
  90. labelPrinter.PrintText(marginX * 2 + offsetX, fourthLineY, "3", textList.get(6), 0, 7, 0, 0);
  91. labelPrinter.PrintText(marginX * 2 + titleWidth + offsetX, fourthLineY, "3", textList.get(7), 0, 7, 0, 0);
  92. labelPrinter.PrintText(marginX * 2 + offsetX, fiveLineY, "3", textList.get(8), 0, 7, 0, 0);
  93. labelPrinter.PrintText(marginX * 2 + titleWidth + offsetX, fiveLineY, "3", textList.get(9), 0, 7, 0, 0);
  94. int code = getInstance().PrintLabel(1,1);
  95. result.success(code == 0 ? true : false);
  96. }
  97. public static void printBarCodeWithText(String barCode, String text, ArrayList<String> textList,@NonNull MethodChannel.Result result) {
  98. getInstance().SetLabelSize(560,640*2);
  99. // 二维码
  100. labelPrinter.PrintBarcodeQR(90, 40, 0, barCode, 'M', 8, 2);
  101. //质量 品质 食安选项框
  102. labelPrinter.PrintRectangle(100, 440, 20, 20, 1);
  103. labelPrinter.PrintText(124, 440, "3", "质量", 0, 7, 0, 0);
  104. labelPrinter.PrintRectangle(220, 440, 20, 20, 1);
  105. labelPrinter.PrintText(244, 440, "3", "品质", 0, 7, 0, 0);
  106. labelPrinter.PrintRectangle(340, 440, 20, 20, 1);
  107. labelPrinter.PrintText(364, 440, "3", "食安", 0, 7, 0, 0);
  108. // 编号 labelPrinter.PrintText(100, 500, "8", "SHXM2024007404", 0, 7, 3, 0);
  109. labelPrinter.PrintText(120, 500, "5", text, 0, 0, 0, 0);
  110. //
  111. int width = 560;
  112. int height = 640;
  113. int marginX = 2;
  114. int marginY = 80;
  115. //矩形框类型
  116. int rectangleWidth = width - marginX * 2;
  117. int rectangleHeight = height - marginY * 2;
  118. int lineWidth = 1;
  119. int lineHeight = rectangleHeight / 6;
  120. int titleWidth = rectangleWidth / 2;
  121. float fontSize = 3;
  122. // 设置初始偏移量
  123. int offsetY = 680;
  124. int offsetX = 0;
  125. // 计算绘制线条的 y 坐标
  126. int secondLineY = marginY + lineHeight * 2 - lineWidth + offsetY;
  127. int thirdLineY = marginY + lineHeight * 3 - lineWidth + offsetY;
  128. int fourthLineY = marginY + lineHeight * 4 - lineWidth + offsetY;
  129. int fiveLineY = marginY + lineHeight * 5 - lineWidth + offsetY;
  130. int textAlignHorizontal = 0; // 左对齐
  131. labelPrinter.PrintText(marginX * 2 + offsetX, marginY + offsetY, "3", textList.get(0), 0, 7, 0, 0);
  132. labelPrinter.PrintText(marginX * 2 + titleWidth + offsetX, marginY + offsetY, "3", textList.get(1), 0, 7, 0, 0);
  133. labelPrinter.PrintText(marginX * 2 + offsetX, marginY + lineHeight - lineWidth + offsetY, "3", textList.get(2), 0, 7, 0, 0);
  134. labelPrinter.PrintText(marginX * 2 + offsetX, secondLineY, "3", textList.get(3), 0, 7, 0, 0);
  135. labelPrinter.PrintText(marginX * 2 + offsetX, thirdLineY, "3", textList.get(4), 0, 7, 0, 0);
  136. labelPrinter.PrintText(marginX * 2 + titleWidth + offsetX, thirdLineY, "3", textList.get(5), 0, 7, 0, 0);
  137. labelPrinter.PrintText(marginX * 2 + offsetX, fourthLineY, "3", textList.get(6), 0, 7, 0, 0);
  138. labelPrinter.PrintText(marginX * 2 + titleWidth + offsetX, fourthLineY, "3", textList.get(7), 0, 7, 0, 0);
  139. labelPrinter.PrintText(marginX * 2 + offsetX, fiveLineY, "3", textList.get(8), 0, 7, 0, 0);
  140. labelPrinter.PrintText(marginX * 2 + titleWidth + offsetX, fiveLineY, "3", textList.get(9), 0, 7, 0, 0);
  141. //
  142. int code = getInstance().PrintLabel(1,1);
  143. result.success(code == 0 ? true : false);
  144. }
  145. // BTP打印机同时打印图标和标签只能调这个,依次printBitMap再printText会导致偶发printText不走。
  146. public static void PrintBitMapAndText(Bitmap bitmap, String deviceName, ArrayList<String> textList,@NonNull MethodChannel.Result result) {
  147. int x = 0;
  148. if(deviceName.contains("BTP-UP321")) {
  149. x = 0;
  150. } else if(deviceName.contains("BTP-P398PLUS")) {
  151. x = 40;
  152. }
  153. int bytes = bitmap.getByteCount();
  154. ByteBuffer buf = ByteBuffer.allocate(bytes);
  155. bitmap.copyPixelsToBuffer(buf);
  156. ByteArrayOutputStream LionData=new ByteArrayOutputStream();
  157. bitmap.compress(Bitmap.CompressFormat.PNG, 100, LionData);
  158. getInstance().SetLabelSize(560,640*2);
  159. int code = getInstance().PrintImageData(x, 0, LionData.toByteArray());
  160. int width = 560;
  161. int height = 640;
  162. int marginX = 2;
  163. int marginY = 80;
  164. //矩形框类型
  165. int rectangleWidth = width - marginX * 2;
  166. int rectangleHeight = height - marginY * 2;
  167. int lineWidth = 1;
  168. int lineHeight = rectangleHeight / 6;
  169. int titleWidth = rectangleWidth / 2;
  170. float fontSize = 3;
  171. // 设置初始偏移量
  172. int offsetY = 680;
  173. int offsetX = 0;
  174. // 计算绘制线条的 y 坐标
  175. int secondLineY = marginY + lineHeight * 2 - lineWidth + offsetY;
  176. int thirdLineY = marginY + lineHeight * 3 - lineWidth + offsetY;
  177. int fourthLineY = marginY + lineHeight * 4 - lineWidth + offsetY;
  178. int fiveLineY = marginY + lineHeight * 5 - lineWidth + offsetY;
  179. int textAlignHorizontal = 0; // 左对齐
  180. labelPrinter.PrintText(marginX * 2 + offsetX, marginY + offsetY, "3", textList.get(0), 0, 7, 0, 0);
  181. labelPrinter.PrintText(marginX * 2 + titleWidth + offsetX, marginY + offsetY, "3", textList.get(1), 0, 7, 0, 0);
  182. labelPrinter.PrintText(marginX * 2 + offsetX, marginY + lineHeight - lineWidth + offsetY, "3", textList.get(2), 0, 7, 0, 0);
  183. labelPrinter.PrintText(marginX * 2 + offsetX, secondLineY, "3", textList.get(3), 0, 7, 0, 0);
  184. labelPrinter.PrintText(marginX * 2 + offsetX, thirdLineY, "3", textList.get(4), 0, 7, 0, 0);
  185. labelPrinter.PrintText(marginX * 2 + titleWidth + offsetX, thirdLineY, "3", textList.get(5), 0, 7, 0, 0);
  186. labelPrinter.PrintText(marginX * 2 + offsetX, fourthLineY, "3", textList.get(6), 0, 7, 0, 0);
  187. labelPrinter.PrintText(marginX * 2 + titleWidth + offsetX, fourthLineY, "3", textList.get(7), 0, 7, 0, 0);
  188. labelPrinter.PrintText(marginX * 2 + offsetX, fiveLineY, "3", textList.get(8), 0, 7, 0, 0);
  189. labelPrinter.PrintText(marginX * 2 + titleWidth + offsetX, fiveLineY, "3", textList.get(9), 0, 7, 0, 0);
  190. getInstance().PrintLabel(1,1);
  191. result.success(code == 0 ? true : false);
  192. }
  193. }