Android Control POS Machine graphic printing (ii)

Source: Internet
Author: User
Tags ticket

The previous article ended the esc/pos instruction set, not seen can go to see, can be used as a tool document to use

Android Control POS Machine graphic printing (i)

This article formally describes how to use a POS machine to print text messages.

Let's start by introducing how the ESC/POS instructions are used,

Font Bold instruction:

Byte[] Font_b =new byte[3];

Font_b[0] = 27;

Font_b[1] = 33;

FONT_B[2] = 8;

The font is enlarged one times in portrait orientation:

Byte[] Clear_font =new byte[3];

Clear_font[0] = 0x1c;

CLEAR_FONT[1] = 0x21;

CLEAR_FONT[2] = 0;

Clear the font magnification directive:

Byte[] Fd_font =new byte[3];

Fd_font[0] = 0x1c;

FD_FONT[1] = 0x21;

FD_FONT[2] = 4;

Print Barcode Instructions:

Byte[] Print_code =new byte[9];

Print_code[0] = 0x1d;

PRINT_CODE[1] = 0x68;

PRINT_CODE[2] = 120;

PRINT_CODE[3] = 0x1d;

PRINT_CODE[4] = 0x48;

PRINT_CODE[5] = 0x10;

PRINT_CODE[6] = 0x1d;

PRINT_CODE[7] = 0x6b;

PRINT_CODE[8] = 0x02;

And so on instructions to use ...

You can set it up by referring to the instruction set for the previous article. Where the esc/pos instruction can be set in decimal and hexadecimal settings, this should be noted.

The next step is to show you how to connect to the printer.

private static STRINGPRINT_IP = "192.168.1.111";//Printer IP Address

Private Staticint Print_port = 9100;//Printer Port default is 9100 We recommend that you do not modify the printer port to modify the

Here set the printer's IP address and port, where the port number of 9100 is the default setting of the printer, it is best not to modify, if modified, may lead to the connection is not, I started when the set up 9000, the connection is not on the ...

Then connect the printer via the socket

Establish printer connections

Socket = new socket ();

Inetsocketaddress isocketaddress = new Inetsocketaddress (print_ip, print_port);//Bind printer

Socket.connect (isocketaddress);//Establish connection

The next step is to write a printing method that calls the printer transfer instructions and data to the printer for printing.

Directly on the code, the code inside the comments are very clear

