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
- /**
- * @param print_data
- * Small Ticket main data
- * @param gs_info
- * Small ticket comes with information
- * @param CAIDAN_SN
- * Small Ticket 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 {
- Barcode Printing 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;
- 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;
- The font is enlarged one-fold vertically
- byte[] Clear_font = new Byte[3];
- Clear_font[0] = 0x1c;
- CLEAR_FONT[1] = 0x21;
- CLEAR_FONT[2] = 0;
- Calculate Total Amount
- int price = 0;
- Initializing the printer
- Socketout.write (27);
- Socketout.write (64);
- Socketout.write (Fd_font);//font magnification
- Socketout.write (font_b);//FONT Bold
- Socketout.write (10);
- Writer.write ("" + Gs_info.get ("gs_name") + "\ r \ n");
- 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
- Socketout.write (Clear_font);
- Socketout.write (10);
- 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");
- 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,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 Parameters
- 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);
- Print on each line
- for (int i = 0; i < bmp.getheight ()/+ 1; i++) {
- Socketout.write (escbmp);
- for (int j = 0; J < Bmp.getwidth (); j + +) {
- for (int k = 0; k < k++) {
- if ((((i) + K) < Bmp.getheight ()) {
- Pixelcolor = Bmp.getpixel (J, (I *) + K);
- if (pixelcolor! =-1) {
- DATA[K/8] + = (byte) (>> (k% 8));
- }
- }
- }
- Socketout.write (data);
- Reset Parameters
- Data[0] = (byte) 0x00;
- DATA[1] = (byte) 0x00;
- DATA[2] = (byte) 0x00;
- }
- Line break
- byte[] Byte_send1 = new byte[2];
- Byte_send1[0] = 0x0d;
- BYTE_SEND1[1] = 0x0a;
- Socketout.write (BYTE_SEND1);
- }
- Line break
- 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 Total" + (Print_data.size ()-1) + "pieces of goods, total costs:" + price + "Yuan \ r \ n");
- Writer.write ("------------------------------\ r \ n");
- Writer.write ("Thank you for your patronage \ r \ n");
- The following instructions automatically take the paper after printing is complete
- Writer.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 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
- 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 ();
Actually Android call PO
Android Control POS Printer (ii)