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 static int print_port = 9100; Printer port default is 9100 do not modify the printer port, modify the May not connect
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
/** * @param print_data * Small ticket main data * @param gs_info * Small ticket with information * @param CAIDAN_SN * Small ticket number */pu blic 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 {//Barcode print directive 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 font magnification directive byte[] Fd_font = new Byte[3]; Fd_font[0] = 0x1c; FD_FONT[1] = 0x21; FD_FONT[2] = 4;//font bold instruction byte[] Font_b = new Byte[3]; Font_b[0] = 27; Font_b[1] = 33; FONT_B[2] = 8;//font magnification One times byte[] Clear_font = new Byte[3]; Clear_font[0] = 0x1c; CLEAR_FONT[1] = 0x21; CLEAR_FONT[2] = 0;//calculates the total amount int price = 0;//initializes the printer Socketout.write; socketout.write (+); Socketout.write (Fd_font);// Font magnification socketout.write (font_b),//font bold Socketout.write (), Writer.write ("" + Gs_info.get ("GS_name ") +" \ r \ n "); Writer.flush ();//key, it is important, otherwise the instruction one-time output, the following instructions overwrite the previous instruction, resulting in cancellation of the amplification instruction invalid socketout.write (Clear_font); Socketout.write, Writer.write ("NO:" + caidan_sn + "\ r \ n"), Writer.write ("------------------------------\ r \ n"); Writer.write ("Buyer's name:" + gs_info.get ("gs_user_name") + "\ r \ n"), Writer.write ("Address:" + gs_info.get ("gs_address") + "\ r \ n"); Writer.write ("Contact Phone:" + gs_info.get ("Gs_tel") + "\ r \ n"), Writer.write ("payment method:" + gs_info.get ("Gs_pay_type") + "\ r \ n"); WRI Ter.write ("------------------------------\ r \ n"); Writer.write (Fix_string_lenth (1,colum_name[0], 4) + Fix_string_ Lenth (0,colum_name[1], +) + 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 () , +) + fix_string_lenth (1,print_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 two-dimensional 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; DATA[2] = (byte) 0x00; Reset parameter int pixelcolor; ESC * M NL nH bitmap 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); Each line is printed for (int i = 0; i < bmp.getheight ()/i++ + 1) {socketout.write (escbmp); for (int j = 0; J < Bmp.getwidth (); j + +) {for (int k = 0; k < k++) { if (((i * 24) +K) < Bmp.getheight ()) {Pixelcolor = Bmp.getpixel (J, (I *) + K); if (pixelcolor! =-1) {DATA[K/8] + = (byte) (>> (k% 8)); }}} socketout.write (data); Reset parameter Data[0] = (byte) 0x00; DATA[1] = (byte) 0x00; DATA[2] = (byte) 0x00; }//newline byte[] byte_send1 = new byte[2]; Byte_send1[0] = 0x0d; BYTE_SEND1[1] = 0x0a; Socketout.write (BYTE_SEND1); }//newline byte[] Byte_send2 = new byte[2]; Byte_send2[0] = 0x0d; BYTE_SEND2[1] = 0x0a; Send test information socketout.write (BYTE_SEND2); Writer.write ("------------------------------\ r \ n"); Writer.write ("This single"+ (Print_data.size ()-1) +" pieces of goods, total cost: "+ price +" Yuan \ r \ n "); Writer.write ("------------------------------\ r \ n "); Writer.write ("Thank you for your patronage \ r \ n");//The following instructions are printed automatically after the completion of the paper writer.write (writer.write); Writer.write (4); Writer.write (10); Writer.close (); Socketout.close ();} catch (IOException e) {e.printstacktrace ();} catch (Exception e) {e.printstacktrace ()}}}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
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, Android calls POS machine to print is not so difficult to do, as long as the Esc/pos command, how to transfer instructions and data in the past, it is not difficult, if you need other operations, such as printing special symbols, fonts and other effects of printing, etc. Can be referenced in the Print method to set the mode of transmission instructions, POS can be recognized to the command received data to print, is not quite simple? What is the problem we can discuss together, I am also the first contact POS machine.
Android Control POS Machine graphic printing (ii)