Java Background-ZPL Command printing

Source: Internet
Author: User

 PackageCom.warehouse.common.util;Importcom.google.common.collect.Lists;ImportJava.io.File;ImportJava.io.FileInputStream;Importjava.io.UnsupportedEncodingException;Importjava.util.List;ImportJavax.print.Doc;ImportJavax.print.DocFlavor;ImportJavax.print.DocPrintJob;Importjavax.print.PrintException;ImportJavax.print.PrintService;ImportJavax.print.PrintServiceLookup;ImportJavax.print.SimpleDoc; Public classSimpleprint {byte[] dotfont; //String s_prt_buffer= "^xa\n^jma^ll180^pw660^md10^rp2^pon^lrn^lh10,0\n", s_prt = "";String s_prt = "^xa", s_prt_buffer= ""; Static classemployees{PrivateString name; PrivateString CompanyName; PrivateString identity;  PublicEmployees (String name,string companyname,string identity) { This. name=name;  This. companyname=CompanyName;  This. identity=identity; }       PublicString GetName () {returnname; }       Public voidsetName (String name) { This. Name =name; }       PublicString Getcompanyname () {returnCompanyName; }       Public voidsetcompanyname (String companyName) { This. CompanyName =CompanyName; }       PublicString getidentity () {returnidentity; }       Public voidsetidentity (String identity) { This. Identity =identity; }   }    Public Static voidMain (string[] args) {List<Employees> employeeslist=lists.newarraylist (); Employeeslist.add (NewEmployees ("Yang Zhijun", "Kai Asahi", "340321198403266210"));//Employeeslist.add (New Employees ("King's Red", "Kai Asahi", "61032119960522721X"));//Employeeslist.add (New Employees ("Li Yan", "Kai Asahi", "130582198805021210"));//Employeeslist.add (New Employees ("Lisa Wang", "Kai Asahi", "411024198811144724"));       for(intI=0;i<employeeslist.size (); i++) {Employees Employees=Employeeslist.get (i); Empprint (Employees.getidentity (), Employees.getname ()+"| "+employees.getcompanyname ()); }   }    Public Staticstring Empprint (String barcode,string empnamecomname) {string ResultStr=NULL; Try{Simpleprint a=NewSimpleprint ();         A.setcommand (Barcode,empnamecomname); String Str=A.getcommand ();         System.out.println (str); ResultStr=a.print (str); }Catch(Exception e) {resultstr= "Simpleprint empprint error";      E.printstacktrace (); }      returnResultStr; }    PublicSimpleprint ()throwsException {String paths=commonutils.getallmessage ("config", "Employee_print_zpl_path"); File File=NewFile (paths); FileInputStream FIS=Newfileinputstream (file); Dotfont=New byte[Fis.available ()];      Fis.read (Dotfont);   Fis.close (); }    Public voidSetCommand (String barcode,string namecompanyname) {PRINTCN (Namecompanyname,100, 15,30,34,1); Printbarcode (Barcode,180, 80); }   protectedString GetCommand () {//return s_prt_buffer+s_prt+ "^pq 1\n^xz";      returnS_prt + s_prt_buffer+ "^xz"; }    Publicstring print (String str)throwsprintexception {printservice Pszebra=Printservicelookup.lookupdefaultprintservice (); if(Pszebra = =NULL) {System.out.println ("No barcode printer found."); return"No barcode printer found"; } docprintjob Job=Pszebra.createprintjob (); byte[] by =str.getbytes (); Docflavor Flavor=Docflavor.byte_array.      AutoSense; Doc Doc=NewSimpledoc (By, flavor,NULL); Job.print (Doc,NULL); return NULL; }   /*** Barcode Printing *@paramBarcode String *@paramx x Coordinate *@paramy y coordinate//*@paramh Height*/   protected voidPrintbarcode (String Barcode,intXinty) {//S_prt_buffer + = "^fo" +x+ "," +y+ "^BY2,3,50^BCN,180,N,N,N,A^FD" + barcode + "^fs\n";S_prt_buffer + = "^fo" +x+ "," +y+ "^BY2,3,60^BCN,180,N,N,N,A^FD" + barcode + "^fs\n"; }   /*** Print English characters, numbers *@paramSTR *@paramx *@paramy*/   protected voidPrintchar (String str,intXintYintHintW) {System.out.println (str); S_prt_buffer+ = "^fo" +x+ "," +y+ "^a0," +h+ "," +w+ "^FD" +str+ "^fs"; }   protected voidPrintcharr (String str,intXintYintHintW) {System.out.println (str); S_prt_buffer+ = "^fo" +x+ "," +y+ "^a0r," +h+ "," +w+ "^FD" +str+ "^fs"; }   /*** Print Chinese String *@paramSTRCN *@paramx *@paramy*/   protected voidPRINTCN (String STRCN,intXintYintHintWintb) {System.out.println (STRCN); byte[] ch =str2bytes (STRCN);      System.out.println (ch.length);  for(intOff = 0; Off <ch.length;) {         if(((int) Ch[off] & 0x00ff) >= 0xA0) {            intQcode = Ch[off] & 0xff; intWcode = Ch[off + 1] & 0xFF; S_prt_buffer=S_prt_buffer+ String.Format ("^fo%d,%d^xg0000%01x%01x,%d,%d^fs\n", x, Y, Qcode, wcode,b,b); S_prt+ = String.Format ("~dg0000%02x%02x,00072,003,\n", Qcode, Wcode); Qcode= (Qcode + 128-32) & 0X00FF; Wcode= (Wcode + 128-32) & 0X00FF; intOffset = ((int) * qcode-16) * 94 * 72 + ((int) wcode-1)                  * 72;  for(intj = 0; J < 72; J + = 3) {Qcode= (int) Dotfont[j + offset] & 0X00FF; Wcode= (int) Dotfont[j + offset + 1] & 0X00FF; intQcode1 = (int) Dotfont[j + offset + 2] & 0X00FF; S_prt+ = String.Format ("%02x%02x%02x\n", Qcode, Wcode, qcode1); } x= x + 25*C; Off= off + 2; } Else if(((int) Ch[off] & 0x00FF) < 0xA0) {Printchar (String.Format ("%c", Ch[off]), X, Y, H, W);//S_prt_buffer + = String.Format ("^fo" +x+ "," +y+ "^a0," +h+ "," +w+ "^fd%c^fs\n", Ch[off]);//S_prt_buffer + = "^fo" +x+ "," +y+ "^a0," +h+ ", 20^fd" +ch[off]+ "^fs\n";x = x + 15; Off++; }      }   }   byte[] str2bytes (String s) {if(NULL= = S | | "". Equals (s)) {         return NULL; }      byte[] Abytes =NULL; Try{abytes= S.getbytes ("gb2312"); } Catch(Unsupportedencodingexception ex) {}returnabytes; }}

Java Background-ZPL Command printing

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.