[Java] view plaincopy

  1. /**
  2. * @param print_data
  3. * Small Ticket main data
  4. * @param gs_info
  5. * Small ticket comes with information
  6. * @param CAIDAN_SN
  7. * Small Ticket Number
  8. */
  9. public void print (list<map<string, object>> print_data,map<string, string> gs_info, String CAIDAN_SN ) {
  10. if (print_data! = null && print_data.size () > 0) {
  11. try {
  12. Barcode Printing Instructions
  13. byte[] Print_code = new BYTE[9];
  14. Print_code[0] = 0x1d;
  15. PRINT_CODE[1] = 0x68;
  16. PRINT_CODE[2] = 120;
  17. PRINT_CODE[3] = 0x1d;
  18. PRINT_CODE[4] = 0x48;
  19. PRINT_CODE[5] = 0x10;
  20. PRINT_CODE[6] = 0x1d;
  21. PRINT_CODE[7] = 0x6b;
  22. PRINT_CODE[8] = 0x02;
  23. Clear font magnification directive
  24. byte[] Fd_font = new Byte[3];
  25. Fd_font[0] = 0x1c;
  26. FD_FONT[1] = 0x21;
  27. FD_FONT[2] = 4;
  28. Font bold Instruction
  29. byte[] Font_b = new Byte[3];
  30. Font_b[0] = 27;
  31. Font_b[1] = 33;
  32. FONT_B[2] = 8;
  33. The font is enlarged one-fold vertically
  34. byte[] Clear_font = new Byte[3];
  35. Clear_font[0] = 0x1c;
  36. CLEAR_FONT[1] = 0x21;
  37. CLEAR_FONT[2] = 0;
  38. Calculate Total Amount
  39. int price = 0;
  40. Initializing the printer
  41. Socketout.write (27);
  42. Socketout.write (64);
  43. Socketout.write (Fd_font);//font magnification
  44. Socketout.write (font_b);//FONT Bold
  45. Socketout.write (10);
  46. Writer.write ("" + Gs_info.get ("gs_name") + "\ r \ n");
  47. Writer.flush ();//key, it is important, otherwise the instruction is output once, the following instruction overrides the preceding instruction, resulting in cancellation of the amplification instruction invalid
  48. Socketout.write (Clear_font);
  49. Socketout.write (10);
  50. Writer.write ("NO:" + caidan_sn + "\ r \ n");
  51. Writer.write ("------------------------------\ r \ n");
  52. Writer.write ("Buyer's name:" + gs_info.get ("gs_user_name") + "\ r \ n");
  53. Writer.write ("Address:" + gs_info.get ("gs_address") + "\ r \ n");
  54. Writer.write ("Contact Phone:" + gs_info.get ("Gs_tel") + "\ r \ n");
  55. Writer.write ("Payment method:" + gs_info.get ("Gs_pay_type") + "\ r \ n");
  56. Writer.write ("------------------------------\ r \ n");
  57. Writer.write (Fix_string_lenth (1,colum_name[0], 4)
  58. + Fix_string_lenth (0,colum_name[1], 14)
  59. + Fix_string_lenth (1,colum_name[2], 4)
  60. + Fix_string_lenth (1,colum_name[3], 6) + "\ r \ n");
  61. for (int i = 0; i < print_data.size ()-1; i++) {
  62. Writer.write (Fix_string_lenth (1,i + 1 + "", 2)
  63. + Fix_string_lenth (0,print_data.get (i). Get ("Cai_name"). ToString (), 14)
  64. + Fix_string_lenth (1,print_data.get (i). Get ("Cai_num"). ToString (), 4)
  65. + Fix_string_lenth (1,print_data.get (i). Get ("Cai_price"). ToString (), 6) + "\ r \ n");
  66. Price + = double.parsedouble (Print_data.get (i). Get ("Cai_price"). toString ());
  67. }
  68. Print two-dimensional code
  69. Bitmap bmp = (Bitmap) print_data.get (Print_data.size ()-1). Get ("Erweima");
  70. byte[] data = new byte[] {0x1B, 0x33, 0x00};
  71. Socketout.write (data);
  72. Data[0] = (byte) 0x00;
  73. DATA[1] = (byte) 0x00;
  74. DATA[2] = (byte) 0x00; Reset Parameters
  75. int pixelcolor;
  76. ESC * M NL NH bitmap
  77. byte[] escbmp = new byte[] {0x1B, 0x2A, 0x00, 0x00, 0x00};
  78. ESCBMP[2] = (byte) 0x21;
  79. NL, NH
  80. ESCBMP[3] = (byte) (Bmp.getwidth ()% 256);
  81. ESCBMP[4] = (byte) (Bmp.getwidth ()/256);
  82. Print on each line
  83. for (int i = 0; i < bmp.getheight ()/+ 1; i++) {
  84. Socketout.write (escbmp);
  85. for (int j = 0; J < Bmp.getwidth (); j + +) {
  86. for (int k = 0; k < k++) {
  87. if ((((i) + K) < Bmp.getheight ()) {
  88. Pixelcolor = Bmp.getpixel (J, (I *) + K);
  89. if (pixelcolor! =-1) {
  90. DATA[K/8] + = (byte) (>> (k% 8));
  91. }
  92. }
  93. }
  94. Socketout.write (data);
  95. Reset Parameters
  96. Data[0] = (byte) 0x00;
  97. DATA[1] = (byte) 0x00;
  98. DATA[2] = (byte) 0x00;
  99. }
  100. Line break
  101. byte[] Byte_send1 = new byte[2];
  102. Byte_send1[0] = 0x0d;
  103. BYTE_SEND1[1] = 0x0a;
  104. Socketout.write (BYTE_SEND1);
  105. }
  106. Line break
  107. byte[] Byte_send2 = new byte[2];
  108. Byte_send2[0] = 0x0d;
  109. BYTE_SEND2[1] = 0x0a;
  110. Send test information
  111. Socketout.write (BYTE_SEND2);
  112. Writer.write ("------------------------------\ r \ n");
  113. Writer.write ("This single Total" + (Print_data.size ()-1) + "pieces of goods, total costs:" + price + "Yuan \ r \ n");
  114. Writer.write ("------------------------------\ r \ n");
  115. Writer.write ("Thank you for your patronage \ r \ n");
  116. The following instructions automatically take the paper after printing is complete
  117. Writer.write (27);
  118. Writer.write (100);
  119. Writer.write (4);
  120. Writer.write (10);
  121. Writer.close ();
  122. Socketout.close ();
  123. } catch (IOException e) {
  124. E.printstacktrace ();
  125. } catch (Exception e) {
  126. E.printstacktrace ();
  127. }
  128. }
  129. }

Print text is a transmission string used to print, and need to print pictures, it is more troublesome, need to convert the picture into lattice data and then print each line, you can refer to the Print method.

The method can then be directly called to print, remember, android4.0 can not be executed in the main thread, you need to create a new thread to execute the method, such as

[Java] view plaincopy

    1. New Thread () {
    2. <span style= "White-space:pre" > </span>public void Run () {
    3. <span style= "White-space:pre" > </span>try {
    4. <span style= "White-space:pre" > </span>new printline (). Print (lists, Mapgs, SN);
    5. <span style= "White-space:pre" > </span>} catch (IOException e) {
    6. <span style= "White-space:pre" > </span>e.printstacktrace ();
    7. <span style= "White-space:pre" > </span>}
    8. <span style= "White-space:pre" > </span>};
    9. }.start ();

Actually Android call PO

Android Control POS Printer (ii)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.