Android-controlled POS machine graphic printing (2), androidpos
The Instruction Set of ESC/POS ended in the previous article. If you haven't read it, you can check it and use it as a tool document.
Image and text printing for android control POS machine (1)
This article officially introduces how to use a POS machine to print graphic information.
First, we will introduce how the ESC/POS command is used,
Font bold command:
Byte [] FONT_ B = new byte [3];
FONT_ B [0] = 27;
FONT_ B [1] = 33;
FONT_ B [2] = 8;
Vertical font magnification:
Byte [] CLEAR_FONT = new byte [3];
CLEAR_FONT [0] = 0x1c;
CLEAR_FONT [1] = 0x21;
CLEAR_FONT [2] = 0;
Clear font amplification instructions:
Byte [] FD_FONT = new byte [3];
FD_FONT [0] = 0x1c;
FD_FONT [1] = 0x21;
FD_FONT [2] = 4;
Print the barcode command:
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...
You can refer to the instruction set in the previous article for settings. The ESC/POS command can be set in decimal or hexadecimal format.
Next we will introduce how to connect to the printer.
Private static StringPRINT_IP = "192.168.1.111"; // printer IP Address
Private staticint PRINT_PORT = 9100; // The default printer port is 9100. do not modify the printer port.
The IP address and port of the printer are set here. The port number 9100 is the default setting for the printer, so it is best not to modify it. If it is modified, the connection may fail, I set 9000 at the beginning and the connection fails...
Then connect to the printer through socket
// Establish a printer connection
Socket = new Socket ();
InetSocketAddress iSocketAddress = new InetSocketAddress (PRINT_IP, PRINT_PORT); // bind a printer
Socket. connect (iSocketAddress); // establish a connection
The next step is to write a printing method and call the printer transmission command and data to print the data to the printer.
The code is directly written, and the comments in the Code are very clear.
/*** @ Param PRINT_DATA * main ticket data * @ param GS_INFO * additional ticket information * @ param CAIDAN_SN * invoice number */public void print (List <Map <String, object> PRINT_DATA, Map <String, String> GS_INFO, String CAIDAN_SN) {if (PRINT_DATA! = Null & PRINT_DATA.size ()> 0) {try {// bar code printing command 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; // clear the font amplification command byte [] FD_FONT = new byte [3]; FD_FONT [0] = 0x1c; FD_FONT [1] = 0x21; FD_FONT [2] = 4; // FONT bold command byte [] FONT_ B = new byte [3]; FONT _ B [0] = 27; FONT_ B [1] = 33; FONT_ B [2] = 8; // enlarge the font vertically by a factor of byte [] CLEAR_FONT = new byte [3]; CLEAR_FONT [0] = 0x1c; CLEAR_FONT [1] = 0x21; CLEAR_FONT [2] = 0; // calculate the total amount int price = 0; // initialize the socketOut printer. write (27); socketOut. write (64); socketOut. write (FD_FONT); // font to enlarge socketOut. write (FONT_ B); // bold socketOut in the font. write (10); writer. write ("" + GS_INFO.get ("GS_Name") + "\ r \ n"); writer. flush (); // The Key is very important. Otherwise, the command is output at one time, and the subsequent command overwrites the preceding command, causing cancellation. The amplification command is invalid. socketOut. write (CLEAR_FONT); socketOut. write (10); writer. write ("NO:" + CAIDAN_SN + "\ r \ n"); writer. write ("------------------------------ \ r \ n"); writer. write ("buyer name:" + GS_INFO.get ("GS_user_name") + "\ r \ n"); writer. write ("Address:" + GS_INFO.get ("GS_address") + "\ r \ n"); writer. write ("contact number:" + GS_INFO.get ("GS_tel") + "\ r \ n"); writer. write ("Payment Method:" + GS_INFO.get ("GS_pay_type") + "\ r \ n"); writer. write (" ------------------------------ \ R \ n "); writer. write (Fix_String_Lenth (1, Colum_Name [0], 4) + Fix_String_Lenth (0, Colum_Name [1], 14) + Fix_String_Lenth (1, Colum_Name [2], 4) + Fix_String_Lenth (1, Colum_Name [3], 6) + "\ r \ n"); for (int I = 0; I <PRINT_DATA.size ()-1; I ++) {writer. write (Fix_String_Lenth (1, I + 1 + "", 2) + Fix_String_Lenth (0, PRINT_DATA.get (I ). get ("cai_name "). toString (), 14) + Fix_String_Lenth (1, PR INT_DATA.get (I ). get ("cai_num "). toString (), 4) + Fix_String_Lenth (1, PRINT_DATA.get (I ). get ("cai_price "). toString (), 6) + "\ r \ n"); price + = Double. parseDouble (PRINT_DATA.get (I ). get ("cai_price "). toString ();} // print the QR code Bitmap bmp = (Bitmap) PRINT_DATA.get (PRINT_DATA.size ()-1 ). get ("erweima"); byte [] data = new byte [] {0x1B, 0x33, 0x00}; socketOut. write (data); data [0] = (byte) 0x00; data [1] = (byte) 0x00; d Ata [2] = (byte) 0x00; // reset the parameter int pixelColor; // ESC * m nL nH lattice map byte [] escBmp = new byte [] {0x1B, 0x2A, 0x00, 0x00, 0x00}; escBmp [2] = (byte) 0x21; // nL, nH escBmp [3] = (byte) (bmp. getWidth () % 256); escBmp [4] = (byte) (bmp. getWidth ()/256); // print for (int I = 0; I <bmp. getHeight ()/24 + 1; I ++) {socketOut. write (escBmp); for (int j = 0; j <bmp. getWidth (); j ++) {for (int k = 0; k <24; k ++) {If (I * 24) + k) <bmp. getHeight () {pixelColor = bmp. getPixel (j, (I * 24) + k); if (pixelColor! =-1) {data [k/8] + = (byte) (128 >>> (k % 8) ;}} socketOut. write (data); // reset the parameter data [0] = (byte) 0x00; data [1] = (byte) 0x00; data [2] = (byte) 0x00;} // wrap byte [] byte_send1 = new byte [2]; byte_send1 [0] = 0x0d; byte_send1 [1] = 0x0a; socketOut. write (byte_send1);} // wrap byte [] byte_send2 = new byte [2]; byte_send2 [0] = 0x0d; byte_send2 [1] = 0x0a; // send the test information socketOut. write (byte_send2); writer. write ("------------------------------ \ r \ n"); writer. write ("Total" + (PRINT_DATA.size ()-1) + "items, total fee:" + price + "Yuan \ r \ n"); writer. write ("------------------------------ \ r \ n"); writer. write ("Xie Huigu \ r \ n"); // The following command automatically goes to the paper writer after printing. write (27); writer. write (100); writer. write (4); writer. write (10); writer. close (); socketOut. close ();} catch (IOException e) {e. printStackTrace ();} catch (Exception e) {e. printStackTrace ();}}}Print text is transmitted to the String for printing. If you need to print the image, you need to convert the image into dot matrix data and print each line. For details, refer to the print method.
Then you can directly call this method to print it. Remember, android4.0 cannot be executed in the main thread. You need to create a new thread to execute this method, as shown in figure
new Thread() {<span style="white-space:pre"></span>public void run() {<span style="white-space:pre"></span>try {<span style="white-space:pre"></span>new PrintLine().print(lists, mapgs, sn);<span style="white-space:pre"></span>} catch (IOException e) {<span style="white-space:pre"></span>e.printStackTrace();<span style="white-space:pre"></span>}<span style="white-space:pre"></span>};}.start();In fact, it is not as difficult as android can imagine to call the POS machine for printing. As long as the ESC/POS commands are mastered, it is not difficult to transmit commands and data in the past, if you still need other operations, such as printing special symbols and printing other effects of fonts, you can refer to the print method to set the transmission instructions, the POS machine can recognize the data received by the command for printing. Is it quite simple? If you have any questions, we can discuss them together. I am also the first to contact the POs server.