PrintLabel80_1.java 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. package com.printer.demo.utils;
  2. import android.util.Log;
  3. import com.printer.sdk.PrinterConstants.LableFontSize;
  4. import com.printer.sdk.PrinterConstants.LablePaperType;
  5. import com.printer.sdk.PrinterConstants.PAlign;
  6. import com.printer.sdk.PrinterConstants.PBarcodeType;
  7. import com.printer.sdk.PrinterConstants.PRotate;
  8. import com.printer.sdk.PrinterInstance;
  9. public class PrintLabel80_1 {
  10. // 材料80mm
  11. private static int MULTIPLE = 2;
  12. private static final int line_width_border = 2; // 边框
  13. private static final int page_width = 260 * MULTIPLE; // 内容宽
  14. private static final int page_height = 165 * MULTIPLE; // 内容高
  15. private static final int margin_horizontal = 2 * MULTIPLE; // 水平边距
  16. private static final int top_left_x = margin_horizontal; // 左上角x坐标,左上角为原点
  17. private static final int margin_vertical = 2 * MULTIPLE; // 垂直边距
  18. private static final int top_left_y = margin_vertical; // 左上角y坐标
  19. private static final int border_width = page_width - 2 * margin_horizontal; // 边框宽度
  20. private static final int border_height = page_height - 2 * margin_vertical; // 边框高度
  21. private static final int top_right_x = top_left_x + border_width; // 右上角x坐标
  22. private static final int bottom_left_y = top_left_y + border_height; // 左下角y坐标
  23. private static final int bottom_right_y = bottom_left_y; // 右下角y坐标
  24. private static final int bottom_right_x = top_right_x; // 右下角x坐标
  25. private static final int row36_column1_width = 10 * MULTIPLE;
  26. private static final int row37_column3_width = 20 * MULTIPLE;
  27. private static final int row36_sep1_x = top_left_x + row36_column1_width;
  28. private static final int row37_sep2_x = top_right_x - row37_column3_width;
  29. private static final int[] row_height = { 10 * MULTIPLE * 2, 27 * MULTIPLE, 27 * MULTIPLE, 27 * MULTIPLE,
  30. 27 * MULTIPLE };
  31. private static final String TAG = "PrintLabel";
  32. public void doPrint(PrinterInstance iPrinter) {
  33. iPrinter.pageSetup(LablePaperType.Size_80mm, page_width, page_height);
  34. drawBox(iPrinter);
  35. // drawVerticalSeparator(iPrinter);
  36. drawRowContent(iPrinter);
  37. iPrinter.print(PRotate.Rotate_0, 1);
  38. }
  39. private void drawRowContent(PrinterInstance iPrinter) {
  40. // int area_start_x = top_left_x + (top_right_x - top_left_x) / 2;
  41. // int area_start_y = top_left_y;
  42. // 第一行左一内容
  43. // iPrinter.drawText(top_left_x, top_left_y, top_left_x + 2 *
  44. // (border_width) / 3 + 20, top_left_y + row_height[1],
  45. // PAlign.START, PAlign.END, "全程路运 12-02 04:22浙江",
  46. // LableFontSize.Size_32, 1, 0, 0, 0, PRotate.Rotate_0);
  47. iPrinter.drawText(top_left_x, top_left_y, "全程路运 12-02 04:22浙江", LableFontSize.Size_32, PRotate.Rotate_0, 1, 0,
  48. 0);
  49. // 第一行左二内容
  50. /*
  51. * iPrinter.drawText(top_left_x + 5 * (border_width) / 9, top_left_y ,
  52. * top_left_x + 2 * (border_width) / 3, top_left_y + row_height[1] ,
  53. * PAlign.CENTER, PAlign.CENTER, "浙江", LableFontSize.Size_32, 1, 0, 0,
  54. * 0, PRotate.Rotate_0);
  55. */
  56. // 第一行右二内容
  57. iPrinter.drawText(top_left_x + 2 * (border_width) / 3 + 20, top_left_y,
  58. top_left_x + 7 * (border_width) / 9 + 50, top_left_y + row_height[1], PAlign.END, PAlign.END, "邮特",
  59. LableFontSize.Size_32, 1, 0, 0, 0, PRotate.Rotate_0);
  60. // 第一行右一内容
  61. iPrinter.drawText(top_left_x + 7 * (border_width) / 9 + 30, top_left_y, top_right_x, top_left_y + row_height[1],
  62. PAlign.END, PAlign.END, "2014", LableFontSize.Size_24, 1, 0, 0, 0, PRotate.Rotate_0);
  63. // 第二行内容
  64. iPrinter.drawText(top_left_x + 7 * (border_width) / 9, top_left_y + row_height[1], top_right_x,
  65. top_left_y + row_height[1] * 2, PAlign.CENTER, PAlign.END, "特快", LableFontSize.Size_32, 1, 0, 0, 0,
  66. PRotate.Rotate_0);
  67. // 第三行左一内容
  68. iPrinter.drawText(top_left_x, top_left_y + row_height[1] * 2, top_left_x + 5 * (border_width) / 9,
  69. top_left_y + row_height[1] * 3, PAlign.START, PAlign.END, "义乌市 ", LableFontSize.Size_48, 1, 0, 0, 0,
  70. PRotate.Rotate_0);
  71. // 第三行左二内容
  72. iPrinter.drawText(top_left_x + 5 * (border_width) / 9, top_left_y + row_height[1] * 2 + 26,
  73. top_left_x + 2 * (border_width) / 3, top_left_y + row_height[1] * 3, PAlign.START, PAlign.START, "局收",
  74. LableFontSize.Size_24, 1, 0, 0, 0, PRotate.Rotate_0);
  75. // 第三行右二内容
  76. iPrinter.drawText(top_left_x + 2 * (border_width) / 3, top_left_y + row_height[1] * 2 + 26,
  77. top_left_x + 7 * (border_width) / 9, top_left_y + row_height[1] * 3, PAlign.END, PAlign.START, "号码",
  78. LableFontSize.Size_24, 1, 0, 0, 0, PRotate.Rotate_0);
  79. // 第三行右一内容
  80. iPrinter.drawText(top_left_x + 7 * (border_width) / 9, top_left_y + row_height[1] * 2, top_right_x,
  81. top_left_y + row_height[1] * 3, PAlign.CENTER, PAlign.END, "58280" + " ", LableFontSize.Size_24, 1, 0,
  82. 2, 0, PRotate.Rotate_0);
  83. // 第四行左一内容
  84. iPrinter.drawText(top_left_x, top_left_y + row_height[1] * 3, top_left_x + 5 * (border_width) / 9,
  85. top_left_y + row_height[1] * 4, PAlign.START, PAlign.END, "江西集散 7件", LableFontSize.Size_32, 1, 0, 0, 0,
  86. PRotate.Rotate_0);
  87. // 第四行左二内容
  88. iPrinter.drawText(top_left_x + 5 * (border_width) / 9, top_left_y + row_height[1] * 3 + 26,
  89. top_left_x + 2 * (border_width) / 3, top_left_y + row_height[1] * 4, PAlign.START, PAlign.START, "局发",
  90. LableFontSize.Size_24, 1, 0, 0, 0, PRotate.Rotate_0);
  91. // 第四行右二内容
  92. iPrinter.drawText(top_left_x + 2 * (border_width) / 3, top_left_y + row_height[1] * 3 + 26,
  93. top_left_x + 7 * (border_width) / 9, top_left_y + row_height[1] * 4, PAlign.END, PAlign.START, "重量",
  94. LableFontSize.Size_24, 1, 0, 0, 0, PRotate.Rotate_0);
  95. // 第四行右一内容
  96. iPrinter.drawText(top_left_x + 7 * (border_width) / 9, top_left_y + row_height[1] * 3, top_right_x,
  97. top_left_y + row_height[1] * 4, PAlign.CENTER, PAlign.END, " " + "10" + " ", LableFontSize.Size_24, 1,
  98. 0, 2, 0, PRotate.Rotate_0);
  99. // 打印第五行条码
  100. // iPrinter.drawBarCode(top_left_x, top_left_y + row_height[1] * 4,
  101. // bottom_right_x, bottom_right_y - 30,
  102. // PAlign.START, PAlign.END, 0, 0,
  103. // "33000166-32206100-1-131-5828-0060-3-0", PBarcodeType.CODE128, 1, 60,
  104. // PRotate.Rotate_0);
  105. iPrinter.drawBarCode(0, top_left_y + row_height[1] * 4, "33000166322061001131582800603",
  106. PBarcodeType.CODE128, 1, 60, PRotate.Rotate_0);
  107. // 打印第五行条码下方的文字
  108. iPrinter.drawText(top_left_x, top_left_y + bottom_right_y - 30, bottom_right_x, bottom_right_y, PAlign.START,
  109. PAlign.START, "33000166-32206100-1-131-5828-0060-3", LableFontSize.Size_24, 1, 0, 0, 0,
  110. PRotate.Rotate_0);
  111. }
  112. private void drawHorizontalSeparator(PrinterInstance iPrinter, int start_x, int end_x) {
  113. int temp = top_left_y; //
  114. for (int i = 1; i < row_height.length; i++) {
  115. temp += row_height[i];
  116. int start_x1 = top_left_x + 7 * (border_width) / 9;
  117. int end_x1 = top_left_x + 5 * (border_width) / 9;
  118. // Log.i("temp", "第"+(i+1)+"次");
  119. iPrinter.drawLine(2, start_x, temp, end_x1, temp, true);
  120. /*
  121. * if(i > 2 ){ iPrinter.drawLine(1, start_x1, temp,
  122. * end_x,temp,true); }
  123. */
  124. /*
  125. * if(i!= 3){ iPrinter.drawLine(line_width_border, start_x, temp,
  126. * end_x, temp); }else{ iPrinter.drawLine(line_width_border,
  127. * row37_sep2_x, temp, end_x, temp); }
  128. */
  129. }
  130. }
  131. private void drawVerticalSeparator(PrinterInstance iPrinter) {
  132. int start_x = top_left_x + row_height[1];
  133. int start_y = top_left_y + row_height[0] + row_height[1];
  134. int end_x = start_x;
  135. int end_y = top_left_y + row_height[0] + row_height[1] * 5;
  136. // 从左边数起第一条分割线
  137. iPrinter.drawLine(line_width_border, start_x, start_y, end_x, end_y, true);
  138. // 从左边数起第二条分割线
  139. start_x = top_left_x + (top_right_x - top_left_x) / 2;
  140. start_y = top_left_y;
  141. end_x = start_x;
  142. end_y = start_y + row_height[0];
  143. Log.i(TAG, "start_x;" + start_x + "end_x:" + end_x);
  144. iPrinter.drawLine(line_width_border, start_x, start_y, end_x, end_y, true);
  145. // 从左边数起第三条分割线
  146. start_x = top_left_x + 3 * (top_right_x - top_left_x) / 4;
  147. start_y = top_left_y + row_height[0] + row_height[1];
  148. end_x = start_x;
  149. end_y = bottom_right_y;
  150. iPrinter.drawLine(line_width_border, start_x, start_y, end_x, end_y, true);
  151. }
  152. private void drawBox(PrinterInstance iPrinter) {
  153. int border_top_left_y = top_left_y;
  154. // iPrinter.drawBorder(3, top_left_x, border_top_left_y, bottom_right_x,
  155. // bottom_right_y);
  156. drawHorizontalSeparator(iPrinter, top_left_x, bottom_right_x);
  157. }
  158. }